USER Form

G

Greg Maxey

Hi,

I am working on a UserForm. I want to enter a number in a
Textbox. If the user enters non-numerical text I want a
message to appear and the focus to return to the text
box. I have the following code set to run on exit from
the text box. I get to the message, but I can't figure
out what I need to return the focus to the textbox that
caused the message.

Private Sub QP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim oForm As Document
Set oForm = ActiveDocument
oForm.Variables("QP").Value = QP
If Not IsNumeric(QP.Value) Then
MsgBox "You must enter a valid number."
' "What needs to go here to return the focus to the
Textbox OP?"
End If
End Sub

Thank you.
 
G

Greg Maxey

I have discovered that Cancel = True will send me back to
the QP textbox but it does not take focus (meaning it just
returns the cursor in the textbox after the offending
text. Is there a command that will return to the QP
textbox and have focus on the text in the box so the user
can just start typing the correct information?
 
S

Steve Lang

Hi Greg,

To select all the text in the text box, try this:

With QP
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With

HTH and have a great day!

Steve
 
G

Greg Maxey

Steve,

That worked! I had to change the event from exit to
change though ????

Now the message is generated the instant a non-numerical
character is entered.
 
G

Greg Maxey

Steve,

I take that back, I was able to get this work by putting a
Cancel = True statement following the code you provided.

Private Sub QP_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim oForm As Document

Set oForm = ActiveDocument
oForm.Variables("QP").Value = QP
If Not IsNumeric(QP.Value) Then
MsgBox "You must enter a valid number."
With QP
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
Cancel = True
End If
End Sub

Thank you very much!!
 

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