Solved

VB6 - Save to access dbase issue with apostrophe

Posted on 2013-11-03
10
310 Views
Last Modified: 2013-11-06
Hi all

I have this application that will save date from mu MSHFlexgrid1 to my Microsoft Access dbase.

But every time that i have a name with ' (apostrophe) in the TextMatrix, ex: L'EXTRA, it cause an issue that it dont want to save it cause missing operation in query expression (Error syntax).

But if i remove the ', it same with no problem.

Would you know why and how i can fix this?

Thanks again for your help


Macro to svae the data
  Dim i As Long, strSQL As String
            Dim j As Long
            Dim MyConn As ADODB.Connection

            Set MyConn = New ADODB.Connection

            MyConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=" & MDIForm1.dta_base_link_audit_tms.Text


            MyConn.Open


            With MSHFlexGrid1
                For i = .FixedRows To .Rows - 1
                    strSQL = "INSERT INTO " & Form3.rate_table_audit.Text & _
                             " (TYPE_OF_RATE, CARRIER_ID, MODE_RATE, EFFECTIVE_DATE, EXPIRATION_DATE, SERVICE_COMMITMENT, EquipmentClass, RATE_GROUP_ID, RATE_TYPE, MINIMUM_CHARGE, VARIABLE_RATE, FLAT_RATE, UNIT_TYPE, UNIT_SIZE, PER_UNIT_CHARGE, CURRENCY_RATE, FREE_STOP, ADD_STOP_RATE, MAX_STOPS, ORIGIN, ORIGIN_COUNTRY, DESTINATION, DESTINATION_COUNTRY, FUEL, FUEL_RATE, NOTES, INTERNAL_COMMENTS, TODAY_DATE, REQUESTER, DONE_BY, ID_RECORDS, WEEK_OF_YEAR) VALUES " & _
                             " ('" & .TextMatrix(i, 0) & "', '" & .TextMatrix(i, 1) & "', '" & _
                             .TextMatrix(i, 2) & "', '" & .TextMatrix(i, 3) & "', '" & .TextMatrix(i, 4) & "', '" & .TextMatrix(i, 5) & "', '" & .TextMatrix(i, 6) & "', '" & .TextMatrix(i, 7) & "', '" & .TextMatrix(i, 8) & "', '" & .TextMatrix(i, 9) & "', '" & .TextMatrix(i, 10) & "', '" & .TextMatrix(i, 11) & "', '" & .TextMatrix(i, 12) & "', '" & .TextMatrix(i, 13) & "', '" & .TextMatrix(i, 14) & "', '" & .TextMatrix(i, 15) & "', '" & .TextMatrix(i, 16) & "', '" & .TextMatrix(i, 17) & "', '" & .TextMatrix(i, 18) & "', '" & .TextMatrix(i, 19) & "', '" & .TextMatrix(i, 20) & "', '" & .TextMatrix(i, 21) & "', '" & .TextMatrix(i, 22) & "', '" & .TextMatrix(i, 23) & "', '" & .TextMatrix(i, 24) & "', '" & .TextMatrix(i, 25) & "', '" & .TextMatrix(i, 26) & "', '" & date1.Caption & "', '" & USER1.Text & "', '" & name_done_by.Text & "', '" & Format(Now, "yyyymmdd") & Format(Time, "hhmmss") & "', '" & week1.Caption & "')"

                    MyConn.Execute (strSQL)
                Next

                MyConn.Close
                Set MyConn = Nothing
            End With

Open in new window

0
Comment
Question by:Wilder1626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 39619912
You should escape apostrophes inside text values. If not, then Access understand that your text (String)  terminates on this apostrophe, as apostrophe is the character for limiting strings in Access.

For escaping a single quote (= apostrophe) in Access, you just have to add another apostrophe.

So you should change your .TextMatrix(i, xxx) by: replace(.TextMatrix(i,xxx), "'", "''")

Hope it helps. Regards.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39619959
HI

but if i replace(.TextMatrix(i,xxx), "'", "''") and my value in my textbox = L'EXTRA , would that just remove the apostrophe even if i need to keep it? My problem is that some cities have apostrophe and i need to keep it.
0
 
LVL 15

Expert Comment

by:gplana
ID: 39619965
No, it won't remove it. It will change it by 2 apostrophes, which makes Access interpret a single apostrophe, because two apostrophes between apostrophes are interpreted by access as an apostrophe between apostrophes.

