Active location on UserForm following MsgBox

S

Starbird

I'm sure this is a very simple thing, but I can't seem to make it happen.
I have the following code:

If me.txtAuth = " " Then
MsgBox ("Enter Author")
Me.Show
End If

I wanted the text box txtAuth to be the active location when user selects OK
in msgbox, how do I do that?

Thanks!
 
J

Jay Freedman

I'm sure this is a very simple thing, but I can't seem to make it happen.
I have the following code:

If me.txtAuth = " " Then
MsgBox ("Enter Author")
Me.Show
End If

I wanted the text box txtAuth to be the active location when user selects OK
in msgbox, how do I do that?

Thanks!

You definitely don't want Me.Show, since the userform (Me) is already
shown.

Instead, use txtAuth.SetFocus to move the cursor back to the textbox.
(Note that the use of Me to refer to controls on the current userform
is optional.)

There are a couple of ways to do this validation, depending on what
effect you want to achieve. Using SetFocus this way is good if you do
all the validation for several-to-many controls all at once when the
user clicks the OK button. Whichever control fails validation, set the
focus to that control.

The other way is to write code in the Exit procedure of each
data-entry control. That procedure comes with a Cancel argument; if
the contents fail validation, you set Cancel = True and the cursor
won't leave the control. Like this:

Private Sub txtAuth_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If txtAuth.Text = "" Then
MsgBox "Enter Author"
Cancel = True
End If
End Sub
 
S

Starbird

Thanks Jay, this group has been so helpful to me in this project I took on.
I could have never done it without all the help from this group!
Thanks again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top