PDA

View Full Version : "Enter" textbox upon UF initialize



jproffer
03-12-2009, 06:26 AM
I'm (fairly) sure this is easy, I'm just not seeing it.

Is there a way to have a certain textbox be the "default" textbox in a userform? I.E. - Have the cursor in it, ready to enter data, without clicking the textbox first.

I've tried to do this with the UF_Initialize code, but nothing I've tried so far has worked.(any textbox property that even remotely seemed like it MIGHT work).

Any help is appreciated. Thanks in advance :bow:

Bob Phillips
03-12-2009, 07:01 AM
Private Sub Userform_Initialize()

Me.TextBox1.Text = "some value"
End Sub

jproffer
03-12-2009, 10:53 AM
Hmmm...Tried that (actually tried --Me.Textbox1.Text = ""--) but I still have to manually "click myself into" the textbox before I can enter data.

After clicking the TB, the cursor is blinking and ready for me to type something...that's what I was hoping to have, but without having to click the TB first. Just be at that point upon UF intialize.

I do appreciate the help just the same, XLD.

Bob Phillips
03-12-2009, 11:16 AM
Sorry, misunderstood



Private Sub Userform_Initialize()

Me.TextBox1.SetFocus
End Sub

jproffer
03-12-2009, 05:14 PM
That makes perfect sense :) . I wish I would've thought to try that before asking a semi-silly question here, lol.

This WB is at work, so I'll try first thing tomorrow.

Thanks again xld.

GreenTree
03-12-2009, 06:40 PM
Is it possible to not only set the focus on a particular text box, but select the text inside it so that the default value can be typed over, if desired?

Thanks!

jproffer
03-12-2009, 07:05 PM
Assuming the "Setfocus" property does what I was looking for (and I'm sure it does cause XLD is the master, for real:bow: ) :

Userform_Initialize
Me.Textbox1.Value = ""
Me.TextBox1.SetFocus


Would do what you want to do, essentially clear the textbox, then enter into it.

EDIT: Just re-read what you said...the code here will delete the default before the user even sees it. As far as selecting it but leaving it to be viewed.............xld??? We're shining the bat signal. :)

Chris Bode
03-12-2009, 09:19 PM
Private Sub UserForm_Initialize()
TextBox1.SetFocus
End Sub




(http://www.spreadsheetconverter.com)

mikerickson
03-12-2009, 09:39 PM
Is it possible to not only set the focus on a particular text box, but select the text inside it so that the default value can be typed over, if desired?

Private Sub UserForm_Initialize()
With Me.TextBox1
.Value = "default value"
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
End Sub

jproffer
03-13-2009, 06:17 PM
Sorry I didn't get back to ya today, but (as I'm sure you already knew it would) the SetFocus worked perfectly. Thanks all.

By the way:

Mikerickson, that's a much better answer to greentree's question than what I put above.

GreenTree
03-13-2009, 07:57 PM
Mike,

Thank you for that! One problem I'm having is that this bit of code seems to work some times, but not others. Seems like the pattern is this:

The first time the form comes up, the value in the ID textbox is selected as I'd hoped for. I can scroll through the ID's (buttons on the form to go up/down) and each time the form is refreshed, the ID is selected.

Then I close the form (me.hide).

The NEXT time the form comes up, however, the ID is NOT selected, and scrolling through them doesn't change that behavior. HOWEVER, if I click in the ID textbox & put the cursor there, then click on Next or Previous, then the ID does come up as selected. And comes up that way for each subsequent refresh of the form. But, when I close the form, I'm back where I started: call it up, ID is not selected, and won't be until I click in that textbox. Then it will be each time the form is refreshed, until I close the form.

Is there something I'm doing wrong?

Thanks,

GT

GTO
03-13-2009, 08:11 PM
Greetings GT,

If you are not holding onto any vals when you hide the userform, you could change:
Me.Hide
'to
Unload Me

Hope this helps,

Mark

mikerickson
03-13-2009, 10:01 PM
If you want the default to be put in the text box every time that the UF is unhidden, move it to the Activate event.