GotFocus, LostFocus Events — Event Procedures

Description

To create an event procedure that runs when the GotFocus or LostFocus event occurs, set the OnGotFocus or OnLostFocus property to [Event Procedure], and click the Build button.

Syntax

Private Sub Form_GotFocus( )

Private Sub controlname_GotFocus( )

Private Sub Form_LostFocus( )

Private Sub controlname_LostFocus( )

The GotFocus and LostFocus event procedures have the following argument.

Argument

Description

controlname

The name of the control whose GotFocus or LostFocus event procedure you want to run.


Remarks   You can use a LostFocus event procedure to validate entered data as the user moves the focus from a control. You can also reverse or change conditions that you set up in the object's GotFocus event procedure.

Other uses for LostFocus and GotFocus event procedures are enabling, disabling, hiding, and displaying other objects.

You can't cancel a GotFocus or LostFocus event.

See Also   GotFocus, LostFocus events — macros.

Example

The following example displays a message in a label when the focus moves to an option button.

To try the example, add the following event procedures to a form named Contacts that contains an option button named OptionYes and a label named LabelYes.

Private Sub OptionYes_GotFocus()
    Me!LabelYes.Caption = "Option button 'Yes' has the focus."
End Sub

Private Sub OptionYes_LostFocus()
    Me!LabelYes.Caption = ""            ' Clear caption.
End Sub