• Setting the Focus in a User Form (VBA/Word/2000)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Setting the Focus in a User Form (VBA/Word/2000)

    Author
    Topic
    #359367

    I have created a userform and would like to set the focus for the first text box in the form, rather than forcing the user to click in the text box.
    I thought all I’d need to do was set the statement
    ProdTitle.SetFocus
    after the userform.show statement….
    ha!
    any suggestions?

    Viewing 3 reply threads
    Author
    Replies
    • #538465

      In the form code module, create

      Private Sub UserForm_Initialize()

      and put your SetFocus statement in there. The problem with having it in the main code module is that it doesn’t run until the UserForm has been dismissed. (Unless I am terribly confused.)

      • #538471

        Then how do I get the AutoNew function to find the private sub for the form? If I move the initialize statement into the Forms’ code the autonew function does not find it?

        Thanks

        p.s. this just shows you that nobody tells the whole story the first time. smile

        • #538474

          Your AutoNew works exactly as it did before, don’t change a thing, just add this event handler in the code for the form so that when it loads itself, it sets the focus:

          Private Sub UserForm_Initialize()
          Me.ProdTitle.SetFocus
          End Sub

          • #538642

            is me significant?

            ’cause I’m still not focussed dizzy

            • #538646

              Catherine,

              The “Me” is a keyword that can be used to refer to the UserForm object itself – you can only use this though in the userform’s own private code module.
              So for instance “Unload Me”.

              The SetFocus statement should be working (not sure why it isn’t), but here’s a different way that may avoid the need to use the UserForm_Initialize procedure altogether: just ensure that the ProdTitle textbox is the first item in the userform’s Tab Order.
              To set this:
              Click anywhere in the Userform.
              Click on View > Tab Order.
              This will display a Tab Order dialog.
              Use the buttons to move “ProdTitle” to first in the list.

              When you then “show” the userform, the focus should be go to the ProdTitle textbox.

              Gary

            • #538659

              No luck
              would the fact that it is a Multipage userform have an effect?

              Thanks

            • #538661

              Yes, the multipage form has an effect on what gets displayed.

              You also have to specify which page gets displayed.

          • #538660

            Me is not needed in self-references.

            • #538662

              Sure, but I need to type Me. to get the intellisense prompts. smile

            • #538664

              You can just type the control name to get the prompts.

            • #538669

              Ok – this is what it looks like now – and it is still not working.

              Private Sub UserForm_Initialize()
              UserForm1.MultiPage1.SetFocus
              UserForm1.ActiveControl.SetFocus
              UserForm1.ProdTitle.SetFocus

              End Sub

            • #538670

              But “me” gives you the control names.

            • #538677

              If I’m following the discussion correctly then I can refer to the userform either by name or me?
              ie
              me.prodtitle.setfocus
              or
              userform1.prodtitle.setfocus

              except neither work.
              What would I do with a multipage form to activate it?

              thanks.

            • #538679

              … Or even prodtitle.setfocus

              But you may have to activate the page of the form on which the textbox resides in order to setfocus. Do this by setting the Value propery of the control to the page number (starting from 0)

            • #538681

              Can you expand on that?

              Thanks

            • #538684

              This code:

              MultiPage1.Value = 1
              TextBox1.SetFocus

              works on a “click” event, but not in a userform_initialize event. I’m not sure why.

            • #538719

              Try this,

              with MultiPage1
              .Value = 1
              .TextBox1.SetFocus
              end with

            • #538798

              As a larger question – if one can not setfocus in the initialize event of a form. What else is it used for?

              Cheers

            • #538817

              Initialise is usually used to put in all the default information and list content so that when the form appears the fields contain information either from code or from data stored elsewhere.

            • #538819

              Aha. Got it.

              This works for me:

              Private Sub UserForm_Activate()
              MultiPage1.Value = 1
              TextBox1.SetFocus
              End Sub

              ie- in the Activate, not the Initialize.

            • #538818

              Howard,

              I thought you might have had something there- but I get the message “Object does not support this property or method” on the “.textbox1” line. Are you doing something different?

            • #538680

              You know what is really making me crazy? When I’m testing – if I click on the second page of the userform and back to the first page – the focus is EXACTLY where it should be!
              How can I make that happen without forcing the user to click?

              Cheers

            • #538683

              Rats. SetFocus sets the focus on the control but does not position the insertion point inside. What idiot designed that?

              This is a very, very cheezy workaround, but…

              Private Sub UserForm_Activate()
              ‘set focus on the correct textbox
              Me.TextBox1.SetFocus
              ‘tab to the next control, then shift-tab back
              SendKeys “{TAB}+{TAB}”, True
              End Sub

              (For some reason, I could not get this to work: “Changing the value of SelStart cancels any existing selection in the control, places an insertion point in the text, and sets the SelLength property to zero.”

              Me.TextBox1.SelStart = 0

              Nope, no insertion point visible.)

            • #538718

              You are not doing this correctly.
              It’s rather complicated to explain.
              You are better served by looking at a good VB book such as Gary Cornell’s Visual Basic 6 from te Ground Up for details.

              In effect, you wannas do something like the following.

              In Userform_Initialize:

              a. Choose the page that is on top when the userform is first displayed.
              b. After you get that working, then add code to choose the control within the displayed page.

            • #538717

              At this location, all programming is done by Me, Myself and I.

              Usually, Me does most of the programming, so Me already has a list of the control names in his head.

              Myself and I, tho as experienced as Me, occasionally use different coding techniques. But Me, as the senior partner, requires that they do not use “Me.” as Me is very protective of his identity.

    • #538838

      I Took that into account. My page 5 is physical page 6.

    • #538839

      You’ll need to get private paid help on this question.

    • #538928

      Have you seen MS Support Bulletin Q237863 – XL2000 SetFocus Property on TextBox does not work as expected?

      I’m hoping that installing the SR1A will solve my problems because my setfocus worked fine on the form under VB5 (Word97) and won’t under VB6 (Word2k)

      Ann Brandon

      Edited to activate link–Charlotte

    Viewing 3 reply threads
    Reply To: Setting the Focus in a User Form (VBA/Word/2000)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: