How to know via code when a user presses the windows X' or the ESC key in a VBA apps

We are trying to determine how to capture in the VBA code (specifically in Outlook VBA) when a user clicks the "X" button of the UserForm window and also if the user presses the ESCAPE key on the keyboard.
rayluvsAsked:
Who is Participating?
 
Patrick MatthewsCommented:
The following page explains the QueryClose event's syntax:

http://msdn.microsoft.com/en-us/library/gg278635.aspx

Thus, to disallow users to close the form by hitting the 'x' button:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    If CloseMode <> 1 Then 
        Cancel = True
        MsgBox "The Close box has been disabled", vbExclamation, "No soup for you!"
    End If

End Sub

Open in new window

0
 
rayluvsAuthor Commented:
That helped on when the user clicks on the "X" exit button of the form, but what about when the user presses the ESCape keyboard?
0
 
rayluvsAuthor Commented:
We have a UserForm that does a drop-down combobox window.

We have a Boolean variable named 'OptionNotSelected' that we want to set to TRUE when the user either press the ESCape keyboard or clicks on the "X" of the UserForm to exit.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Patrick MatthewsCommented:
You can use the KeyPress event for that:


Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    
    If KeyAscii = 27 Then MsgBox "Foo!"
    
End Sub

Open in new window



The Esc key's ANSI/ASCII value is 27.
0
 
rayluvsAuthor Commented:
doesn't work
0
 
Patrick MatthewsCommented:
Please show the exact code you are trying, and explain what you mean by "doesn't work".
0
 
rayluvsAuthor Commented:
I just copied your exact code.  Attached is an example in a an Excel (we have to do this for both an Excel and Outlook).  


TestEsc-Xbutton.xlsm
0
 
Patrick MatthewsCommented:
Use the same events for your controls:


Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 If KeyAscii = 27 Then MsgBox "Foo!"
End Sub

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 If KeyAscii = 27 Then MsgBox "Foo!"
End Sub

Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 If KeyAscii = 27 Then MsgBox "Foo!"
End Sub

Open in new window

0
 
rayluvsAuthor Commented:
Ok, you initial script was placing it in the UserForm, we have to place it in the field area not the form.

How about when clicking on the "X" corner ?

How doe we use your script:


   If CloseMode <> 1 Then
        Cancel = True
        MsgBox "The Close box has been disabled", vbExclamation, "No soup for you!"
    End If
0
 
Patrick MatthewsCommented:
Just add the code I gave you in http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_27421900.html#a37050772 to your UserForm's code module.

The code I supplied there cancels the event by setting Cancel = True.

You do not have to do that, of course.
0
 
rayluvsAuthor Commented:
I placed it in the UsertForm1 and it didnt work..  Do i have to put it in the fields (TextBox)?
0
 
rayluvsAuthor Commented:
It did worked!

Thanx!!
0
 
David Johnson, CD, MVPOwnerCommented:
escape won't work in the form itself but the close will as it is not in the text box where the 'textbox' has the focus all the escape key does is clear the box
0
 
rayluvsAuthor Commented:
Dont understand, I tried it and it works, ESC key when pressed is being captured.
0
 
rayluvsAuthor Commented:
Thanx
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.

All Courses

From novice to tech pro — start learning today.