Results 1 to 5 of 5
  1. #1
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370

    Question Listbox filters

    Hello all,



    I'm wondering if there is a more efficient way of doing the process I'm doing.

    As of now, I have 2 list boxes filtering two fields on my form. They can work independently or together to narrow down even further.

    I'm hoping to add a third list box to filter another field, however it seems like I would have to create several more if statements if I continue down this route.


    Code:
    Private Sub Command2_Click()
    
       Dim Criteria As String
       Dim Criteria2 As String
       Dim i As Variant
    
    
       ' Build criteria string from selected items in list box.
       Criteria = ""
       Criteria2 = ""
       For Each i In Me!
    [List1].ItemsSelected
          If Criteria <> "" Then
             Criteria = Criteria & " OR "
          End If
          Criteria = Criteria & "[PlanStatusID]=" _
           & Me!
    [List1].ItemData(i)
       Next i
       
       For Each i In Me!
    [List0].ItemsSelected
          If Criteria2 <> "" Then
             Criteria2 = Criteria2 & " OR "
          End If
          Criteria2 = Criteria2 & "[PlanNameID]=" _
           & Me!
    [List0].ItemData(i)
       Next i
       ' Filter the form using selected items in the list box.
       
       If Criteria = "" Then
            Me.Filter = Criteria2
            Me.FilterOn = True
       End If
       If Criteria2 = "" Then
            Me.Filter = Criteria
            Me.FilterOn = True
       End If
       If Criteria <> "" And Criteria2 <> "" Then
            Me.Filter = Criteria & "AND" & Criteria2
            Me.FilterOn = True
       End If
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Thanks June, I'll give it a look. It appears like its only for using one list box, but I'll dive in and get back at you if I need any help with adding on another list box.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Use as many controls as you want, including multiple listboxes. Just means more conditional and concatenation code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I've done this by looping through the items selected for each list and adding them to a comma separated array to build a WHERE clause. Naturally, you need to test for the listbox itemsselected property not being null (or "", I forget) before running an event such as a button click or listbox double click and decide how to handle that. Consider passing the itemsselected to a function that returns the array so you don't have to repeat the code for every listbox. After the last listbox is evaluated, add the semicolon to the clause and append that to the sql string. Then run the sql.

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

Similar Threads

  1. Replies: 1
    Last Post: 01-31-2015, 09:03 PM
  2. Replies: 2
    Last Post: 03-23-2014, 06:50 AM
  3. Replies: 3
    Last Post: 12-13-2012, 04:40 AM
  4. Replies: 1
    Last Post: 09-10-2012, 11:21 PM
  5. Replies: 1
    Last Post: 07-26-2012, 11:45 AM

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