Just try it ;)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Author Comment

by:Wilder1626
ID: 39620014
I updated the script to the below. I will test this and let you know about the result. So far, it works on Textmatrix 19 in both scenarios, with or without apostrophie

            
            With MSHFlexGrid1
                For i = .FixedRows To .Rows - 1
                On Error GoTo save_sql
                    strSQL = "INSERT INTO " & Form3.rate_table_audit.Text & _
                             " (TYPE_OF_RATE, CARRIER_ID, MODE_RATE, EFFECTIVE_DATE, EXPIRATION_DATE, SERVICE_COMMITMENT, EquipmentClass, RATE_GROUP_ID, RATE_TYPE, MINIMUM_CHARGE, VARIABLE_RATE, FLAT_RATE, UNIT_TYPE, UNIT_SIZE, PER_UNIT_CHARGE, CURRENCY_RATE, FREE_STOP, ADD_STOP_RATE, MAX_STOPS, ORIGIN, ORIGIN_COUNTRY, DESTINATION, DESTINATION_COUNTRY, FUEL, FUEL_RATE, NOTES, INTERNAL_COMMENTS, TODAY_DATE, REQUESTER, DONE_BY, ID_RECORDS, WEEK_OF_YEAR) VALUES " & _
                             " ('" & .TextMatrix(i, 0) & "', '" & .TextMatrix(i, 1) & "', '" & _
                             .TextMatrix(i, 2) & "', '" & .TextMatrix(i, 3) & "', '" & .TextMatrix(i, 4) & "', '" & .TextMatrix(i, 5) & "', '" & .TextMatrix(i, 6) & "', '" & .TextMatrix(i, 7) & "', '" & .TextMatrix(i, 8) & "', '" & .TextMatrix(i, 9) & "', '" & .TextMatrix(i, 10) & "', '" & .TextMatrix(i, 11) & "', '" & .TextMatrix(i, 12) & "', '" & .TextMatrix(i, 13) & "', '" & .TextMatrix(i, 14) & "', '" & .TextMatrix(i, 15) & "', '" & .TextMatrix(i, 16) & "', '" & .TextMatrix(i, 17) & "', '" & .TextMatrix(i, 18) & "', '" & .TextMatrix(i, 19) & "', '" & .TextMatrix(i, 20) & "', '" & .TextMatrix(i, 21) & "', '" & .TextMatrix(i, 22) & "', '" & .TextMatrix(i, 23) & "', '" & .TextMatrix(i, 24) & "', '" & .TextMatrix(i, 25) & "', '" & .TextMatrix(i, 26) & "', '" & date1.Caption & "', '" & USER1.Text & "', '" & name_done_by.Text & "', '" & Format(Now, "yyyymmdd") & Format(Time, "hhmmss") & "', '" & week1.Caption & "')"

save_sql:
                    strSQL = "INSERT INTO " & Form3.rate_table_audit.Text & _
                             " (TYPE_OF_RATE, CARRIER_ID, MODE_RATE, EFFECTIVE_DATE, EXPIRATION_DATE, SERVICE_COMMITMENT, EquipmentClass, RATE_GROUP_ID, RATE_TYPE, MINIMUM_CHARGE, VARIABLE_RATE, FLAT_RATE, UNIT_TYPE, UNIT_SIZE, PER_UNIT_CHARGE, CURRENCY_RATE, FREE_STOP, ADD_STOP_RATE, MAX_STOPS, ORIGIN, ORIGIN_COUNTRY, DESTINATION, DESTINATION_COUNTRY, FUEL, FUEL_RATE, NOTES, INTERNAL_COMMENTS, TODAY_DATE, REQUESTER, DONE_BY, ID_RECORDS, WEEK_OF_YEAR) VALUES " & _
                             " ('" & .TextMatrix(i, 0) & "', '" & .TextMatrix(i, 1) & "', '" & _
                             .TextMatrix(i, 2) & "', '" & .TextMatrix(i, 3) & "', '" & .TextMatrix(i, 4) & "', '" & .TextMatrix(i, 5) & "', '" & .TextMatrix(i, 6) & "', '" & .TextMatrix(i, 7) & "', '" & .TextMatrix(i, 8) & "', '" & .TextMatrix(i, 9) & "', '" & .TextMatrix(i, 10) & "', '" & .TextMatrix(i, 11) & "', '" & .TextMatrix(i, 12) & "', '" & .TextMatrix(i, 13) & "', '" & .TextMatrix(i, 14) & "', '" & .TextMatrix(i, 15) & "', '" & .TextMatrix(i, 16) & "', '" & .TextMatrix(i, 17) & "', '" & .TextMatrix(i, 18) & "', '" & Replace(.TextMatrix(i, 19), "'", "''") & "', '" & .TextMatrix(i, 20) & "', '" & .TextMatrix(i, 21) & "', '" & .TextMatrix(i, 22) & "', '" & .TextMatrix(i, 23) & "', '" & .TextMatrix(i, 24) & "', '" & .TextMatrix(i, 25) & "', '" & .TextMatrix(i, 26) & "', '" & date1.Caption & "', '" & USER1.Text & "', '" & name_done_by.Text & "', '" & Format(Now, "yyyymmdd") & Format(Time, "hhmmss") & "', '" & week1.Caption & "')"



                    MyConn.Execute (strSQL)
                Next
                

                   
                MyConn.Close
                Set MyConn = Nothing
            End With

