• Status: Solved
  • Priority: High
  • Security: Public
  • Views: 85
  • Last Modified:

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!
0
mpim
Asked:
mpim
2 Solutions
 
Martin LissOlder than dirtCommented:
If Screen.ActiveControl.Name = "Quit" Then
0
 
Martin LissOlder than dirtCommented:
Here's another way.

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

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

Open in new window

0
 
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!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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.
0
 
mpimAuthor Commented:
Thank you Martin.  I have the following simple code:

Private Sub tUserId_LostFocus()
If  ValidateUserInput() = False Then
    tUserId.SetFocus
End If
End Sub

Private Sub btnQuit_Click()
DoCmd.close
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!
0
 
crystal (strive4peace) - Microsoft MVP, AccessRemote Training and ProgrammingCommented:
hi,

> " 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,
crystal
1
 
PatHartmanCommented:
Please look at my last answer to this similar post.  It explains how to control saving by using a Save button = https://www.experts-exchange.com/questions/29080811/Access-VBA-Code-to-stop-data-saving-to-tables.html

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
    Me.Undo
End If

Open in new window

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

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now