nimeshaanethmini
New member
- Local time
- Today, 05:03
- Joined
- Aug 13, 2024
- Messages
- 4
Hey guys......
I’m working on a project in Microsoft Access and have hit a bit of a roadblock with some VBA code. I’m trying to filter records on a form based on user input, but I’m having trouble getting it to work as expected.
Here’s what I’m aiming to do:
I have a form with several fields, and I want users to be able to enter criteria into these fields to filter the records displayed in a continuous subform. The filtering should be dynamic, meaning that as users type in each field, the records in the subform should automatically update to match the criteria.
So far, I’ve set up the form and subform, and I’ve written some basic VBA code to handle the filtering. However, I’m encountering issues with the filter not applying correctly, and sometimes it even results in an error.
Here is the VBA code:
A few things I’m struggling with:
Thanks!

I’m working on a project in Microsoft Access and have hit a bit of a roadblock with some VBA code. I’m trying to filter records on a form based on user input, but I’m having trouble getting it to work as expected.
Here’s what I’m aiming to do:
I have a form with several fields, and I want users to be able to enter criteria into these fields to filter the records displayed in a continuous subform. The filtering should be dynamic, meaning that as users type in each field, the records in the subform should automatically update to match the criteria.
So far, I’ve set up the form and subform, and I’ve written some basic VBA code to handle the filtering. However, I’m encountering issues with the filter not applying correctly, and sometimes it even results in an error.
Here is the VBA code:
Code:
Private Sub txtFilter_AfterUpdate()
Dim strFilter As String
strFilter = "1=1" ' Start with a base condition that’s always true
If Not IsNull(Me.txtField1) Then
strFilter = strFilter & " AND Field1 LIKE '*" & Me.txtField1 & "*'"
End If
If Not IsNull(Me.txtField2) Then
strFilter = strFilter & " AND Field2 LIKE '*" & Me.txtField2 & "*'"
End If
Me.subForm.Form.Filter = strFilter
Me.subForm.Form.FilterOn = True
End Sub
A few things I’m struggling with:
- Handling Null Values: The current setup should handle null values, but sometimes I get an error when the form loads if a field is left blank. I’m not sure if this is causing issues with the filtering.
- Special Characters: I need to make sure the filter works correctly even if the user enters special characters. The current implementation uses LIKE with wildcards, but I’m concerned about how special characters might affect this.
- Performance: The form is becoming slower with more records. Are there any performance considerations or best practices I should be aware of for optimizing this filtering process?
- Error Handling: I’d like to add some error handling to provide user-friendly messages if something goes wrong. What’s the best way to incorporate error handling into this VBA code?
Thanks!