Closed Thread
Results 1 to 13 of 13

Userforms : Default button

  1. #1
    Registered User
    Join Date
    02-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    36

    Userforms : Default button

    Been wondering how to set up so that forms have a default button.

    By that, I mean if you take a look for example at most softwares Print menu. You can edit the fields, click radio buttons, checkboxes and stuff, but yet the focus still stays on the 'OK' button, so if you press enter from anywhere, it sends it to print.

    That is just one example, but I find myself often pressing Enter when in my forms, and all it does is switch to the next field. The focus does not stay on the 'OK' button.

    Just wondered if there was a simple way to achieve that, or a simple setting in the Userforms that would allow that. Googling produces alot of answers that wasn't referring to this :P Thank you~

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Userforms : Default button

    Hi Franck,

    In the Design View of the userform, select the command button you want to set as the default and in the Properties window set the "Default" property to True. That should take care of it, hopefully!

  3. #3
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Userforms : Default button

    My idea is

    all controls on userform have properties TabStop - set it into False
    on Userform_initialize event run OKbutton.SetFocus
    on each control other then OKbutton on event afterudate of keyup or click etc. - call OKbutton.SetFocus at the end of code
    Last edited by MaczaQ; 03-27-2012 at 03:30 PM.
    Best Regards
    MaczaQ
    ---------------------------------------------------------------------------------------------------------------------------
    If you are satisfied with the solution(s) provided, please mark your thread as Solved
    If you are pleased with my answer consider to rate it. To thank someone who has helped you, click on the star icon below their name.
    - This way you will add him some reputation points ... thanks in advance.

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Userforms : Default button

    Hi Maczaq,

    The SetFocus method will only set the focus on that button when you load the form. As soon as you click a textbox or checkbox, etc., that focus will be drawn away from the "default" command button. You need to set it as the default in its properties to make sure it stays active when pressing Enter. (If you have more than one command button on the form and click a non-default button, the default status of your primary 'default' button will be lost, though - until you click into one of the other objects like textboxes/checkboxes, then the default status of your button will return.)

  5. #5
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Userforms : Default button

    so my idea is i.e. any move on form has been made - at the end of any controls code on form at the end of code Frank should type OKbutton.SetFocus
    so focus should be set up after any action

  6. #6
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Userforms : Default button

    You could, but using the Default property requires no code, so you don't have to remember to adjust it when something changes. (4 out of 5 programmers prefer only making 1 change instead of multiple. The fifth guy likes Java coding, so we don't really count him in polls.)


  7. #7
    Registered User
    Join Date
    02-22-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    36

    Re: Userforms : Default button

    Different ideas is good still, I'll use the default option Paul offered up for simplicity (lol java user :D), but I'll keep that in mind, there could be instances where it's handy to set it up manually after all.

  8. #8
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: Userforms : Default button

    You have right - now I lern how to use Default button
    I attach sample workbook shows my point of view - I know it is a bit stupid but works

    My solution works but default button is better indeed.

    I find only problem when on form there is more then one commandButton - other button had to have:
    Please Login or Register  to view this content.
    Cheers
    Attached Files Attached Files
    Last edited by MaczaQ; 03-27-2012 at 03:52 PM.

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2011
    Location
    Mississauga, CANADA
    MS-Off Ver
    Excel 2010
    Posts
    503

    Re: Userforms : Default button

    First you have to select all the controls in your form and set the TAB STOP property to false
    then paste this code in your form vba code page. Replace the "UserForm" word with the name of the form and replace the "CommandButton1" with the name of the button you want to activate when the user press ENTER.

    Please Login or Register  to view this content.
    Regards,
    Khaled Elshaer
    www.BIMcentre.com

    Remember To Do the Following....
    1. Thank those who have helped you by clicking the Star below their post.
    2. Mark your post SOLVED if it has been answered satisfactorily:
    • Select Thread Tools (on top of your 1st post)
    • Select Mark this thread as Solved

  10. #10
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: Userforms : Default button

    Here's a version using the Default property of CommandButton1 on the userform. Only code is to open the userform upon opening the workbook, and to display a message box when the button is clicked or Enter is pressed from any of the fields on the userform.
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    04-17-2019
    Location
    Europe
    MS-Off Ver
    2003
    Posts
    2

    Re: Userforms : Default button

    This doesn't work for me.

  12. #12
    Registered User
    Join Date
    04-17-2019
    Location
    Europe
    MS-Off Ver
    2003
    Posts
    2

    Re: Userforms : Default button

    Quote Originally Posted by MaczaQ View Post
    My idea is

    all controls on userform have properties TabStop - set it into False
    on Userform_initialize event run OKbutton.SetFocus
    on each control other then OKbutton on event afterudate of keyup or click etc. - call OKbutton.SetFocus at the end of code
    I don't have TabStop (in Excel 2003), but .SetFocus on the Userform_initialize event worked for me. Thanx!

  13. #13
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,780

    Re: Userforms : Default button

    Administrative Note:

    Welcome to the forum, Lestrad.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original. Please start a new thread - See Forum rule #4

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1