Kurt Wagner
Kurt Wagner

Reputation: 3315

How to Automatically have Text Cursor in Text Box (as Opposed to Command Button Selected) in Form?

I have two Excel picture objects linked to different forms, each with a text box and OK/Cancel buttons. In one form, the text cursor is in the textbox when clicking the object which is what I want:

Correct

but in another it selects the OK command button rather than have the text cursor in the textbox:

Incorrect


I've gone through the form and textbox/command button properties and see nothing about selection, and the 'correct' macro properties appear to be the same as the 'incorrect' one.

What do I do to change the form such that when it's opened the text cursor goes to the textbox instead of the command button being selected?

Upvotes: 5

Views: 47855

Answers (3)

David Zemens
David Zemens

Reputation: 53623

Before the form is displayed, you can do soemthing like:

TextBox1.SetFocus

Obviously, replace "TextBox1" with whatever the name of your textbox object is.

This should go in whatever event or macro causes the form to .Show, immediately before the .Show statement.

Upvotes: 1

Kazimierz Jawor
Kazimierz Jawor

Reputation: 19067

Quite easy solution is to change TabIndex Property to 0.

So, 1) go to VBA Editor, 2) select your TextBox in your UserForm and 3) change TabIndex Property in Property window as presented below.

enter image description here

Upvotes: 12

Ripster
Ripster

Reputation: 3585

Add an event to the form so that when it is initialized it selects the correct texbox.

Private Sub UserForm_Initialize()
    TextBox2.SetFocus
End Sub

Upvotes: 4

Related Questions