?
Solved

Spliting SQL?

Posted on 2003-03-07
11
Medium Priority
?
163 Views
Last Modified: 2010-05-01
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?
0
Comment
Question by:mercia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 8087434
>>Select("Field1, field2, field3, field4
From data
where field1 = "test")
??

Should be like this?

"Select Field1, field2, field3, field4 From data where field1 = 'test'"
0
 

Author Comment

by:mercia
ID: 8087459
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.
0
 
LVL 7

Expert Comment

by:Smallint
ID: 8087496
try

strSql = "Select(Field1, field2, field3, field4 " + _
         "From Data " + _
         "where field1 = 'test')"
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:mercia
ID: 8087523
Still get errors if I try this
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 8087525
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
0
 

Expert Comment

by:diya123
ID: 8087529
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...



0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 8088023
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
0
 
LVL 7

Expert Comment

by:Smallint
ID: 8088185
try


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

Be carefully to left one space between "+" and "_"
0
 
LVL 1

Expert Comment

by:lucho_nd
ID: 8088522
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
0
 
LVL 2

Accepted Solution

by:
cero earned 300 total points
ID: 8088573
Hi Mercia,

If you use concatenation like

Sql = "Select Field1, Field2, " & _
      " Field3 From Table1, Table2 " 

And this sentence is very large, like you mention, you will obtain errors, so, the best way to achieve this is:

var = "Test"

Sql = "Select Field1, Field2, Field3, Field4 "
Sql = Sql & " From Data "
Sql = Sql & " Where Field1 = '" & var & "'"

'be careful of spaces when concatening

hope this helps,

cero
0
 

Author Comment

by:mercia
ID: 8088793
Thanks for help works, i thought there might be a shorter way of doing it, but has help me overcome problem.  Cheers
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question