Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Expand Minimize

How to: Allow a Command Button to Take Focus Upon Clicking

Office 2007

The following example uses the TakeFocusOnClick property to control whether a CommandButton receives the focus when the user clicks on it. The user clicks a control other than CommandButton1 and then clicks CommandButton1. If TakeFocusOnClick is True, CommandButton1 receives the focus after it is clicked. The user can change the value of TakeFocusOnClick by clicking the ToggleButton.

To use this example, copy this sample code to the Script Editor of a form. To run the code you need to open the form so the Open event will activate. Make sure that the form contains:

  • A CommandButton named CommandButton1.
  • A ToggleButton named ToggleButton1.
  • One or two other controls, such as an OptionButton or ListBox.
Sub CommandButton1_Click()
    MsgBox "Watch CommandButton1 to see if it takes the focus."
End Sub

Sub ToggleButton1_Click()
    Set ToggleButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ToggleButton1")
    Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("CommandButton1")

    If ToggleButton1 = True Then
        CommandButton1.TakeFocusOnClick = True
        ToggleButton1.Caption = "TakeFocusOnClick On"
    Else
        CommandButton1.TakeFocusOnClick = False
        ToggleButton1.Caption = "TakeFocusOnClick Off"
    End If
End Sub

Sub Item_Open()
    Set ToggleButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("ToggleButton1")
    Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").Controls("CommandButton1")

    CommandButton1.Caption = "Show Message"
    
    ToggleButton1.Caption = "TakeFocusOnClick On"
    ToggleButton1.Value = True
    ToggleButton1.Width = 90
End Sub


Community Additions

ADD
Show:
© 2015 Microsoft