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:
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Jana

What database are you trying to use this with?

Most have some way of 'escaping' characters like.

It really depends on the the database involved how to deal with this sort of thing.
 
Upvote 0
what I want to do is I want to creat a temp table into the microsoft sql server by importing a spreadsheet. and I want to make a vba script to link the spreadsheet to the temp table in able to update the table value easily when I update the spreadsheet.

Does this explanation help? :-)
 
Upvote 0
You are having a problem because of the single quote in the last name. Try the following code and see if this solves your problem.

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

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

For X = 2 To 10
SQL = "Insert Into Agent_list Values ( '" & Cells(X, 1) & "', "
If Cells(X, 2) = "" Then
SQL = SQL & "Null, "
Else
If (InStr(1, Cells(X, 2), "'") > 0) Then
SQL = """" & Cells(X, 2) & """"
Else
SQL = SQL & "'" & Cells(X, 2) & "', "
End If
End If
'---------------------------
'C.Execute SQL

Next X
End Sub
 
Upvote 0
In a pinch, this will probably work:
Code:
SQL = SQL & "'" & Replace(Cells(X, 2),"'","''") & "', "
 
Upvote 0
More fully, I think:
Code:
For X = 2 To 10

    SQL = SQL & " Insert Into Agent_list Values ( "
    SQL = SQL &  "'" & Cells(X, 1).Value & "', "
    If Cells(X, 2) = "" Then
        SQL = SQL & "Null"
    Else
        SQL = SQL & Replace(Cells(X, 2),"'","''") & "' "
    End If
    SQL = SQL & ");"

    C.Execute SQL

Next x

We are using '' to indicate a single quote that is part of the quoted string itself.

Note: your SQL will be more robust if you use the field names (that way if the table structure changes, such as adding a field, the SQL still works:
Code:
INSERT INTO Agent_list ([COLOR="Blue"]AgentID, AgentName[/COLOR]) Values (1, 'O''Bama');
 
Last edited:
Upvote 0
Hi Xenou,

If the situation only happens in cell (x,1), should I only use the first part of your script which related to cell (x,1)?

thanks,
Jana
 
Upvote 0
Correct. I think I got my 1's and 2's mixed up. If both are text, then may as well cover both values:

Code:
For X = 2 To 10

    SQL = SQL & " Insert Into Agent_list Values ( "
    SQL = SQL = SQL & "'" & Replace(Cells(X, 1),"'","''") & "' , "
    If Cells(X, 2) = "" Then
        SQL = SQL & "Null"
    Else
        SQL = SQL & "'" & Replace(Cells(X, 2),"'","''") & "' "
    End If
    SQL = SQL & ");"

    C.Execute SQL

Next x

In the above, (X,1) cannot be null, otherwise both are treated the same. There is a remote possibility of failure if you actually have '' in a cell (two single quotes next to each other), since this would become '''' (four single quotes). But I think that very unlikely.
 
Upvote 0
Code:
SQL="Flase'AKL'",'XX')"
This doesn't look like a valid construction for any kind of sql that I recognize - back up a step. What's the outcome supposed to look like?

Note: was it supposed to be:
Code:
SQL = [COLOR="Blue"]SQL &[/COLOR] "Flase'AKL'",'XX')"
 
Upvote 0

Forum statistics

Threads
1,224,598
Messages
6,179,818
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