ms access filter subform based on multiple checkboxes

Attached is my sample DB. The checkboxes are in frm_Dependent_Search in Search Tab.
 

Attachments

The problem is I believe you want to filter your subform not the main form. Change the code to
Code:
End With  ' for the me part
With Me.Sub_frm_Search.Form
      .Filter = vFilter
      .FilterOn = True
      Debug.Print vFilter
 End With
However the filter does not make any sense. I assume you want an OR and you do need a Not.
You cannot be Like 01* AND Like 05*.
 
The subform table is not filtering right. The checkboxes are only filtering one at a time.
 
I simplified the code. I just added the search to the tag property of the check like
'01*'
Code:
Private Function FilterSub()
  'MajP
   Dim sFilter As String
   Dim ctrl As Access.Control
   For Each ctrl In Me.Controls
     If ctrl.ControlType = acCheckBox And ctrl.Tag <> "" Then
       If ctrl.Value = True Then
         If sFilter = "" Then
           sFilter = "CRN like " & ctrl.Tag
         Else
           sFilter = sFilter & " OR CRN Like " & ctrl.Tag
        End If
       End If
     End If
    Next ctrl
     With Me.Sub_frm_Search.Form
      .Filter = sFilter
      .FilterOn = True
      Debug.Print sFilter
     End With
End Function
I got rid of the event handlers and made a single function to handle the events.
 

Attachments

Great. One more thing. Can you do if all is checked show all records(Select All), if none are checked dont display anything (De-Select All).
 
Code:
Private Sub Form_Load()
'add this
FilterSub
End Sub

Private Function FilterSub()
  'MajP
   Dim sFilter As String
   Dim ctrl As Access.Control
   For Each ctrl In Me.Controls
     If ctrl.ControlType = acCheckBox And ctrl.Tag <> "" Then
       If ctrl.Value = True Then
         If sFilter = "" Then
           sFilter = "CRN like " & ctrl.Tag
         Else
           sFilter = sFilter & " OR CRN Like " & ctrl.Tag
        End If
       End If
     End If
    Next ctrl
   'If nothing selected then make a bogus filter
    If sFilter = "" Then sFilter = "TRUE = FALSE"
    With Me.Sub_frm_Search.Form
      .Filter = sFilter
      .FilterOn = True
      Debug.Print sFilter
     End With
End Function
Add the load event, and add a bogus filter when no checks selected.
 
What will be the code I will put at Select All and De-Select All buttons?
 
Loop your checkboxes again like in the filter.
Code:
Private Sub cmdSelectAll_Click()
   Dim ctrl As Access.Control
   For Each ctrl In Me.Controls
     If ctrl.ControlType = acCheckBox And ctrl.Tag <> "" Then
       ctrl = True
     End If
  Next ctrl
  FilterSub
End Sub
Private Sub cmdDeSelectAll_Click()
    Dim ctrl As Access.Control
   For Each ctrl In Me.Controls
     If ctrl.ControlType = acCheckBox And ctrl.Tag <> "" Then
       ctrl = False
     End If
  Next ctrl
  FilterSub
 
how to create a report (print preview) based on the filtered subform?
 
Modified code
Code:
Private Sub Form_Load()
 FilterSub
End Sub

Private Function FilterSub()
  'MajP
   Dim sFilter As String
   sFilter = GetFilter
   With Me.Sub_frm_Search.Form
      .Filter = sFilter
      .FilterOn = True
      Debug.Print sFilter
     End With
End Function
Private Function GetFilter() As String
   Dim sFilter As String
   Dim ctrl As Access.Control
   For Each ctrl In Me.Controls
     If ctrl.ControlType = acCheckBox And ctrl.Tag <> "" Then
       If ctrl.Value = True Then
         If sFilter = "" Then
           sFilter = "CRN like " & ctrl.Tag
         Else
           sFilter = sFilter & " OR CRN Like " & ctrl.Tag
        End If
       End If
     End If
    Next ctrl
    'If nothing selected then make a bogus filter
     If sFilter = "" Then sFilter = "TRUE = FALSE"
    GetFilter = sFilter
End Function
Private Sub cmdPreviewReportcbo_Click()
  DoCmd.OpenReport "rpt_All_municipalities", acViewPreview, , GetFilter()
End Sub
 
No. For both. Just modified the code to reduce duplication.
 
Re the Code HERE:-
Loop your checkboxes again like in the filter.

If you look at the Nifty Tips page on my website:- https://www.niftyaccess.com/nifty-tips/ there's a tip near the bottom titled "Command Buttons - Convert Two in to One" you could replace the two command buttons "cmdSelectAll" & "cmdDeSelectAll" with one button YouTube:-

Lock/Unlock Controls 4 - Nifty Access

 
MajP so I'll just replace the code with the modified code except the select and de-select?
 
MajP so I'll just replace all the code for checkbox with the modified code except the select and de-select?
 
MajP can you recommend any reading materials/ebook about access and vba?
 
There are a couple of threads on this site for good resources on vba. I am a book person, others like videos. For software I usually by older used books because you can get a 100 dollar book for 5 bucks. The Access Inside Out books are a good start and very thorough. As long as it is after 2007 version you should be fine. Not much has changed especially on basics. I see a 2013 version for 8.00. If you get through that the Bible is
Litwin and Getz Access Developers Handbook (2002). This goes into advanced concepts. Search here for threads on other recources.
 
MajP,
I copied all the code from your sample database to my final database but nothing is working? Am i missing something? Please Help.
 

Users who are viewing this thread

Back
Top Bottom