Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Star Lounger
    Join Date
    Aug 2001
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Have cursor appear in a textbox (2000)

    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.

  2. #2
    Lounger
    Join Date
    Jan 2001
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

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

  3. #3
    Star Lounger
    Join Date
    Aug 2001
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

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

  4. #4
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

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

    <pre>Private Sub UserForm_Initialize()
    Me.TextBox2.SetFocus
    End Sub
    </pre>


    Where are you putting your code?
    Legare Coleman

  5. #5
    Star Lounger
    Join Date
    Aug 2001
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    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

  6. #6
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    Could you attach an example?
    Legare Coleman

  7. #7
    Star Lounger
    Join Date
    Aug 2001
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    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.

  8. #8
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    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.
    Legare Coleman

  9. #9
    Star Lounger
    Join Date
    Aug 2001
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    Alright, thanks! Here it is.
    Attached Files Attached Files

  10. #10
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    Applying a workaround from Jefferson Scher in a recent VBA Post, try the following form Activate event in place of your Initialize event :<pre>Private Sub UserForm_Activate()
    SendKeys "{TAB}+{TAB}", True
    Me.txtLastName.SetFocus
    End Sub</pre>


    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

  11. #11
    Star Lounger
    Join Date
    Aug 2001
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    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.

  12. #12
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    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?
    Legare Coleman

  13. #13
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    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

  14. #14
    Uranium Lounger
    Join Date
    Jan 2001
    Location
    South Carolina, USA
    Posts
    7,295
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    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.
    Legare Coleman

  15. #15
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Have cursor appear in a textbox (2000)

    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

Page 1 of 2 12 LastLast

Posting Permissions

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