I tried the code above and found errors, so I rewrote it. Based on Andy49's code
There are 1 to 9 togglebuttons on the form.
Each togglebutton has a name the begins with 3 letters - "tgl" and then has a number 1 to 9.
Example name: tgl1
There is code in the click event of each togglebutton:
Code:
Private Sub tgl1_Click()
Call SetFilter
End Sub
Private Sub tgl2_Click()
Call SetFilter
End Sub
Private Sub tgl3_Click()
Call SetFilter
End Sub
The code to set/clear the filter
Code:
Private Sub SetFilter()
Const conFilterField As String = "categoryid = "
Dim sFilter As String
Dim myfilter As String
Dim ctl
For Each ctl In Me.Controls
If ctl.ControlType = acToggleButton Then ' is controltype a togglebutton?
If (Left(ctl.Name, 3) = "tgl") And ctl.Value = True Then
myfilter = myfilter & conFilterField & CInt(Right(ctl.Name, 1)) & " or "
End If
End If
Next ctl
If Len(Trim(myfilter)) = 0 Then
Me.Filter = ""
Me.FilterOn = False
Else
myfilter = Left(myfilter, (Len(myfilter) - 4))
Me.Filter = myfilter
Me.FilterOn = True
End If
' Debug.Print Me.Filter
End Sub
Could also add a button (not toggle) that would clear the filter.
Button name is "btnClearFilter".
The button click event would be
Code:
Private Sub btnClearFilter_Click()
Me.Filter = ""
Me.FilterOn = False
Me.tgl1 = False
Me.tgl2 = False
Me.tgl3 = False
End Sub
Had to add a check for control type because some controls do not have a Value property.