VBA MS Access Form Filter Example

This post is going to show you a ms access form filter sample.

The database I created only has one table called “Categories”.

We are going to have a form called “frmCategory”.

ms_access_form_filter_example_create_form

The form is going to be a “Split Form”, because that seems to be quicker than the “main form / sub form” setup.

The “Datasheet on Bottom” setting is set giving it this effect. Having this format, gives us this effect:

ms_access_form_filter_example_after_split_form

These are the settings for the “Split Form”

ms_access_form_filter_example_settings1

ms_access_form_filter_example_settings2

I reordered my fields so I can filter better (here is how it looks):

ms_access_form_filter_example_field_reorder

Here is my form all together:

ms_access_form_filter_example_after_update

…and here is my code:

Option Compare Database

Private m_Where As String

Private Sub btnReset_Click()
    Me.FilterOn = False
    
    'clear all the variables
    m_Where = ""
    Me.cboDescription = ""
    Me.cboIncomeExpense = ""
    Me.chkTax = 0
        
    
End Sub

Private Sub cboDescription_AfterUpdate()
    

    m_Where = m_Where & " AND Description = '" & Me.cboDescription & "'"
    FilterThis
    
End Sub

Private Sub cboIncomeExpense_AfterUpdate()
    
    m_Where = m_Where & " AND [Income/Expense] = '" & Me.cboIncomeExpense & "'"
    FilterThis
End Sub
Private Sub chkTax_AfterUpdate()
    m_Where = m_Where & " AND [Taxable] = " & Me.chkTax
    FilterThis
End Sub

Sub FilterThis()
    
    'clean the string first
    If Left(m_Where, 4) = " AND" Then
        m_Where = Mid(m_Where, 5)
    End If
    
    Me.Filter = m_Where
    Me.FilterOn = True
    
End Sub

The variable “m_Where” is where the filter string is stored, and has modular level scope, so the value persists while the form is open.

Let me know if you have any questions.




By the way, if you got or are getting value from the VBA information, please click the "Donate" button to give me a small token of your appreciation, thanks!


These posts may help answer your question too...

Learn Access VBA: Understand Tables, Queries, Forms, and Reports

Learn Access VBA: From Zero to Database Hero If you’ve ever opened Microsoft Access and wondered how all the pieces fit together — tables, queries, forms, and reports — this tutorial is made for you. In just a few minutes, you’ll understand how Access works behind the scenes and see how VBA (Visual Basic for […]

How To Make An Access Form Time Picker

Here is a relatively easy way to select times for your time entry text boxes. It’s a reusable form that allows you to pick a time from an Access form. There are probably different ways to do this but here is the way I would do it. On the form that has the time fields, […]

How to pick a file to load In VBA

How to Pick a File in VBA: FileDialog & GetOpenFilename Explained When building Excel VBA applications, you’ll often need to let users pick a file to load in VBA. Instead of hard-coding file paths, you can use built-in dialogs that make file selection easy and user-friendly. VBA offers two main approaches: FileDialog object (flexible, customizable) […]

What is the purpose of the Me keyword in Access VBA?

What does the Me keyword mean? “Me” refers to the Access form currently in focus. Instead of writing out the entire form reference, you can just use the keyword “Me” which is easier. Like: Me.txtbox = “I am a textbox on the form that currently has the focus.” or you can update a label’s caption […]


Support these sponsors:
Previous Post

Access Database Users And Permissions Example

Next Post

MS Access Object Security