Open in new window

0
 
LVL 15

Accepted Solution

by:
gplana earned 400 total points
ID: 39620063
Sorry, but have you change the script?

I think you should do it like this:

            With MSHFlexGrid1
                For i = .FixedRows To .Rows - 1
                On Error GoTo save_sql
                    strSQL = "INSERT INTO " & Form3.rate_table_audit.Text & _
                             " (TYPE_OF_RATE, CARRIER_ID, MODE_RATE, EFFECTIVE_DATE, EXPIRATION_DATE, SERVICE_COMMITMENT, EquipmentClass, RATE_GROUP_ID, RATE_TYPE, MINIMUM_CHARGE, VARIABLE_RATE, FLAT_RATE, UNIT_TYPE, UNIT_SIZE, PER_UNIT_CHARGE, CURRENCY_RATE, FREE_STOP, ADD_STOP_RATE, MAX_STOPS, ORIGIN, ORIGIN_COUNTRY, DESTINATION, DESTINATION_COUNTRY, FUEL, FUEL_RATE, NOTES, INTERNAL_COMMENTS, TODAY_DATE, REQUESTER, DONE_BY, ID_RECORDS, WEEK_OF_YEAR) VALUES " & _
                             " ('" & replace(.TextMatrix(i, 0),"'","''") & "', '" & .replace(TextMatrix(i, 1),"'","''") & "', '" & _
                             replace(.TextMatrix(i, 2),"'","''") & "', '" & replace(.TextMatrix(i, 3),"'","''") & "', '" & replace(.TextMatrix(i, 4),"'","''") & "', '" & replace(.TextMatrix(i, 5),"'","''") & "', '" & replace(.TextMatrix(i, 6),"'","''") & "', '" & replace(.TextMatrix(i, 7),"'","''") & "', '" & replace(.TextMatrix(i, 8),"'","''") & "', '" & replace(.TextMatrix(i, 9),"'","''") & "', '" & replace(.TextMatrix(i, 10),"'","''") & "', '" & .replace(TextMatrix(i, 11),"'","''") & "', '" & replace(.TextMatrix(i, 12),"'","''") & "', '" & replace(.TextMatrix(i, 13),"'","''") & "', '" & replace(.TextMatrix(i, 14),"'","''") & "', '" & replace(.TextMatrix(i, 15),"'","''") & "', '" & replace(.TextMatrix(i, 16),"'","''") & "', '" & replace(.TextMatrix(i, 17),"'","''") & "', '" & replace(.TextMatrix(i, 18),"'","''") & "', '" & replace(.TextMatrix(i, 19),"'","''") & "', '" & replace(.TextMatrix(i, 20),"'","''") & "', '" & replace(.TextMatrix(i, 21),"'","''") & "', '" & replace(.TextMatrix(i, 22),"'","''") & "', '" & replace(.TextMatrix(i, 23),"'","''") & "', '" & replace(.TextMatrix(i, 24),"'","''") & "', '" & replace(.TextMatrix(i, 25),"'","''") & "', '" & replace(.TextMatrix(i, 26),"'","''") & "', '" & replace(date1.Caption,"'","''") & "', '" & replace(USER1.Text,"'","''") & "', '" & replace(name_done_by.Text,"'","''") & "', '" & Format(Now, "yyyymmdd") & Format(Time, "hhmmss") & "', '" & replace(week1.Caption,"'","''") & "')"

