Solved

How do I structure long SQL statements in VBA?

Posted on 2007-11-25
4
931 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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…

743 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

20 Experts available now in Live!

Get 1:1 Help Now