How to Prevent Unwanted Carriage Returns in Access Text Boxes on Focus
Question details
The user needs to prevent a carriage return from being automatically inserted when navigating into an Access memo text box using the Enter key.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A user moves focus into a memo text box using the Enter key, where the text box is configured to 'New Line in Field'.
- Observed behavior
- A carriage return is inserted before the existing text, and attempting to fix it by setting SelStart in the GotFocus event fails to prevent the key action.
Before modifying VBA form module code, open your Access database in Design View and ensure your form's macro security settings allow code execution.
Handle the Enter Key in the KeyDown Event
Use custom VBA logic to intercept the Enter key press and determine whether to add a new line or shift focus to the next control.
When the Enter key is set to add a new line, it evaluates the keystroke immediately. By intercepting the keystroke at the KeyDown event level, you can cancel the default behavior (KeyCode = 0) and implement custom navigation logic.
Right-click your form in the Navigation Pane and select 'Design View'. Select the problematic memo text box.
Open the Property Sheet (F4), switch to the 'Event' tab, and click the ellipsis (...) next to the 'On Key Down' property. Choose 'Code Builder'.
In the VBA editor, add an If statement to check if KeyCode = vbKeyReturn. If true, handle your custom focus logic and set KeyCode = 0 to suppress the unwanted carriage return.

Set SelStart in the Enter Event
Move cursor-positioning code from the GotFocus event to the Enter event to prevent the line break before the control fully receives focus.
Looking for a Lightweight, Free Office Suite?
While WPS Office does not include a database management tool like Microsoft Access, it serves as an exceptional free alternative for your document, spreadsheet, and presentation needs. Experience seamless format compatibility, a familiar interface, and an incredibly lightweight installation.

Frequently Asked Questions
Why does my Access text box add a new line when I press Enter?
This occurs because the 'Enter Key Behavior' property of your text box is set to 'New Line in Field'. Pressing Enter to navigate to this field triggers the new line action instead of just moving the focus.
What is the difference between Enter and GotFocus events in Access?
The Enter event fires immediately before the control actually receives the focus, whereas GotFocus fires after the focus is established. Modifying cursor positions is often more effective in the Enter event.
How do I change the default Enter Key Behavior for an Access text box?
Open your form in Design View, select the text box, and open the Property Sheet. Go to the 'Other' tab and change the 'Enter Key Behavior' from 'New Line in Field' to 'Default'.




