VBA & Macro Problems

Filter Microsoft Access Records with an Unbound Search Box

Steven ParkerSteven Parker Nov 25, 2026 869 views

Question details

Filter an Access form using a search value without changing stored records, and display a message when multiple records match.

Filter Microsoft Access Records with an Unbound Search Box
Product
Microsoft Access
Device & OS
not provided
Scenario
A user enters a value in a form text box to show matching records.
Observed behavior
Existing VBA filters inconsistently, and the form needs to notify the user when more than one record matches.
Before you start

Open the form in Design View and identify the text field in its record source that you want to search. The example below uses an exact match on a text field named YourFieldName; replace it with your actual field name.

Solution 1Recommended

Filter the form from an unbound text box

Use this approach to show all exact text matches without writing the search value to a table field.

1
Add an unbound search box

Open the form in Design View and add a text box to the Form Header. In its Property Sheet, set Name to txtSearch and leave Control Source blank.

2
Open the event procedure

Select txtSearch, open the Property Sheet's Event tab, and choose After Update > Event Procedure. Click the ellipsis button to open the VBA editor.

3
Add the filter and count code

Replace the generated txtSearch_AfterUpdate procedure with the following code, changing YourFieldName to the text field in the form's record source: Private Sub txtSearch_AfterUpdate() Dim searchValue As String Dim rs As Object searchValue = Trim(Nz(Me.txtSearch.Value, "")) If Len(searchValue) = 0 Then Me.FilterOn = False Exit Sub End If Me.Filter = "[YourFieldName]='" & Replace(searchValue, "'", "''") & "'" Me.FilterOn = True Set rs = Me.RecordsetClone If Not rs.EOF Then rs.MoveLast If rs.RecordCount > 1 Then MsgBox rs.RecordCount & " matching records found.", vbInformation End If End If rs.Close Set rs = Nothing End Sub

4
Test the search

Save the form and switch to Form View. Enter a value in txtSearch, then press Tab or move focus away to run After Update. Clear the box and move focus away again to remove the filter.

Text fields and exact matches: The example escapes apostrophes in search values and matches the entire text value. Numeric and date fields require different filter expressions.
Free Microsoft Office alternative

Review exported Access records in WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar interface and Excel format compatibility for an easy transition. Export Access records to an Excel workbook to review and filter them in WPS Spreadsheets; WPS does not fix Access forms or run their VBA code.

Filter exported records in a spreadsheetCompatible with Microsoft Excel XLS and XLSX filesFamiliar spreadsheet interfaceFree and lightweight Office alternative
QA img-9

Frequently Asked Questions

Why should the search text box be unbound?

A bound text box can change a field in the current record. In Design View, select the search box and leave its Control Source property blank so it only holds the search value.

How do I show all records again?

Clear txtSearch and move focus away from it. The example's After Update procedure sets Me.FilterOn to False when the search box is empty.

Why doesn't the filter run as soon as I type?

The After Update event runs when the text box value is committed, such as when you press Tab or move focus to another control.

Can I use this code for a numeric field?

Not as written. For a numeric field, validate the input as a number and build the filter without the single quotes used for text values.

Can WPS Office run this Access form and VBA?

No. Use Microsoft Access for the form and its VBA. To review the records in WPS Spreadsheets, export them from Access to an Excel workbook first.