Find out if a button key has been pressed

I have a form where i do some input validation on a field. However, if the user presses the QUIT button on the form i would like to skip the validation. Is there a way to find out if the QUIT button was pressed?

I use lostfocus event for my validation and hence the keypress event is not executed (at least not before the lostfocus event). Thank you so much for any help!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Martin LissOlder than dirtCommented:
If Screen.ActiveControl.Name = "Quit" Then
Martin LissOlder than dirtCommented:
Here's another way.

Private mstrLastControlName As String
Private Sub SomeOtherControl_Click()
If mstrLastControlName = "Quit" Then
End If
End Sub

Private Sub Quit_Click()
mstrLastControlName = "Quit"
End Sub

Open in new window

mpimAuthor Commented:
Thank you Martin, Unfortunately this does not work as I do the validation in Lostfocus of the previous control (lets say tUserID). The focus never gets transferred to the Quit control as the validation fails in Lostfocus... This means that if the user does not correct the input he will never get out of this control!
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Martin LissOlder than dirtCommented:
Did you try my second method? If so and it didn't work then please zip up and attach a project that shows the problem.
mpimAuthor Commented:
Thank you Martin.  I have the following simple code:

Private Sub tUserId_LostFocus()
If  ValidateUserInput() = False Then
End If
End Sub

Private Sub btnQuit_Click()
End Sub

Now the problem is that if the user input validation fails than the user will remain in the respective field which is fine. Except when he presses the Quit button then we should ignore the validation and execute the quit or close button. At present, the Quit button is ignored as the lostfocus has precedence. Thus, I need to test in the Lostfocus whether or not the Quit button was clicked and if yes ignore the validation. Thank you!
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:

> " I do the validation in Lostfocus of the previous control (lets say tUserID)"

Generally, the proper place for validation is on the form BeforeUpdate event.  That way, the user can go back and forth to get things just right.  Sometimes, you may want to also put validation on a control's BeforeUpdate event so the entry can be cancelled right away.  

> "if the user presses the QUIT button on the form i would like to skip the validation"

do you want to accept the record as it is, or should Quit behave like Undo and Close?

You need to move the validation since, as you have it now, validation will happen before the code can know what the user is clicking on next.

have an awesome day,
Please look at my last answer to this similar post.  It explains how to control saving by using a Save button =

If you don't want a Save button but you do want a cancel button, then the code for the cancel button would be:

If Msgbox("Are you sure you want to cancel?  Press Yes to Cancel and Press No to continue editing.",vbYesNo) = vbYes Then
End If

Open in new window


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
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
Microsoft Access

From novice to tech pro — start learning today.