Link to home
Start Free TrialLog in
Avatar of shifty_mc
shifty_mc

asked on

How to effectively cancel LostFocus event in vba

Hi,

I have some code that has to be run in the LostFocus event of a textbox.

It checks the contents, and if they're not ok according to some rules, it sets the focus back on the textbox and higlights the text...
If ... Then
        MsgBox "The UAN you entered is not valid, please retype", vbCritical, "Invalid UAN"
        Cancel = True
        Me.B_C.SelStart = 0
        Me.B_C.SelLength = Len(Me.B_C.Value)
End If

That works fine in the BeforeUpdate event where cancel is a parameter, and I understand why it won't work in the LostFocus event, but is there some kind of workaround? ie would it work if I abstracted the code to a module or something along those lines?

Thanks for your help
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

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
Avatar of shifty_mc
shifty_mc

ASKER

Beautiful stuff - simple as that.

Cheers - points given.