Solved

How do I structure long SQL statements in VBA?

Posted on 2007-11-25
4
936 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now