How to Select Access Textbox Content After an Update Event
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.

- 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 modifying your form events, ensure you have a backup of your Microsoft Access database and note the exact VBA names of your form controls.
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.
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').
Right-click the next control, select 'Build Event...', and choose 'Code Builder' to open the VBA Editor for that form.
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.
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.

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. Download the installer: Visit the official WPS website and click on the 'Download WPS Office Free' button.
- 2. Install the suite: Run the downloaded setup file to quickly install WPS Writer, Spreadsheet, and Presentation.
- 3. Open your database exports: Seamlessly open your Microsoft Access CSV or Excel data exports in WPS Spreadsheet for advanced formatting and analysis.

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.




