Filter listbox based on combo selection

inkbird1

Board Regular
Joined
Apr 21, 2020
Messages
51
Hi, I want to have a combo box which the user can filter down the available options in a multiselect list box.
I have tried to use the below code but doesn't work

VBA Code:
Private Sub cbo_filterProvider_Click()

    Dim strRS As String
 
    ' Filter the list box appropriately based on the combo box selection(s)
    If Me.cbo_filterProvider.ListIndex >= 0 Then strRS = strRS & " AND ProviderID = " & Me.cbo_filterProvider
 
    'ADD FILTER TO QUERY
    strRS = "SELECT qry_studentsExtended.StudentID, qry_studentsExtended.StudentName FROM qry_studentsextended " & strRS
 
    'ADD ORDER BY CLAUSE
    strRS = strRS & " ORDER BY qry_studentsExtended.Last Name;"
 
    Me.lst_Students.RowSource = strRS
    Me.lst_Students.Requery
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Far easier to alter the listbox's .RowSource to reference the combobox value. Access will handle the events for you. Something like:

SQL:
  select * from qry_studentsextended where ProviderID = forms!theFormName!cbo_filterProvider

If you need to handle the "I don't want a filter condition" you can add an "ALL" choice to the combobox values and check for that. Assuming ALL's value is -1:

SQL:
  select * from qry_studentsextended where ProviderID = forms!theFormName!cbo_filterProvider or forms!theFormName!cbo_filterProvider = -1

No code necessary.

-- Craig
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top