I'm attempting to retreive a resultset from a MS Access database using VBA for Excel. In the VBA code, I'm constructed a string equal to:
strSql = "SELECT * FROM Pricing WHERE Account In (''1234'', ''ABCD'') '; "
Note that there are 2 single quotes around the strings within the SQL statement. There is also a single quote before the semi-colon. If I'm not mistaken, this evaluates to:
SELECT * FROM Pricing WHERE Account In ('1234', 'ABCD') ;
This query works fine when run directly in MS Access. However, in Excel VBA, I keep getting the Run-time error:
Syntax error (missing operator) in query expression 'Account In (''1234'', ''ABCD'') '; '
Notice that this error actually cut off the first half of the SQL statement.
I've tried a few variations, using double-quotes, double double-quotes, no quotes, etc. etc.
Any advice?
Thanks.
"SELECT * FROM Pricing WHERE Account In ('1234', 'ABCD');"
? – MonkpitDebug.Print strSql
, run the code and see what it shows you in the Immediate window. – HansUp3 o'clock
for example ... you would need to double up that quote like thisIn ('abc', '3 o''clock')
But more generally, always verify that the text of the SQL statement your code builds matches what you expect --- and that's whereDebug.Print
and the Immediate window can be useful. – HansUp