Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to "SetFocus" on textbox if condition not met 2

Status
Not open for further replies.

TeddB

MIS
Jul 16, 2001
38
US
Simple form with two TextBoxes, Okay, and Cancel buttons.
If condition in TextBox1 is not met, I want to keep focus on it until condition is met or Cancel button is clicked.

I have tried the following code for TextBox1:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Value = "" Then
MsgBox "Invalid entry"
TextBox1.SetFocus
TextBox1.SelStart = 0
TextBox1.SelLength = 1000
Exit Sub
End If
End Sub

Does not work. MsgBox is displayed, then focus goes to TextBox2. I have tried the above code without "Exit Sub" and had same result.

(headscratch-scratch) I'm stumped!?
 
Hi,

Try adding
Code:
   UserForm1.Repaint
Hope this helps :) Skip,
metzgsk@voughtaircraft.com
 
Set the Cancel argument to True:

If TextBox1.Value = "" Then
MsgBox "Invalid entry"
Cancel = True
TextBox1.SelStart = 0
TextBox1.SelLength = 1000
Exit Sub
End If

ilses
 
Skip and ilses,

I tried the "Cancel = True" approach and it worked. I'm sure the "Repaint" approach will work too.

Thank you very much :->
Ted
 
Skip and Ilses,

After having overcome a couple of my coding hurdles- with your help, I have come up for air and realized my gratitude was incomplete.

Purple stars and my sincere thanks,
Ted

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top