GotFocus, LostFocus Events

Applies To

Bound Object Frame Control, Check Box Control, Combo Box Control, Command Button Control, Form, List Box Control, Option Button Control, Text Box Control, Toggle Button Control, Unbound Object Frame Control.

Description

These events occur:

  • GotFocus — When a form or control receives the focus.
  • LostFocus — When a form or control loses the focus.

Note The GotFocus and LostFocus events apply only to forms and controls on a form.

Remarks

These events occur when the focus moves in response to a user action, such as pressing the TAB key or clicking the object, or when you use the SetFocus method in Visual Basic or the SelectObject, GoToRecord, GoToControl, or GoToPage action in a macro.

A control can receive the focus only if its Visible and Enabled properties are set to Yes. A form can receive the focus only if it has no controls or if all visible controls are disabled. If a form contains any visible, enabled controls, the GotFocus event for the form doesn’t occur.

You can specify what happens when a form or control receives the focus by running a macro or executing an event procedure when the GotFocus event occurs. For example, by attaching a GotFocus event procedure to each control on a form, you can guide the user through your application by displaying brief instructions or messages in a text box. You can also provide visual cues by enabling, disabling, or displaying controls that depend on the control with the focus.

The GotFocus event differs from the Enter event in that the GotFocus event occurs every time a control receives the focus. For example, suppose the user clicks a check box on a form, then clicks a report, and finally clicks the check box on the form to bring it to the foreground. The GotFocus event occurs both times the check box receives the focus. In contrast, the Enter event occurs only the first time the user clicks the check box. The GotFocus event occurs after the Enter event.

The LostFocus event differs from the Exit event in that the LostFocus event occurs every time a control loses the focus. The Exit event occurs only before a control loses the focus to another control on the same form. The LostFocus event occurs after the Exit event.

If you move the focus on one form to a control that doesn’t have the focus on another form, the Exit and LostFocus events for the control that does have the focus on the first form occur before the Enter and GotFocus events for the control on the second form.

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, 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 you switch between two open forms, the Deactivate event occurs for the first form, and the Activate event occurs for the second form. If the forms contain no visible, enabled controls, the LostFocus event occurs for the first form before the Deactivate event, and the GotFocus event occurs for the second form after the Activate event.

Note To customize the order in which the focus moves from control to control on a form when you press the TAB key, set the tab order or specify access keys for the controls.

See Also

Activate, Deactivate Events; ActiveControl Property; ActiveForm Property; Enabled, Locked Properties; Enter, Exit Events; GoToControl Action; GoToControl Action; Open, Close Events; SelectObject Action; SetFocus Method; Visible Property (Microsoft Access).