How to use Quotations in a ADO sql string?

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
Imagine I have a query saved in a string variable that is passed to an Connection.Open command via ado. The problem I am having is that I dont know how to get to use double quotes in my sql code in my excel vba code.

An example is as follows:

Code:
SqlQuery="Select Table1.STATE_ABREVIATION & "-" & Format(Table1.IDS,"00000000")) AS IDS_for_Display From Table1"

This draws an error. I tried to double up my quotes as follows but still draws an error.

Code:
SqlQuery="Select Table1.STATE_ABREVIATION & ""-"" & Format(Table1.IDS,""00000000"")) AS IDS_for_Display From Table1"
 
Last edited:

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)
some ideas, yes.

it is best to explain what the error is. Simply stating "This draws an error" without explaining the error is not very helpful

when I run the code it does not error

debug.print the string to see if it is what you're expecting
when I debug.print there is no issue

maybe your SQL uses ' instead of " for text. so
Code:
SqlQuery = "Select Table1.STATE_ABREVIATION & '-' & Format(Table1.IDS,'00000000')) AS IDS_for_Display From Table1"

Maybe the table & field names don't match the database table & field names?
Does the database have a table Table1?
Does the table have fields STATE_ABREVIATION and IDS

just in case it helps, where I live ABBREVIATION would be expected, not ABREVIATION

all the best
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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