Ignore lostfocus event when a cancel button is pressed

Posted on 2005-03-09
Medium Priority
Last Modified: 2012-05-05

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

Question by:shvanwijlen
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 32

Accepted Solution

Erick37 earned 500 total points
ID: 13499174
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.
LVL 13

Expert Comment

ID: 13499207
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

LVL 13

Expert Comment

ID: 13499240
And if there is onnly validation I am absolutelly agree with Erik37.

Expert Comment

ID: 13499871
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
LVL 13

Expert Comment

ID: 13500390
RussellMartin is right,  forget my post

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


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question