save_sql:
                    strSQL = "INSERT INTO " & Form3.rate_table_audit.Text & _
                             " (TYPE_OF_RATE, CARRIER_ID, MODE_RATE, EFFECTIVE_DATE, EXPIRATION_DATE, SERVICE_COMMITMENT, EquipmentClass, RATE_GROUP_ID, RATE_TYPE, MINIMUM_CHARGE, VARIABLE_RATE, FLAT_RATE, UNIT_TYPE, UNIT_SIZE, PER_UNIT_CHARGE, CURRENCY_RATE, FREE_STOP, ADD_STOP_RATE, MAX_STOPS, ORIGIN, ORIGIN_COUNTRY, DESTINATION, DESTINATION_COUNTRY, FUEL, FUEL_RATE, NOTES, INTERNAL_COMMENTS, TODAY_DATE, REQUESTER, DONE_BY, ID_RECORDS, WEEK_OF_YEAR) VALUES " & _
                             " ('" & replace(.TextMatrix(i, 0),"'","''") & "', '" & .replace(TextMatrix(i, 1),"'","''") & "', '" & _
                             replace(.TextMatrix(i, 2),"'","''") & "', '" & replace(.TextMatrix(i, 3),"'","''") & "', '" & replace(.TextMatrix(i, 4),"'","''") & "', '" & replace(.TextMatrix(i, 5),"'","''") & "', '" & replace(.TextMatrix(i, 6),"'","''") & "', '" & replace(.TextMatrix(i, 7),"'","''") & "', '" & replace(.TextMatrix(i, 8),"'","''") & "', '" & replace(.TextMatrix(i, 9),"'","''") & "', '" & replace(.TextMatrix(i, 10),"'","''") & "', '" & replace(.TextMatrix(i, 11),"'","''") & "', '" & replace(.TextMatrix(i, 12),"'","''") & "', '" & replace(.TextMatrix(i, 13),"'","''") & "', '" & replace(.TextMatrix(i, 14),"'","''") & "', '" & replace(.TextMatrix(i, 15),"'","''") & "', '" & replace(.TextMatrix(i, 16),"'","''") & "', '" & replace(.TextMatrix(i, 17),"'","''") & "', '" & replace(.TextMatrix(i, 18),"'","''") & "', '" & Replace(.TextMatrix(i, 19), "'", "''") & "', '" & replace(.TextMatrix(i, 20),"'","''") & "', '" & replace(.TextMatrix(i, 21),"'","''") & "', '" & replace(.TextMatrix(i, 22),"'","''") & "', '" & replace(.TextMatrix(i, 23),"'","''") & "', '" & replace(.TextMatrix(i, 24),"'","''") & "', '" & replace(.TextMatrix(i, 25),"'","''") & "', '" & replace(.TextMatrix(i, 26),"'","''") & "', '" & replace(date1.Caption,"'","''") & "', '" & replace(USER1.Text,"'","''") & "', '" & replace(name_done_by.Text,"'","''") & "', '" & Format(Now, "yyyymmdd") & Format(Time, "hhmmss") & "', '" & replace(week1.Caption,"'","''") & "')"



                    MyConn.Execute (strSQL)
                Next
                

                   
                MyConn.Close
                Set MyConn = Nothing
            End With

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39620088
why i did it my way was because if i have an apostrophe, i will go to save_sql and use:
Replace(.TextMatrix(i, 19), "'", "''")

Open in new window



but if i dont have any apostrophe, it will use:
 
.TextMatrix(i, 19)

Open in new window


The thing is that if i replace when i don't have apostrophe, it was not transferring to Access properly.
0
 
LVL 15

Expert Comment

by:gplana
ID: 39620096
if it doesn't have an apostrophe, you can still use replace, which will make no changes.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39620098
ok, let me try it again
0
 
LVL 46

Assisted Solution

by:aikimark
aikimark earned 100 total points
ID: 39624962
you could use a replace function to change the apostrophe characters into ASCII 146 characters, which would appear to display properly in your grid.  You would need to change these back to apostrophe, ASCII 39, characters when you saved the data.  If someone uses an apostrophe character in a grid search, you would need to have your code do the ASCII 146 replacement.
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 39626969
Perfect thanks a lot for your help

it works great now.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question