Link to home
Start Free TrialLog in
Avatar of shvanwijlen
shvanwijlen

asked on

Ignore lostfocus event when a cancel button is pressed

Hi,

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

Thanks,
  Simon
ASKER CERTIFIED SOLUTION
Avatar of Erick37
Erick37
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
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

Cheers!
BTW
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,

Sorry