Enter Event [Access 2003 VBA Language Reference]
The Enter event occurs before a control actually receives the focus from a control on the same form.
Private Sub object_Enter()
Object The name of a control on a Form.
The Enter event applies only to controls on a form, not controls on a report. This event does not apply to check boxes, option buttons, or toggle buttons in an option group. It applies only to the option group itself.
To run a macro or event procedure when these events occur, set the OnEnter or OnExit property to the name of the macro or to [Event Procedure].
Because the Enter event occurs before the focus moves to a particular control, you can use an Enter macro or event procedure to display instructions; for example, you could use a macro or event procedure to display a small form or message box identifying the type of data the control typically contains, or giving instructions on how to use the control.
Unlike the GotFocus and LostFocus events, the Enter and Exit events don't occur when a form receives or loses the focus. For example, suppose you select a check box on a form, and then click a report. The Enter and GotFocus events occur when you select the check box. Only the LostFocus event occurs when you click the report. The Exit event doesn't occur (because the focus is moving to a different window). If you select the check box on the form again to bring it to the foreground, the GotFocus event occurs, but not the Enter event (because the control had the focus when the form was last active). The Exit event occurs only when you click another control on the form.
If you move the focus to a control on a form, and that control doesn't have the focus on that form, the Exit and LostFocus events for the control that does have the focus on the form occur before the Enter and GotFocus events for the control you moved to.
If you use the mouse to move the focus from a control on a main form to a control on a subform of that form (a control that doesn't already have the focus on the subform), the following events occur:
Exit (for the control on the main form)
LostFocus (for the control on the main form)
Enter (for the subform control)
Exit (for the control on the subform that had the focus)
LostFocus (for the control on the subform that had the focus)
Enter (for the control on the subform that the focus moved to)
GotFocus (for the control on the subform that the focus moved to)
If the control you move to on the subform previously had the focus, neither its Enter event nor its GotFocus event occurs, but the Enter event for the subform control does occur. If you move the focus from a control on a subform to a control on the main form, the Exit and LostFocus events for the control on the subform don't occur, just the Exit event for the subform control and the Enter and GotFocus events for the control on the main form.
Note You often use the mouse or a key such as TAB to move the focus to another control. This causes mouse or keyboard events to occur in addition to the events discussed in this topic.
When the Enter or Exit event occurs, you can run a macro that uses a GoToControl action to change the tab order based on current data in a control. For example, if the user enters Single in the MaritalStatus field, you can ensure that the focus doesn't move to the SpouseName field.
You can use the CancelEvent action in an Exit macro to cancel exiting the control. You can't use the CancelEvent action in an Enter macro to cancel the Enter event. However, you can use the GoToControl action in an Enter macro to move the focus to a different control.
In the following example, two event procedures are attached to the LastName text box. The Enter event procedure displays a message specifying what type of data the user can enter in the text box. The Exit event procedure displays a dialog box asking the user if changes should be saved before the focus moves to another control. If the user clicks the Cancel button, the Cancel argument is set to True (–1), which moves the focus to the text box without saving changes. If the user chooses the OK button, the changes are saved, and the focus moves to another control.
To try the example, add the following event procedure to a form that contains a text box named LastName.
Private Sub LastName_Enter() MsgBox "Enter your last name." End Sub Private Sub LastName_Exit(Cancel As Integer) Dim strMsg As String strMsg = "You entered '" & Me!LastName _ & "' as your last name." & _ vbCrLf & "Is this correct?" If MsgBox(strMsg, vbYesNo) = vbNo Then Cancel = True ' Cancel exit. Else Exit Sub ' Save changes and exit. End If End Sub
Applies to | BoundObjectFrame Object | CheckBox Object | ComboBox Object | CommandButton Object | CustomControl Object | ListBox Object | ObjectFrame Object | OptionButton Object | OptionGroup Object | SubForm Object | SubReport Object | TextBox Object | ToggleButton Object