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