This is a Query in VBA (Access 2007) I have 3 strings defined:
str_a = 'db.col1 = 5' str_b = ' and db.col2 = 123' str_c = ' and db.col3 = 42'
Then I use these in the WHERE part of my Query:
'WHERE '' & str_a & '' '' & str_b & '' '' & str_c & '' ;'
This fails, but If I paste in the strings like this:
'WHERE db.col1 = 5 and db.col2 = 123 and db.col3 = 42;'
It works perfectly. I’m guessing the syntax is wrong when using multiple variables in a string.
Anyone have any hints?
will include single quotes within your completed WHERE clause. And the working version you showed us has none:
So, try constructing the
WHEREclause with no single quotes:For debugging purposes, it’s useful to view the final string after VBA has finished constructing it. If you’re storing the string in a variable named
strSQL, you can use:to display the finished string in the Immediate Window of the VB Editor. (You can get to the Immediate Window with the CTRL+g keyboard shortcut.)
Alternatively, you could display the finished string in a message box window: