XL97: Setting the Default CommandButton on a UserForm

Last reviewed: March 13, 1998
Article ID: Q161537
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

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

MORE INFORMATION

Behavior When the Default Property Is True

If you set the Default property of a CommandButton 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 engineers 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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

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 ComboBox named "ComboBox1" (without the quotation marks) 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 ComboBox, regardless of the tab order on the UserForm.

REFERENCES

For more information about default CommandButtons, click the Office Assistant, type "default," click Search, and then click to view "Default Property."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Microsoft Excel Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q120802
   TITLE     : Office: How to Add/Remove a Single Office Program or
               Component


Additional query words: XL97
Keywords : kbcode kbprg xlvbahowto xlvbainfo
Version : WINDOWS:97
Platform : WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 13, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.