Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Miked1978 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2019
    Posts
    38

    VBA SQL statement help

    I have a form where a user can select multiple parameters and the form will return the dataset in a subform. It currently works IF the users selects ALL of the parameters listed but I need to make it work where the user can only select 1 or 2 parameters. Each parameter is in a List drop down box.



    So for instance on the form the user may select one or two hulls and will want to return all records for those hulls. However the way I have it written they also must select a "WS', "LeadDept", and "Phase" for the form to return anything.

    I'm pretty sure it has to do with my SQL statement not knowing what to do if the user doesn't select anything.


    strCriteriaHull = Right(strCriteriaHull, Len(strCriteriaHull) - 1)
    strCriteriaWS = Right(strCriteriaWS, Len(strCriteriaWS) - 1)
    strCriteriaLeadDept = Right(strCriteriaLeadDept, Len(strCriteriaLeadDept) - 1)
    strCriteriaPhase = Right(strCriteriaPhase, Len(strCriteriaPhase) - 1)

    strSQL = " SELECT * " & _
    " FROM dbo_tblPrintCenter_SGI " & _
    " WHERE dbo_tblPrintCenter_SGI.hull IN (" & strCriteriaHull & ")" & _
    " AND dbo_tblPrintCenter_SGI.WS IN (" & strCriteriaWS & ")" & _
    " AND dbo_tblPrintCenter_SGI.LeadDept IN (" & strCriteriaLeadDept & ")" & _
    " AND dbo_tblPrintCenter_SGI.CalcSS BETWEEN '" & txtCalcSSBegin & "' AND '" & txtCalcSSEnd & "'" & _
    " AND dbo_tblPrintCenter_SGI.SSPhase IN (" & strCriteriaPhase & ")"


    Any help will be greatly appreciated.

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,418
    first thing you will get an error here

    strCriteriaHull = Right(strCriteriaHull, Len(strCriteriaHull) - 1)

    if strCriteriaHull is null or has a length of 1

    same for your other assignments

    user may select one or two hulls
    what do these strings actually look like? and how does the user select them?

    might have an impact on the solution

    but to answer your question you need to build your criteria separately using if starements perhaps something like

    Code:
    strSQL = " SELECT * FROM dbo_tblPrintCenter_SGI"
    strWHERE=""
    if nz(strCriteriaHull,"")<>"" then strWHERE=" AND dbo_tblPrintCenter_SGI.hull IN (" & strCriteriaHull & ")" 'AND included to simplify the code
    if nz(strCriteriaWS,"")<>"" then  strWHERE=" AND dbo_tblPrintCenter_SGI.WS IN (" & strCriteriaWS & ")" & strWHERE
    etc.
    
    if strWHERE<>"" then strSQL=strSQL & " WHERE" & mid(strWHERE,5)

  3. #3
    Miked1978 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2019
    Posts
    38
    Here is the beginning of my code. I pieced all of this together from googling different issues as they occurred. I'm sure its not the best way to write VBA.

    Private Sub Cmd_RunQuery_Click()
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim varItem As Variant
    Dim strCriteriaHull As String
    Dim strCriteriaWS As String
    Dim strCriteriaLeadDept As String
    Dim strCriteriaPhase As String
    Dim CalcSSBegin As Date
    Dim CalcSSEnf As Date
    Dim strSQL As String
    Dim qryDef As QueryDef





    Set db = CurrentDb()
    Set qdf = db.QueryDefs("qryform")


    For Each varItem In Me!cmboHull.ItemsSelected
    strCriteriaHull = strCriteriaHull & ",'" & Me!cmboHull.ItemData(varItem) & "'"
    Next varItem



    For Each varItem In Me!cmboWS.ItemsSelected
    strCriteriaWS = strCriteriaWS & ",'" & Me!cmboWS.ItemData(varItem) & "'"
    Next varItem


    For Each varItem In Me!cmboLeadDept.ItemsSelected
    strCriteriaLeadDept = strCriteriaLeadDept & ",'" & Me!cmboLeadDept.ItemData(varItem) & "'"
    Next varItem


    For Each varItem In Me!cmboPhase.ItemsSelected
    strCriteriaPhase = strCriteriaPhase & ",'" & Me!cmboPhase.ItemData(varItem) & "'"
    Next varItem

  4. #4
    Miked1978 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2019
    Posts
    38
    What should this be? strCriteriaHull = Right(strCriteriaHull, Len(strCriteriaHull) - 1)

    I used your SQL statement and it works if I have all the criteria selected.

  5. #5
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Code:
    Private Sub Cmd_RunQuery_Click()
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim varItem As Variant
    Dim strCriteriaHull As String
    Dim strCriteriaWS As String
    Dim strCriteriaLeadDept As String
    Dim strCriteriaPhase As String
    Dim CalcSSBegin As Date
    Dim CalcSSEnf As Date
    Dim strSQL As String
    Dim qryDef As QueryDef
    
    Set db = CurrentDb()
    Set qdf = db.QueryDefs("qryform")
    
    For Each varItem In Me!cmboHull.ItemsSelected
       strCriteriaHull = strCriteriaHull & ",'" & Me!cmboHull.ItemData(varItem) & "'"
    Next varItem
    
    For Each varItem In Me!cmboWS.ItemsSelected
       strCriteriaWS = strCriteriaWS & ",'" & Me!cmboWS.ItemData(varItem) & "'"
    Next varItem
    
    For Each varItem In Me!cmboLeadDept.ItemsSelected
       strCriteriaLeadDept = strCriteriaLeadDept & ",'" & Me!cmboLeadDept.ItemData(varItem) & "'"
    Next varItem
    
    For Each varItem In Me!cmboPhase.ItemsSelected
       strCriteriaPhase = strCriteriaPhase & ",'" & Me!cmboPhase.ItemData(varItem) & "'"
    Next varItem
    
    'you don't need these
    'strCriteriaHull = Right(strCriteriaHull, Len(strCriteriaHull) - 1)
    'strCriteriaWS = Right(strCriteriaWS, Len(strCriteriaWS) - 1)
    'strCriteriaLeadDept = Right(strCriteriaLeadDept, Len(strCriteriaLeadDept) - 1)
    'strCriteriaPhase = Right(strCriteriaPhase, Len(strCriteriaPhase) - 1)
    
    strSQL = " SELECT * FROM dbo_tblPrintCenter_SGI WHERE " 
    if len(strcriteriahull) > 0 then strSQL = strSQL & "dbo_tblPrintCenter_SGI.hull IN (""" & strCriteriaHull & """) AND " 
    if len(strcriteriaws) > 0 then strSQL = strSQL & "dbo_tblPrintCenter_SGI.WS IN (""" & strCriteriaWS & """)" AND "
    if len(strcriterialeaddept) > 0 then strSQL = strSQL & "dbo_tblPrintCenter_SGI.LeadDept IN (""" & strCriteriaLeadDept & """)" AND "
    if len(strcriteriaphase) > 0 then strSQL = strSQL & "dbo_tblPrintCenter_SGI.SSPhase IN (""" & strCriteriaPhase & """) AND "
    strSQL = strSQL & "dbo_tblPrintCenter_SGI.CalcSS BETWEEN #" & txtCalcSSBegin & "# AND #" & txtCalcSSEnd & "#"
    
    You have to encapsulate your IN string in quotes
    this also assumes txtcalcssbegin and txtcalcssend are dates if they're text values you can go back to your ' marks
    you can also use a debug.print strsql before you try to do anythign with it to see where your errors are by pasting the SQL code into a query window and seeing what error it throws

  6. #6
    Miked1978 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2019
    Posts
    38
    I'm getting a Run-Time error '13' Type Mismatch

    if len(strcriteriaws) > 0 then strSQL = strSQL & "dbo_tblPrintCenter_SGI.WS IN (""" & strCriteriaWS & """)" AND "

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I don't see the need for all the quotes. Try

    if len(strcriteriaws) > 0 then strSQL = strSQL & "dbo_tblPrintCenter_SGI.WS IN (" & strCriteriaWS & ") AND "
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Miked1978 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2019
    Posts
    38
    That got rid of my error but now nothing is happening when the query runs. When I press the command button on my form the mouse spins like the query is running but i'm not getting any results. Just to be clear I have a subform underneath my mainform that displays the results of the query. The results don't change when I run the query despite selecting different criteria. Below is the code I have thus far.


    Private Sub Cmd_RunQuery_Click()
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim varItem As Variant
    Dim strCriteriaHull As String
    Dim strCriteriaWS As String
    Dim strCriteriaLeadDept As String
    Dim strCriteriaPhase As String
    Dim CalcSSBegin As Date
    Dim CalcSSEnf As Date
    Dim strSQL As String
    Dim qryDef As QueryDef


    Set db = CurrentDb()
    Set qdf = db.QueryDefs("qryform")


    For Each varItem In Me!cmboHull.ItemsSelected
    strCriteriaHull = strCriteriaHull & ",'" & Me!cmboHull.ItemData(varItem) & "'"
    Next varItem


    For Each varItem In Me!cmboWS.ItemsSelected
    strCriteriaWS = strCriteriaWS & ",'" & Me!cmboWS.ItemData(varItem) & "'"
    Next varItem


    For Each varItem In Me!cmboLeadDept.ItemsSelected
    strCriteriaLeadDept = strCriteriaLeadDept & ",'" & Me!cmboLeadDept.ItemData(varItem) & "'"
    Next varItem


    For Each varItem In Me!cmboPhase.ItemsSelected
    strCriteriaPhase = strCriteriaPhase & ",'" & Me!cmboPhase.ItemData(varItem) & "'"
    Next varItem



    strSQL = " SELECT * FROM dbo_tblPrintCenter_SGI WHERE "
    If Len(strCriteriaHull) > 0 Then strSQL = strSQL & "dbo_tblPrintCenter_SGI.hull IN (" & strCriteriaHull & ") AND "
    If Len(strCriteriaWS) > 0 Then strSQL = strSQL & "dbo_tblPrintCenter_SGI.WS IN (" & strCriteriaWS & ") AND "
    If Len(strCriteriaLeadDept) > 0 Then strSQL = strSQL & "dbo_tblPrintCenter_SGI.LeadDept IN (" & strCriteriaLeadDept & ") And "
    If Len(strCriteriaPhase) > 0 Then strSQL = strSQL & "dbo_tblPrintCenter_SGI.SSPhase IN (" & strCriteriaPhase & ") AND "
    strSQL = strSQL & "dbo_tblPrintCenter_SGI.CalcSS BETWEEN #" & txtCalcSSBegin & "# AND #" & txtCalcSSEnd & "#"

    Me!qryformsubform.Form.RecordSource = "qryform"

    End Sub

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I think you have to encapsulate the IN part in quotes if you're testing strings against one another don't you?

    Code:
    if len(strcriteriaws) > 0 then strSQL = strSQL & "dbo_tblPrintCenter_SGI.WS IN (""" & strCriteriaWS & """) AND "
    


    Try this instead, I had an extra set of quotes in the line after the closing ) on the IN statement, the other lines will have to be corrected as well assuming removing them entirely does not work.

  10. #10
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    also, debug.print your SQL statement and paste it into a query window to see where it bombs out, then address that part in your SQL building statement

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You've set a SQL string to some SQL but not put it anywhere. Either use the QueryDef you've got to set its SQL property to your SQL, or just set the subform's source to the SQL instead of the query.

    Here's a visual on what rpeare is suggesting:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by rpeare View Post
    I think you have to encapsulate the IN part in quotes if you're testing strings against one another don't you?
    Unless I'm misunderstanding, no. The variable strCriteriaWS contains the single quotes around each value.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    Miked1978 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2019
    Posts
    38
    I really appreciate yall helping me out.

    I did the Debug.Print and might have spotted the problem, just not sure how to fix it even though i'm no longer getting any errors.

    If you look at the IN clause it has a comma in front of each
    SELECT * FROM dbo_tblPrintCenter_SGI WHERE dbo_tblPrintCenter_SGI.hull IN (,'2618') <--- shouldn't be a comma there???
    AND dbo_tblPrintCenter_SGI.SSPhase IN (",'001'")
    <--- shouldn't be a quotation and comma there???
    AND dbo_tblPrintCenter_SGI.CalcSS BETWEEN 1/1/2016 AND 7/10/2019

  14. #14
    Miked1978 is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2019
    Posts
    38
    Quote Originally Posted by pbaldy View Post
    You've set a SQL string to some SQL but not put it anywhere. Either use the QueryDef you've got to set its SQL property to your SQL, or just set the subform's source to the SQL instead of the query.

    Here's a visual on what rpeare is suggesting:

    http://www.baldyweb.com/ImmediateWindow.htm
    Since I'm not sure how to make the subform read the SQL, how would I use the QueryDef?

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    That's actually easier:

    Me!qryformsubform.Form.RecordSource = strSQL

    You wouldn't need the QueryDef at all. To use the QueryDef:

    qdf.SQL = strSQL


    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. If statement
    By UT227 in forum Programming
    Replies: 9
    Last Post: 03-19-2018, 11:06 AM
  2. IF Then vba statement
    By GCLIFTON in forum Queries
    Replies: 3
    Last Post: 05-23-2016, 11:01 AM
  3. Replies: 11
    Last Post: 04-29-2015, 01:38 PM
  4. if statement in sql statement, query builder
    By 54.69.6d.20 in forum Access
    Replies: 4
    Last Post: 09-11-2012, 07:38 AM
  5. Replies: 7
    Last Post: 08-17-2011, 01:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums