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
Solved

How do I structure long SQL statements in VBA?

Posted on 2007-11-25
4
938 Views
Last Modified: 2013-11-27
Hi experts,

I am trying to structure my SQL statement in VBA better than having it all in one line.  My main reason for wanting to do this, apart from it being easier to read, is that I have some SQL statements that appear to be too long for one line in VBA.

The code I am using, although not my longest is as follows:

Dim SQL as String

SQL = "SELECT tblPlan.ID, tblPlan.Criticality, tblPlan.Plan_Prod_Serv_Name, tblPlan.LRef, tblPlan.LKey, tblPlan.Business_Unit_ID" & _
        "FROM tblPlan" & _
        "WHERE (((tblPlan.LRef) = [Forms]![switchboard]![LRef]) And ((tblPlan.LKey) = [Forms]![switchboard]![LKey]) And ((tblPlan.Business_Unit_ID) = IIf([Forms]![switchboard]![SwitchGroup].[Form]![UnitID] = 1, [Forms]![switchboard]![SwitchGroup].[Form]![UnitID], [Forms]![switchboard]![SwitchGroup].[Form]![UnitID])))" & _
        "ORDER BY tblPlan.Criticality DESC , tblPlan.Plan_Prod_Serv_Name;"

The above SQL needs to go into the rowsource of my listbox on the form.  The listbox is called lstPlans and the code to get the SQL into lstPlans is as follows:

Me.lstPlans.RowSource = SQL

This is not working and I do not know why.  I have no errors coming up and I am separating each line of code with "& _".  Please help as I must be doing something wrong?

I create my SQL statements using the Query Builder, so if anybody has a better solution as opposed to using an SQL statement in my VBA code I would be grateful to read them.

Thank you.

Terry
0
Comment
Question by:TerenceHewett
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 400 total points
ID: 20346555
Hello TerenceHewett,

SQL = "SELECT ID, Criticality, Plan_Prod_Serv_Name, LRef, LKey" & _
    "Business_Unit_ID FROM tblPlan " & _
    "WHERE LRef = '" & [Forms]![switchboard]![LRef]) & "' And LKey = '" & _
    [Forms]![switchboard]![LKey] & "' And Business_Unit_ID = IIf(" & _
    ([Forms]![switchboard]![SwitchGroup].[Form]![UnitID] = 1) & ", '" & _
    [Forms]![switchboard]![SwitchGroup].[Form]![UnitID] "', '" & _
    [Forms]![switchboard]![SwitchGroup].[Form]![UnitID]"') " & _
    "ORDER BY Criticality DESC , Plan_Prod_Serv_Name;"


Regards,

Patrick
0
 

Author Comment

by:TerenceHewett
ID: 20346613
Thanks Patrick for your help.  What is the main difference from what I had and why will your statement work? I would like to try and understand what I am doing wrong as opposed to just using your code which I am sure will work.

Thank you again.
Terry
0
 
LVL 10

Assisted Solution

by:LennyGray
LennyGray earned 100 total points
ID: 20346675
Patrick's code is correct but I prefer breaking the code down into readable portions and simply concatenate the sqlstatement like below:

   
 sqlString = "SELECT tblDT_ComponentYear.iComponentYearID, tblDT_ComponentYear.sDocumentName "
    sqlString = sqlString & "FROM tblDT_ComponentYear "
    sqlString = sqlString & "WHERE (((tblDT_ComponentYear.sDocumentName) Is Not Null and (tblDT_ComponentYear.sDocumentName)<>"""") AND ((tblDT_ComponentYear.iAwardYear)= " & iAwardYear & "))"
 
    Set dbs = CurrentDb
    Set rs = dbs.OpenRecordset(sqlString, dbOpenDynaset)

Open in new window

0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 400 total points
ID: 20346713
Terry,

Excellent questions, and sorry I did not spend more time on the explanation, which in any event is
more valuable than the answer :)

As I see it, the main differences between my code and yours are:

1) You did not allow for spaces between the clauses in your SQL statement, such as between the
last field in the SELECT clause and the beginning of the FROM clause.  This will make your code
fail.

2) I treated the "query by form" values differently by embedding the actual values into the SQL
string.  The way you did it would be just fine in a regular saved query, but you cannot use that
syntax when you build the SQL string dynamically in VBA.

I also stripped out the table name qualifier on the field instances, but that's small beer.

Regards,

Patrick
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

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.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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