Enter, Exit Events Event Procedures

Description

To create an event procedure that runs when the Enter or Exit event occurs, set the OnEnter or OnExit property to [Event Procedure], and click the Build button.

Syntax

Private Sub controlname_Enter( )

Private Sub controlname_Exit(Cancel As Integer)

The Enter event procedure has the following argument.

Argument

Description

controlname

The name of the control whose Enter event procedure you want to run.


The Exit event procedure has the following arguments.

Argument

Description

controlname

The name of the control whose Exit event procedure you want to run.

Cancel

The setting determines if the Exit event occurs. Setting the Cancel argument to True (–1) cancels the Exit event


Remarks   You can't cancel the Enter event.

See Also   Enter, Exit events — macros.

Example

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