VBA- how to use' ' correctly

Jana.Luo

Board Regular
Joined
Jan 15, 2009
Messages
109
Hi there,

Part of the script like this below. The values in some columns are varchar. But this script works fine until it meets a name like this KK K'mm.

How can I modify the script to avoid this problem?

====================================

Sub Test1()
Dim SQL As String
Dim X As Integer

-------------------------
C.Execute "Truncate Table Agent_list"

For X = 2 To 142


SQL = "Insert Into Agent_list Values ( '" & Cells(X, 1) & "', "


If Cells(X, 2) = "" Then
SQL = SQL & "Null, "
Else
SQL = SQL & "'" & Cells(X, 2) & "', "
End If
---------------------------


C.Execute SQL

Next X

============================

many thanks,
Jana:confused:
 
the message box came out :

Run-time error 'xxxxxxxxxx)':
Incorrect syntax near')'.

and when my cursor over the

C.Execute SQL
the message was :

SQL="Flase'AKL','XXX')"

I think the result should be "('AKL','XXX')" , right?
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I guess so, but even that is only part of a sql statement. You seem to be not constructing the proper sql command text. I'd start with sample data that doesn't have single quotes in them, then when that's working move up to the data that has the single quotes in it. It also helps (if you can) to be able to test the raw sql directly on your database (such as in SQL Server Management Studio, in an Access query, or at a MySQL prompt or Oracle prompt).
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top