PDA

View Full Version : Run procedure on exiting a field not on change



davidboutche
07-31-2009, 09:14 AM
I'm trying to get vba to run a procedure as soon as the contents of a field has been entered, the DOB field. It will run a small 'if' procedure to calculate someone's age to years and then if they're under 18 give a message.

i've tried:

Private Sub DOBBOX_Change()
If CalcAge(DOBBOX.Text) < 18 _
And UserForm1.Combo_Box_01 = "Simple Caution" Then
MsgBox ("You can't issue a simple caution to an under 18")
End If
End Sub

It essentially works every time a character is entered not when it is complete.

I've seen on another forum someone use '_exit' but this gives me an error message.

How should it be correctly used if this is the correct command?

David:dunno

fumei
07-31-2009, 09:53 AM
"It essentially works every time a character is entered not when it is complete."

_Change means exactly that. It fires every time there is a change. ANY change. Every time a character is entered...that is a change. So _Change properly fires.

"as soon as the contents of a field has been entered"

How is VBA to know when that is? It has no way of knowing if:

"bla" is complete, or "blahblah" is complete.

Please try to avoid things like:

"I've seen on another forum someone use '_exit' but this gives me an error message."

If you mention an error message it is helpful if you tell us that error message is.

_Exit may work, but for some reason - we do not know why as you did not tell us - it does not seem to be working for you.

Other options may be BeforeUpdate, or AfterUpdate. It may help if you look up these events in Help.