MajP
You've got your good things, and you've got mine.
- Local time
- Today, 11:10
- Joined
- May 21, 2018
- Messages
- 8,904
NOTE: See updated code below. I recommend using the updated class module that @arnelgp modified. It further simplifies building a search form and addresses some bugs.
I figured this was worth reposting, because i see so many people struggling with wanting to filter a form. I have seen some painful attempts. I doubt without writing pages and pages of code you can replicate the following to filter a continuous form. The date filter alone would be 1000s of lines and multiple forms. Here is my form with every field having a complex multiple select option filter.
When choosing a text field the filters available
When selecting a date field the choice are limitless
I added a calculated year field
So how much code to build this form with all this functionality. According to Word 40 lines of code, but only 118 words.
The Most Powerful form Filter and Requires almost No Code
NOTE: See updated code below. I recommend using the updated class module that @arnelgp modified. It further simplifies building a search form and addresses some bugs...
www.access-programmers.co.uk
I figured this was worth reposting, because i see so many people struggling with wanting to filter a form. I have seen some painful attempts. I doubt without writing pages and pages of code you can replicate the following to filter a continuous form. The date filter alone would be 1000s of lines and multiple forms. Here is my form with every field having a complex multiple select option filter.
When choosing a text field the filters available
When selecting a date field the choice are limitless
I added a calculated year field
So how much code to build this form with all this functionality. According to Word 40 lines of code, but only 118 words.
Code:
Option Compare Database
Option Explicit
Private SelectedButton As Variant
Public Function FilterForm()
On Error GoTo errlbl
Set SelectedButton = ActiveControl
Me.Controls(ActiveControl.Tag).SetFocus
Me.Recordset.MoveFirst
DoCmd.RunCommand acCmdFilterMenu
Exit Function
errlbl:
If Err.Number = 3021 Then
MsgBox "No records Returned", vbInformation
Me.Filter = ""
Me.FilterOn = False
SetButtons
Else
MsgBox Err.Number & " " & Err.Description
End If
End Function
Private Sub Form_ApplyFilter(Cancel As Integer, ApplyType As Integer)
SetButtons
End Sub
Private Sub Form_Load()
DoCmd.Maximize
End Sub
Private Sub SetButtons()
Dim cmd As Access.Control
For Each cmd In Me.Controls
If cmd.ControlType = acCommandButton Then
If cmd.Tag <> "" Then
If InStr(Me.Filter, cmd.Tag) > 0 Then
cmd.Picture = "Filter"
Else
cmd.Picture = "Down"
End If
End If
End If
Next cmd
End Sub
Attachments
Last edited: