Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I put the cursor in a textbox when a userform is activated?

Status
Not open for further replies.

RobBroekhuis

Technical User
Oct 15, 2001
1,971
0
0
US
This should be a simple question: I have a userform with only a textbox and a few buttons. The textbox has tabindex=0, but when the form first displays, there is no cursor in the textbox. I've tried using the setfocus method, and that doesn't help. How do I get the behavior of the standard inputbox function, which displays the cursor right away? Having to click in the box before starting to type is annoying.
Rob
 
Try running the macro recorder then starting up your UserForm and clicking in the TextBox. Then stop the recorder and look at the code it generated. Maybe you can incorporate what it spits out into your code. ----------------------------------------
If you are reading this, then you have read too far... :p

lightwarrior@hotmail.com
 
Have you tried to qualify the textbox name with the
form name?

frmWhatever.txtWhatever.SetFocus

This might help.
 
Thanks Logius and vbMax, for your suggestions. Unfortunately:
- the macro recorder records a wonderfully empty macro (even if I type text in the textbox) when I try logius' approach
- I shouldn't have to qualify the textbox, since I am accessing it from within the userform code, but I tried anyway, and it didn't help.

I thought this would be an easy problem, but I guess it's not as simple as I thought...
Rob
 
Check to see if the TabStop property is set to true. ----------------------------------------
If you are reading this, then you have read too far... :p

lightwarrior@hotmail.com
 
Yes, the tabstop property is set to true. And I just checked something else - when I start up the userform, and tab once, I go to the OK button, tab once more I go to the cancel button (that's all according to my tabindex sequence), and a third time and I go to the textbox WITH A CURSOR! Now how do I get it to do that without having to tab through a full cycle?
Rob
 
I just found a work-around: in the userform_activate sub, I now have the following code:
Code:
Private Sub UserForm_Activate()
   CancelButton.SetFocus
   CommentText.SetFocus
End Sub
Somehow adding the cancelbutton.setfocus did the trick...
Rob
 
That's just wierd. I tried to duplicate the problem you were having, but the only time it worked the same for me was when I set the TabStop property to False. Otherwise, the textbox always appeared with the cursor in it. ----------------------------------------
If you are reading this, then you have read too far... :p

lightwarrior@hotmail.com
 
I have noticed that if a textbox is in a FRAME on the userform then the cursor isn't in the textbox even though the tabindex is 0. Just an observation - I wish I had an answser why.
 
My First Message on the Forum

Haven't tested this but I'm pretty sure the tabindex should be 1.
 
In the Sub form_activate()
say me.show and then set text1.setfocus and try

HTH
 
Try like this

Private Sub UserForm_Activate()
Me.show
CancelButton.SetFocus
CommentText.SetFocus
End Sub

This works

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top