Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

SetFocus when a UserForm Displays

I have a form (UserForm1), that has ComboBox2 in it.  It is one that the user can type in the box, and the list it is attached to will goto the entry that first brings with the first letters typed then second... etc.

How do I set the focus to that field in the comboxbox?  I know it is something like:  UserForm1.ComboBox2.SetFocus, but where does this go?  I tried placing it just after the Show.UserForm1, but that did not work.   Currently the user has to click the dropdown arrow to get to the selection that populate that list.  I would rather that they can just hit a few keystrokes and get to their name in the list.

Any ideas?  I thought there was some sort of UserForm1_initalize sort of command that I could place it under, but not seeing that either.

Please advise and thanks. -R-
2 Solutions
You're right... It goes in the Initialize event

Viewing the Code window, there are two combo boxes
The left: Lists the objects -  select userform1 there..
The right: Lists the events- select initialize and the procedure will appear in the code.
The intialize event is good for initializing variable, setting certain properties, ect.  I would not use it as a rule for action type methods such as setting focus.  A better way to show a userform is to explicitly declare it.  This give you much more control.
Sub Example()
    Dim frm As UserForm1
    'this loads the form into memory but does not show it
    Set frm = New UserForm1
    'you now have access to all of the forms properties
    'now show your form
    frm.Show vbModal
    'don't unload the userform from within the userform module
    'simply hide it and unload it from your reference
    Unload frm
    Set frm = Nothing
End Sub

Open in new window

Rory ArchibaldCommented:
If you just want it to be the first focussed control, why not make its Tabindex 0? You don't need any code, just do it at design time.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Rory ArchibaldCommented:
PS Nice to have you here Tom, if you are the same guy from MrExcel. :)
Hi Rory.  Yes.  It's been awhile.  Congrats on your MSMVP.  That's how long it's been since we have last spoken.  Tom
Rory ArchibaldCommented:
That would be a long time! (except I wasn't a member of MrExcel before I was an MS MVP!) :) Good to "see" you again though.
RWayneHAuthor Commented:
Thanks for the help. -R-

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now