Link to home
Start Free TrialLog in
Avatar of mercia
mercia

asked on

Spliting SQL?

How do you split a SQL statment over more than one line in VB? For example

Select("Field1, field2, field3, field4
From data
where field1 = "test")

what do I need to put at the end of these lines to stop errors?
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

>>Select("Field1, field2, field3, field4
From data
where field1 = "test")
??

Should be like this?

"Select Field1, field2, field3, field4 From data where field1 = 'test'"
Avatar of mercia
mercia

ASKER

Sorry my actual query is longer than this and therefore needs to be split over more than one line, the sql shown was just an example.
try

strSql = "Select(Field1, field2, field3, field4 " + _
         "From Data " + _
         "where field1 = 'test')"
Avatar of mercia

ASKER

Still get errors if I try this
Hi mercia,

To split any statement over more than one line use & _

rst.Open "Select " & _
                 "Field1, Field2, Field3 " & _
                 "From MyTable " & _
                 "Where Field1 = 'Test'", _
                 cnn, adOpenStatic,adLockReadOnly

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com
hello merica,

try this one out.

1.  sql="Select Field1, field2, field3, field4" & _
2.    "From data " & _
3.    "where field1 = test"

or else

1.  sql="Select Field1, field2, field3, field4"
2.   sql =sql & "From data " 
3.   sql= sql & "where field1 = test"

if test is a string parameter which u want to check then change

line
 3. "where field1='" &test &"'"


happiness always...



I think you need the Carriage Return Line Feed in there (in vb, use vbCrLf predefined constant) :

st.Open "Select " & vbcrlf & _
                 "Field1, Field2, Field3 " & vbcrlf &  _
                 "From MyTable " & vbcrlf &  _
                 "Where Field1 = 'Test'" , _
                 cnn, adOpenStatic,adLockReadOnly
try


strSql = "Select(Field1, field2, field3, field4 " + _
        "From Data " + _
        "where field1 = 'test')"

Be carefully to left one space between "+" and "_"
try

strSQL = "SELECT Field1, Field1, Field1, Field1 " _
     & "FROM Table1, Table1, Table1 " _
     & "WHERE Field1 = '" & <VALUE> & "'"

or

strSQL = "SELECT Field1, Field1, Field1, Field1 "
strSQL = strSQL & "FROM Table1, Table1, Table1 "
strSQL = strSQL & "WHERE Field1 = '" & <VALUE> & "'"

good luck

lucho_nd
ASKER CERTIFIED SOLUTION
Avatar of cero
cero

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mercia

ASKER

Thanks for help works, i thought there might be a shorter way of doing it, but has help me overcome problem.  Cheers