SQL statement in VBA

  • Thread starter Thread starter Stanley
  • Start date Start date
S

Stanley

I have a very long SQL statment that I am coding in VBA. I can't fit it on a
line. Is there any way I can extext it? I used the _ character to no avail.
Thanks,
Stan
 
I have a very long SQL statment that I am coding in VBA. I can't fit it on a
line. Is there any way I can extext it? I used the _ character to no avail.
Thanks,
Stan

You need to use the space and _ characters ( _) together....

"Select tblName.FieldA, tblName.FieldB" _
& " From tblName ... etc.... "
 
I usually do the following:

Dim SQL as String

SQL = "Select Field1, Field2, Field3 "
SQL = SQL & " FROM Table "
SQL = SQL & " INNER JOIN Table2 on Table1.field = Table2.Field "
SQL = SQL & " WHERE Field1=True "


Something like that...

Chris
 
I have a very long SQL statment that I am coding in VBA. I can't fit it on a
line. Is there any way I can extext it? I used the _ character to no avail.
Thanks,
Stan

Two things: you should not try to split a quote-delimited string across
multiple lines; and the continuation flag is not "_" but " _" - a space
followed by an underscore. Try something like

Dim strSQL As String
strSQL = "SELECT this, that, theother" _
& " FROM tablename" _
& " WHERE this = 1223 AND That = 'XYX'" _
& <etc etc>

Note that you're using & to concatenate text strings together, and that those
text strings should contain the necessary blanks; I'm using " WHERE in order
to prevent the resulting SQL string from containing

tablenameWHERE

all run together.
 
Back
Top