Skip to content
Advertisement

Adding quotes into a sql query with Vba code

I can recover data from my database using vba, but I have to add quotes on the result of column (“value1”, “Value2”, “Value3″ etc) But I do not know how to add quotes inside the SQL query, when using ” it’s wrong the request. I am used to do this kind of concat in SQL but in VBA I am not able to do it. Thanks for your help.

Sub CopyDataFromDB()
Dim DKCon As ADODB.Connection
Dim DKData As ADODB.Recordset
Dim iCols As Integer
Set DKCon = New ADODB.Connection
Set DKData = New ADODB.Recordset


DKCon.ConnectionString = SqlProvider
DKCon.Open

With DKData
        .ActiveConnection = DKCon
        .Source = "select sitsmhlccd from sitsmemoh"
        .LockType = adLockReadOnly
        .CursorType = adOpenForwardOnly
        .Open
End With

Worksheets.Add
ActiveSheet.Range("A2").CopyFromRecordset DKData
DKData.Close
DKCon.Close

Advertisement

Answer

When you use VBA, you need to double double-quote if you want to use it inside a string:

"select '""'+ sitsmhlccd +'""' from sitsmemoh"
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement