Filter form based on user input

Hi Experts,

I am working on a physical inventory database that uses a single table to hold all items to be inventoried.  Each item is unique based on the inventory period and the location where it is physically counted.  To enter the physical counts, each department has its own form filtered by department and sorted by location to match the counting sheets from the physical inventory.  I'm stuck on how to filter the form with user input based solely on the inventory period date.  I've researched how to pass the content from a combobox to form's filter property but I just don't understand how the code works.  If that isn't the correct approach, how do I structure a query that would use a date from another table.  I've attached a portion of the db for illustration.  Thank you, any help would be greatly appreciated!
InventoryDateFiltering.accdb
Skip SleeperIT ManagerAsked:
Who is Participating?
 
Gustav BrockConnect With a Mentor CIOCommented:
You can use code like this with two unbound textboxes:

Private Sub FromDate_AfterUpdate()

    FilterDates

End Sub

Private Sub FilterDates()

    Dim Filter  As String
    
    Filter = ""
    
    ' Build filter.
    If Not IsNull(Me!FromDate.Value) Then
        Filter = "InventoryPeriodDate >= #" & Format(Me!FromDate.Value, "yyyy\/mm\/dd") & "#"
    End If
    If Not IsNull(Me!ToDate.Value) Then
        If Filter <> "" Then
            Filter = Filter & " And "
        End If
        Filter = Filter & "InventoryPeriodDate <= #" & Format(Me!ToDate.Value, "yyyy\/mm\/dd") & "#"
    End If
    
    ' Apply filter.
    Me.Filter = Filter
    Me.FilterOn = (Filter <> "")
    
    ' Debug.Print Me.FilterOn, Me.Filter

End Sub

Private Sub ToDate_AfterUpdate()

    FilterDates
    
End Sub

Open in new window

See the attached demo, please.
InventoryDateFiltering.accdb
0
 
Skip SleeperIT ManagerAuthor Commented:
Hi Gustav,

I can't believe you did that so fast!  I'm going to get there someday...

If you don't mind, what are the procedures at the start and end of your code?

Thank you!
0
 
Gustav BrockCIOCommented:
Those are the events raised  when a date has been changed (or deleted).

They both call the same subfunction to set (or remove) the filter.
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Skip SleeperIT ManagerAuthor Commented:
Why isn't there a "command" preceding the FILTERDATES in the events code?
Thanks!
0
 
Gustav BrockCIOCommented:
That is not need. VBA looks up the name, finds it as a subfunction, and runs it.

If you prefer, you can write, using the old syntax:

    Call FilterDates()

Open in new window

0
 
Skip SleeperIT ManagerAuthor Commented:
Just curious-thanks for the info and the help!
0
 
Skip SleeperIT ManagerAuthor Commented:
Great help!
0
 
Gustav BrockCIOCommented:
You are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.