Others

How to Select Access Textbox Content After an Update Event

Algirdas JasaitisAlgirdas Jasaitis Oct 23, 2026 868 views

Question details

The user needs to automatically select and highlight the text inside a Microsoft Access search textbox after updating it and triggering a subform requery.

How to Select Access Textbox Content After an Update Event
Product
Microsoft Access
Device & OS
not provided
Scenario
A user enters a search term into a textbox, which uses a Like expression query to filter results in a subform. After pressing Enter, the user wants the original search text to remain highlighted for quick modification.
Observed behavior
When the AfterUpdate event triggers and requeries the subform, the search textbox loses focus, and the text selection disappears. Attempts to fix it break the search box functionality.
Before you start

Before modifying your form events, ensure you have a backup of your Microsoft Access database and note the exact VBA names of your form controls.

Solution 1Recommended

Use the GotFocus Event of the Next Control to Restore Selection

Shift the focus-restoring VBA code to the control that receives focus immediately after the search textbox.

When an AfterUpdate event requeries a subform in MS Access, the focus naturally shifts to the next control in the tab order. Attempting to set focus back to the original textbox directly within the AfterUpdate event often fails or breaks the Enter key functionality. Placing the selection code in the GotFocus event of the subsequent control resolves this conflict seamlessly.

1
Identify the next control in the tab order

Open your Access Form in Design View. Select the search textbox, check its Tab Index property, and identify which control has the next highest number (e.g., a control named 'myNextTextBox').

2
Open the VBA Editor

Right-click the next control, select 'Build Event...', and choose 'Code Builder' to open the VBA Editor for that form.

3
Add the GotFocus code

Enter the following VBA code into the GotFocus event of the next control: 'Private Sub myNextTextBox_GotFocus() Me.txtSB.SetFocus Me.txtSB.SelStart = 0 Me.txtSB.SelLength = Len(Me.txtSB.Text) End Sub'. Replace 'txtSB' with the actual name of your search textbox.

4
Save and test

Save the VBA module, switch back to Form View, and type in your search textbox. Press Enter to verify that the subform requeries and the search text remains selected.

Use the GotFocus Event of the Next Control to Restore Selection
Reliable Focus Shift: This method ensures the subform refresh completes entirely before focus is handed back to your search textbox, preventing locking issues.
Free Microsoft Office alternative

Looking for a Reliable, Free Office Suite? Try WPS Office

While WPS Office does not include a database application like MS Access, it provides a comprehensive, lightweight, and completely free alternative for your Word, Excel, and PowerPoint tasks. If you frequently export data or reports from databases, WPS Spreadsheet is a highly compatible and efficient tool for managing your CSV and XLSX files.

  1. 1. Download the installer: Visit the official WPS website and click on the 'Download WPS Office Free' button.
  2. 2. Install the suite: Run the downloaded setup file to quickly install WPS Writer, Spreadsheet, and Presentation.
  3. 3. Open your database exports: Seamlessly open your Microsoft Access CSV or Excel data exports in WPS Spreadsheet for advanced formatting and analysis.
Highly compatible with Microsoft Excel (.xlsx), Word (.docx), and PowerPoint (.pptx) formats.Lightweight installation that runs smoothly on older or low-spec devices.All-in-one tabbed interface for managing documents, spreadsheets, and PDFs in a single seamless window.
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't the AfterUpdate event keep the textbox focus?

In Microsoft Access, completing an AfterUpdate event typically forces the focus to move to the next control in the tab index. Trying to retain focus in the same event can cause logical conflicts or prevent the update from fully registering.

Can I use the KeyDown event instead of AfterUpdate to run the search?

Yes, you can capture the Enter key (KeyCode 13) in the KeyDown event to trigger your search procedure. However, you must still properly manage focus shifts if your VBA code requeries a subform.

How do I find the name of the next control receiving focus?

Open your form in Design View, select the search textbox, and check the 'Tab Index' property in the Property Sheet. The control with the next sequential Tab Index number is the one that receives focus after you press Enter.