SetFocus when a UserForm Displays

Posted on 2010-01-03
Last Modified: 2012-06-21
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-
Question by:RWayneH

    Accepted Solution

    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.
    LVL 6

    Assisted Solution

    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

    LVL 85

    Expert Comment

    by:Rory Archibald
    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.
    LVL 85

    Expert Comment

    by:Rory Archibald
    PS Nice to have you here Tom, if you are the same guy from MrExcel. :)
    LVL 6

    Expert Comment

    Hi Rory.  Yes.  It's been awhile.  Congrats on your MSMVP.  That's how long it's been since we have last spoken.  Tom
    LVL 85

    Expert Comment

    by:Rory Archibald
    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.

    Author Closing Comment

    Thanks for the help. -R-

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Sometimes we don't want to show zeros in our Excel spreadsheets. This is sometimes most evident in our charts. Look at the chart below, all the zero values are visible. I think that all will agree with the fact that zero values are not looking nice …
    Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
    Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
    The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now