SetFocus Method

See Also9DBZ0UV              Example32CUPV8>Low

Sets the focus1L3L8ZY to a form or control.

Syntax

object.SetFocus

Remarks

The object must be a form, MDI form, or control  which can gain the focus.  The SetFocus method can be used on any control except Frame, Image, Line, Menu, Shape and Timer.  After SetFocus is executed, any user input is directed to the specified form or control.

You can only set focus to a visible form or control.  Since a form and controls on a form are not visible until the form's Load event has finished, you cannot use SetFocus to set the focus to the form being loaded in its own Load event unless you also use the Show method to show the form before the completion of the Form_Load procedure.

You also cannot set the focus to a form or control if the Enabled property is set to False.  If the Enabled property has been set to False at design time, you must first set it to True before it can gain the focus using SetFocus.


See Also

Enabled Property3XD4FN

Load Event2S1GYBF

Show Method2IF9Z3Q


SetFocus Method Example

The example uses the SetFocus method to change focus between two command buttons.  To try this example, paste the code into the Declarations section of a form named Form1 that has two command buttons.  Use the default names for the command buttons.  Then press F5 and click the form.

 

Sub Form_Click ()

   Dim Msg                               ' Declare variable

   Msg = "Choose OK to watch the focus change to the other control."

   MsgBox Msg                            ' Display message.

   If Screen.ActiveControl.Caption = "Command1" Then

      Command2.SetFocus                  ' Set focus to button 2.

   Else

      Command1.SetFocus                  ' Set focus to button 1.

   End If

End Sub