Results 1 to 4 of 4
  1. #1
    Breezer23 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    10

    Two Multi Selection List Boxes to Query Results

    Afternoon,
    I've been browsing through numerous forums and have finally been able to figure out how to query results based on a list box. However, I need to query results off selections in two list boxes. I cannot figure out how to merge the two into the same query. The 2nd list box results always override the first listbox - meaning the 1st ListBox is more or less pointless.

    I took the code from the MS Support Site:

    Code:
    Private Sub Command4_Click()
    Dim Q As QueryDef, DB As Database
       Dim Criteria As String
       Dim ctl As Control
       Dim Itm As Variant
    
    
       ' Build a list of the selections.
       Set ctl = Me!
    [List0]
    
    
       For Each Itm In ctl.ItemsSelected
          If Len(Criteria) = 0 Then
             Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34)
          Else
             Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _
              & Chr(34)
          End If
       Next Itm
    
    
       If Len(Criteria) = 0 Then
          Itm = MsgBox("You must select one or more items in the" & _
            " list box!", 0, "No Selection Made")
          Exit Sub
       End If
    
    
       ' Modify the Query.
       Set DB = CurrentDb()
       Set Q = DB.QueryDefs("qry_IncType")
       Q.SQL = "Select * From tbl_FinalCapture Where [Incident Type] In(" & Criteria & _
         ");"
       
       ' Run the query.
       DoCmd.OpenQuery "qry_IncType"
    
    
    End Sub
    However, I need to include
    [List2] selections into this query.

    Any help would be appreciated. I'm a bit of a VBA noob.
    Regards,


    Travis

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Simplest for you might be to create a second criteria variable, like Criteria2. Build it in the same way, then

    Q.SQL = "Select * From tbl_FinalCapture Where [Incident Type] In(" & Criteria & _
    ") AND OtherField In(" & Criteria2 & ")"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Breezer23 is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2013
    Posts
    10
    Quote Originally Posted by pbaldy View Post
    Simplest for you might be to create a second criteria variable, like Criteria2. Build it in the same way, then

    Q.SQL = "Select * From tbl_FinalCapture Where [Incident Type] In(" & Criteria & _
    ") AND OtherField In(" & Criteria2 & ")"
    You are amazing! Thank you very much!
    Regards,
    Travis

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 05-29-2014, 09:58 PM
  2. Replies: 15
    Last Post: 11-20-2013, 04:30 PM
  3. Replies: 2
    Last Post: 06-09-2012, 07:59 AM
  4. Multi-selection from list box to reports
    By SgtSaunders69 in forum Forms
    Replies: 3
    Last Post: 03-02-2012, 09:56 AM
  5. Replies: 1
    Last Post: 03-09-2006, 12:12 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