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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetFocus in VBA Script in Excel 1

Status
Not open for further replies.

ACH381

Technical User
Apr 3, 2002
49
US
I have a UserForm in Excel named "DailyForm"; within that I have a textbox named "TxtDay". When the user tabs out of TxtDay a script validates the data in TxtDay. If the entry is invalid I want the cursor set back to TxtDay and a message box indicating there's a problem.

I tried using:
If .... Then
DailyForm.TxtDay.SetFocus
MsgBox "....."

End If

However, the script ignores the SetFocus command and sets the cursor in the next field. I also tried reversing the order of MsgBox and SetFocus, it still doesn't work. I've tried setting the focus to the CancelButton as mentioned in another Tek-Tip question, but that doesn't work either. Actually I get an error message stating there was an unexpected command.

I've also tried using SendKeys "+{TAB}" but this produced unexpected tabbing problems when the form was piloted.

I've written a script with only the "DailyForm.TxtDay.SetFocus" command and it works fine by itself. It just doesn't work when applied as part of the If-Then-Else validation script.

How do I get this to work? Any work-around will do.
 
Try using the Exit event, and when you have done all the tests and want the focus to stay on this text box, set
Code:
 Cancel = True
. This kills the Exit event and allows the user to leave the textbox only if you approve.
 
Hi segmentationfault!

Thiss is what I was looking for and this work just fine.

Hope this would not be late.

[blush]

Cesar Humberto Parrales
Application Support
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top