0

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
1
  • 2
    What SQL technology? Commented Apr 19, 2019 at 12:14

1 Answer 1

1

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

"select '""'+ sitsmhlccd +'""' from sitsmemoh"
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this solution but it dosen't work. I have an ugly run-time error: Invalid column name '+sitsmhlccd +'
@YouFec: what is the back-end database?
@Louis: I am not concatenating strings, "+" symbol is inside of a string
@Louis: to concatenate double quotes with the result that's coming from the database, that's what OP is asking. The "+" sign will be part of the database query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.