Link to home
Start Free TrialLog in
Avatar of kpt112
kpt112

asked on

Making a field required with Set focus

I want to make sure the use of my app enters properly formatted data into a text box.  The code I have does that, but when the user moves away form the box the set focus command is still active.

How can I get code the set focus to stop if the user decided to move to another part of my app?

Below is the code I am using.


Many Thanks in advance Kevin!



Private Sub TXTMBRNUMBER_LostFocus()
msg = "The Member Number must be exactly 10 digits in length"     ' Define message.
Style = vbOKOnly                      'Define buttons.
mylen = Len(TXTMBRNUMBER.Text)        'find length of string

If mylen > 11 Then           ' checks length of string
response = MsgBox(msg, Style, "Invalid Text Entry")
TXTMBRNUMBER.Text = " "
TXTMBRNUMBER.SetFocus
Exit Sub
End If
If mylen < 10 Then          ' checks length of string
response = MsgBox(msg, Style, "Invalid Text Entry")
TXTMBRNUMBER.Text = " "
TXTMBRNUMBER.SetFocus
Exit Sub
End If
Avatar of caraf_g
caraf_g

Instead of in the LostFocus procedure, why not code this validation in the (for example) click of the OK button? (It is quite irritating to have messages pop up every time you leave a control). Anyway - by doing that you avoid the whole problem.
ASKER CERTIFIED SOLUTION
Avatar of michelv
michelv

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
michelv, that's eerie! We both had the same idea in the exact same minute :-)
Yeah I saw that to, haha, fun !
Maybe we're interconnected through some kind of interstellar hyperkinetic beam or something *grin*
"teedeedeedee teedeedeedee" <theme from "the twilight zone">
Avatar of kpt112

ASKER

The problem is not with checking the length. The problem is if a user does type the incorrect length I want a message to popup and the focus to be set back to the item. When I use the set focus command it keeps setting the focus until the program blows up.  My text field can be for 10 or 11 digits. I want to know of a way to break out of the set focus if the user goes some where else.