Enter, Exit Events -- Event Procedures

Description

To create an event procedure that is run 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 uses the following argument.

Argument Description
controlname A string that is the name of the control affected by the event procedure.


The Exit event procedure uses the following arguments.

Argument Description
controlname A string that is the name of the control affected by the Exit event procedure.
Cancel The setting determines if the Exit event occurs. Setting Cancel to True (-1) cancels the Exit event


Remarks

You can’t cancel the Enter event.

See Also

Enter, Exit Events — Macros.

Example

In this 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 chooses Cancel, the Cancel argument is set to True (-1), which moves the focus to the text box without saving changes. If the user chooses OK, the changes are saved, and the focus moves to another control.

To try the following example, add the code to the Declarations section of a form that contains a text box named CodeName.


Private Sub LastName_Enter ()
    MsgBox "Enter your last name."Sub
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 IfSub