Results 1 to 9 of 9
  1. #1
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776

    Multi Select List Box only filters on one selection at a time?

    Database3.zip

    Hi all,
    I attached a sample db for reference.



    I am trying to filter my list boxes with selections in them but it will only populate with one selection.
    Is there a way if I select more then one then the next gets the value of all I selected from the other?

    Basically What I select in Lst1 I want it to filter Lst2 & Lst3 and what I select in Lst2 to filter Lst3
    Depending on how many i select in each listbox? If that makes any sense.

    Thanks
    Dave

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    You likely need to use the IN() statement if more than one value.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Check this out: http://allenbrowne.com/ser-50.html
    Basically you just want to make sure multi select is enabled for the listbox then when you run your code you will loop through all the items in the listbox and build a string with values to filter by

  4. #4
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    kd2017
    I use that for one of my reports all ready but not really sure how that would work with cascading list boxes?

  5. #5
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi all,
    welshgasman,
    I am totally unfamiliar with the IN() Statement, will need to ready up on that one.
    I use the allen brown example but the key work is "how to use a mutli select" A being the key word!
    I am trying to cascade three multiselect listboxes
    Thanks

  6. #6
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    As pointed out, you'll need to use an "In" clause.

    such as (in red)
    Code:
    Dim sCategorySub As String
    
    
        sCategorySub = "SELECT [tblCategorySub].[SubCategoryID]," & _
                       " [tblCategorySub].[CategoryID]," & _
                       " [tblCategorySub].[SubCategoryName] " & _
                         "FROM tblCategorySub " & _
                         "WHERE [CategoryID] In(" & getLBX(Me.Lst1) & ")"
        Me.Lst2.RowSource = sCategorySub
    I use a public function with Multi-select listboxes which returns a delimited string of your selections.

    Code:
    Public Function getLBX(lbx As ListBox, Optional intColumn As Variant = 0, Optional Seperator As String = ",", _
                           Optional delim As Variant = Null) As String
    
    
    'Iterates thru the multiselect listbox and constructs an array of the selected items
    'Arguments:
    'Lbx is Listbox Object ie.Me.MyListbox
    'intColumn is the column # to be returned
    'Seperator is the character seperating items in array returned
    'Delim is optional delimiter to be return in array ie. #1/1/2001#,#12/25/2015#
    
    
        Dim strlist As String
        Dim varSelected As Variant
    
    
        'On Error GoTo getLBX_Error
    
    
        If lbx.ItemsSelected.Count = 0 Then
            'MsgBox "Nothing selected"
        Else
    
    
            For Each varSelected In lbx.ItemsSelected
    
    
                If Nz(lbx.Column(intColumn, (varSelected)), "") <> "" Then
    
    
                    strlist = strlist & delim & lbx.Column(intColumn, (varSelected)) & delim & Seperator
    
    
                Else
    
    
                    strlist = strlist
    
    
                End If
    
    
            Next varSelected
    
    
            If Nz(strlist, "") <> "" Then
    
    
                strlist = Left$(strlist, Len(strlist) - 1)  'remove trailing comma
    
    
            End If
        End If
    
    
        getLBX = strlist
    
    
        On Error GoTo 0
        Exit Function
    
    
    getLBX_Error:
    
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure getLBX of Module modLBX"
    
    
    End Function
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  7. #7
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Thanks Moke123,
    I put the code in my forms as suggested and put the function in a module. I am getting an error
    "A Module Is Not a Valid Type"
    Not sure what I need to do?
    Thanks
    Dave

  8. #8
    d9pierce1 is offline Expert
    Windows 10 Access 2019
    Join Date
    Jan 2012
    Location
    Oklahoma
    Posts
    776
    Hi Moke123
    Thank you,
    Didn't have the correct name in Module. Sorry for confusion!
    Works like a charm!
    Thank you very much!
    Dave

  9. #9
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    Good luck with your project.
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 3
    Last Post: 08-24-2020, 05:09 AM
  2. Replies: 10
    Last Post: 10-26-2019, 12:15 PM
  3. Replies: 1
    Last Post: 10-20-2017, 11:38 AM
  4. Replies: 2
    Last Post: 06-30-2015, 06:20 PM
  5. Replies: 8
    Last Post: 12-22-2010, 08:12 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