Ignore lostfocus event when a cancel button is pressed


I have a nr of fields on my form that I validate via a lostfocus event. But, when I click the cancel-button, I want to exit straight away and ignore those events. Is that possibe?
I can add a line to each lostfocus event like "if combo1.text = "" then do nothing", but I'd like to think there is a more generic way...

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Normally you would validate the contents in the Validate event.  Setting the CausesValidation=False for the Cancel button will prevent the Validate event from firing when that button is pressed.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hi shvanwijlen,
Declare global var that will indicate that cancel button was pressed

Dim bWasCanceled as Boolean

Private Sub btnCancel_Click()
       bWasCanceled   = True

' .... rest of your code if any

End Sub

Private Sub YourControl_LostFocus()

If bWasCanceled Then Exit Sub

'.... your validation code

End sub

And if there is onnly validation I am absolutelly agree with Erik37.
go  with Erick37 .
I dont believe michaels approach would work since the lost focus event would in fact invoke before the click event of the cancel button
RussellMartin is right,  forget my post

I am using this technique  sometime but never with LostFocus event,

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.