GotFocus, LostFocus Events -- Event Procedures

Description

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

Argument

Description

controlname

A string that is the name of the control affected by the GotFocus and LostFocus event procedures.


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 this example, add the following code to the Declarations section of a module for a form called 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."SubSub OptionYes_LostFocus()
    Me!LabelYes.Caption = ""                    ' Clear caption.Sub