Single Form
To achieve the desire in the title it is required a table, a form and a little VBA code. The form will contain, in addition to the table fields, a List Box control and a button.
List Box source: SELECT DISTINCT t_test.oras FROM t_test;
For achieving the multi-selection, being in the Design View mode, we go to the Properties panel of the ListBox control, in the Other tab, where we select the Multi Select option to Simple (there is also the Extended option, the difference being the following: while for the Simple option it is enough to click successively to make the selection, for the Extended option it is necessary to press the Control key for non-contiguous selection or Shift for successive ones).
Filter Button Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Private Sub btnList_Click() Dim critList As String Dim i As Variant critList = "" ' Making a criterion for selected items from listbox For Each i In lst_oras.ItemsSelected If critList <> "" Then critList = critList & " OR " End If critList = critList & "[oras]='" & lst_oras.ItemData(i) & "'" Next i ' Filtrarea propriu-zisa Forms!f_test.Filter = critList Forms!f_test.FilterOn = True End Sub |
Form with Subform
As can be seen in the picture at the beginning, the disadvantage of placing a listbox in the same form it looks somewhat unsightly, having unused space. Solving the problem might be if we include another form. Also in that new form (parent) we bring the listbox. The code undergoes a small change, on the last lines, those under the comment “Filtering itself“. In the next picture you can see the result.
1 2 3 |
' Filtering itself Me.f_test_s.Form.Filter = critList Me.f_test_s.Form.FilterOn = True |
Source: Microsoft.com