• Have cursor appear in a textbox (2000)

    Author
    Topic
    #360608

    Hey all, when a UserForm appears, I want the text cursor to appear in a TextBox so the user can just start typing as soon as the UserForm is shown. As it is right now, I have to click into the textbox to start typing.

    I tried txtFirstname.SetFocus but it didn’t seem to do the trick.

    Viewing 1 reply thread
    Author
    Replies
    • #543373

      Do you mean in Access? I wasn’t aware of forms in Excel.

      • #543377

        No, I mean in a UserForm using VBA with Excel.

        • #543380

          The code below, placed in the form initilize event procedure, works for me:

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

          Where are you putting your code?

          • #543384

            That didn’t seem to do the trick. I put the Sub in in the code of the form in question.

            For example, I’m loading “AddStudent”

            I put the Sub UserForm_Initialize() in the code body of AddStudent to pop the cursor to the “LastName” field.

            Sub UserForm_Initialize()

            Me.txtLastName.SetFocus

            End Sub

            • #543386

              Could you attach an example?

            • #543389

              Ack, it doesn’t let me attach exported forms. Grr! I’m simply placing the sub in the body of the code. All my other Initialize procedures run fine, just not this one. I’ve even tried Repaint-ing.

            • #543390

              No, you will need to attach a workbook that includes the form. You can either delete everything but the form, or create a new workbook and create a new form.

            • #543392

              Alright, thanks! Here it is.

            • #543415

              Applying a workaround from Jefferson Scher in a recent VBA Post, try the following form Activate event in place of your Initialize event :

              Private Sub UserForm_Activate()
                  SendKeys "{TAB}+{TAB}", True
                  Me.txtLastName.SetFocus
              End Sub

              The SendKeys line would probably be sufficient as the tab order should give focus to txtLastName by default, but it is no harm to include the line.

              Andrew C

            • #543421

              Thanks a lot! SendKeys is really useful and I can’t believe it took me this long to know of its existence. It did the trick.

            • #543425

              SendKeys can be very useful, and can also be very frustrating at times. It is a last resort solution.

              Glad to see it helped in this case.

              Andrew

            • #543428

              Andrew: Do you know what is causing the initilize event not to work as it was coded? It seems to work fine when I run the form from VBE.

            • #543435

              Legare,

              I cannot really say the the initialise event is not working properly, as the textbox does have focus – the problem seems to be with getting the cursor (insertion point) to show in the text box. I used the Activate event so that the active form would get the sendkeys message (when the form is being initialized it is not the active window, so I don’t think sendkeys would work there, except maybe by recourse tp API calls).

              In the file as posted by Kevin, it does not work for me (XL 2000) in the VBE without using Sendkeys, so I wonder what the difference might be. Does it work for you whilst the workbook is the active window ?

              Andrew

            • #543469

              Yes, it works fine for me when the workbook is the active window.

              Does using the SetFocus without the SendKeys if the code is in the Activate event instead of the Initilize? Also, when the code is in the Initilize event routine, is it getting executed when it does not work? What happens when it does not work?

            • #543624

              Legare,

              Just for the record Excel 2000, no SRs installed. Do yoiu have any SR ?

              Using Setfocus without Sendkeys does not work in the Activate event (nor in Initialize), but the reverse is true, i.e. Sendkeys on it’s own will work (in the Activate event only). I have come to the conclusion that the Frames are the cause of the problem. If the tab stop property of the first frame is set to false, the Activate works fine without sendkeys. However it is not a good idea to set that property to false as you may wish to tab back to the control, so I came up with the following which does work without the use of sendkeys, and involves both Initialize and Activate events :

              Private Sub UserForm_Initialize()
              Me.frmName.TabStop = False
              'Me.txtLastName.SetFocus
              End Sub
              Private Sub UserForm_Activate()
              Me.txtLastName.SetFocus
              Me.frmName.TabStop = True
              End Sub

              Now for the bizarre bit : If the green line is not commented out, nothing works, i.e. the cursor is not in the textbox. So as far as I can see using setfocus in the initilaize event is more a hindrance than a help. I can sort of understand why it might not leave the focus, as the actual form (window) is not active during the initialize event and so when the event finishes focus might be diverted. But I cannot understand why it should interfere with a setfocus in the activate event.

              I have set up a userform with 2 text boxes and nothing else. When displayed, the cursor appears in textbox 1 without any code. However if I try to setfocus in the initialize event, no cursor. This also happens if a frame is introduced.

              There is a bug in there somewhere, and it is not really connected with setfocus but rather having the cursor/Insertion Point show.

              Andrew C

            • #543641

              I have SR1 installed, and that may be the difference. I found This in the KB, and it sounds similar to this problem. It seems to have been fixed in SR1.

            • #543683

              SR1 seems to cure the problem ok. As a matter of fact there is no need to use setfocus (in either Initialize or Acivate events) at all unless you want to start at a tab order other than the designated first.

              Andrew

            • #543424

              If I run the form from the VBE, the initilize code seems to work just fine. What is happening for you? If you put a breakpoint in the initilize routine, do you hit the breakpoint when you display the form? How are you displaying the form?

    • #543822

      Hi Kevin

      You said you had the code in the Initialize event, move the code to the Activate event, it should work for you.

      I had the same problem, and I moved the code and it worked.

      Best of luck.

      Wassim

    Viewing 1 reply thread
    Reply To: Have cursor appear in a textbox (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: