Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Set the Default Command Button on a UserForm


View products that this article applies to.

Summary

On a UserForm, you can designate only one command button object as the default command button. Because only one command button can be the default, if you set the default property for any command button to True, the default properties for all other command buttons on the UserForm are automatically set to False.

↑ Back to the top


More information

Behavior When the Default Property Is True

If you set the default property of a command button to True, the following events occur, depending on which control has focus when you press ENTER during run time.
   Control with focus             Event
 --------------------------------------------------------------------------

   CommandButton (default=False)  Click and KeyDown (KeyDown has higher
                                  precedence) events for this CommandButton
                                  occur. (The default CommandButton is NOT
                                  activated.)

   CommandButton (default=True)   Click, KeyDown, or KeyUp (KeyDown has
                                  highest precedence) events for this
                                  CommandButton occur.

   Any control other than a       The default CommandButton is given the
   CommandButton                  focus, and the Click or KeyUp (Click has
                                  higher precedence) events for this
                                  CommandButton occur.
				
NOTE: The KeyDown event occurs when you press a key while the UserForm or control on the UserForm has the focus. The KeyUp event occurs after any event (KeyDown) for a control is started by pressing a key.

If you want a specific control on the UserForm to have the focus when you display the UserForm, set the TabIndex property for the control to zero (0) or use the SetFocus method.

Setting the TabIndex Property

If you set the TabIndex property of a control to a value of zero (0), the control is given the focus when you display the UserForm. The TabIndex property specifies the position of the object in the tab order (the order in which the focus moves from one object to another when you press TAB or SHIFT+TAB).

Using the SetFocus Method

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS If you do not want to change the tab order of the controls on the UserForm, use the SetFocus method to specify which object has focus when you display the UserForm. For example, if you want a combo box named ComboBox1 to have the focus when you display the UserForm, use the following sample code for the Initialize event of the UserForm:
Private Sub UserForm_Initialize()
    ComboBox1.SetFocus
End Sub
				
When you display the UserForm, the insertion point appears inside the combo box, regardless of the tab order on the UserForm.

↑ Back to the top


References

For more information about default command buttons, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type default property in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213743, kbprogramming, kbinfo, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213743
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 288