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.
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.