SetFocus Method [Access 2003 VBA Language Reference]

The SetFocus method moves the focus to the specified form, the specified control on the active form, or the specified field on the active datasheet.


expression Required. An expression that returns one of the objects in the Applies To list.


You can use the SetFocus method when you want a particular field or control to have the focus so that all user input is directed to this object.

In order to read some of the properties of a control, you need to ensure that the control has the focus. For example, a text box must have the focus before you can read its Text property.

Other properties can be set only when a control doesn't have the focus. For example, you can't set a control's Visible or Enabled properties to False (0) when that control has the focus.

You can also use the SetFocus method to navigate in a form according to certain conditions. For example, if the user selects Not applicable for the first of a set of questions on a form that's a questionnaire, your Visual Basic code might then automatically skip the questions in that set and move the focus to the first control in the next set of questions.

You can move the focus only to a visible control or form. A form and controls on a form aren't visible until the form's Load event has finished. Therefore, if you use the SetFocus method in a form's Load event to move the focus to that form, you must use the Repaint method before the SetFocus method.

You can't move the focus to a control if its Enabled property is set to False. You must set a control's Enabled property to True (–1) before you can move the focus to that control. You can, however, move the focus to a control if its Locked property is set to True.

If a form contains controls for which the Enabled property is set to True, you can't move the focus to the form itself. You can only move the focus to controls on the form. In this case, if you try to use SetFocus to move the focus to a form, the focus is set to the control on the form that last received the focus.




The following example uses the SetFocus method to move the focus to an EmployeeID text box on an Employees form:


Applies to | BoundObjectFrame Object | CheckBox Object | ComboBox Object | CommandButton Object | Control Object | CustomControl Object | Form Object | Image Object | ListBox Object | ObjectFrame Object | OptionButton Object | OptionGroup Object | Page Object | SubForm Object | TextBox Object | ToggleButton Object