PDA

View Full Version : ESC key to unload a form



sujittalukde
12-13-2007, 02:03 AM
I am using the following code to unload a user form :


Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then
Unload Me
End If
End Sub

In case the form doesnot contain any control then esc unloads the form but if the user form has some controls esc doesnot unloads the user form.
How this can be done?

Simon Lloyd
12-13-2007, 02:45 AM
You need to use the OnKey event, like this:

Application.OnKey "{ESC}", "YOUR PROCEDURE NAME"

and you reset the ESC key like this:

Application.OnKey "{ESC}"

sujittalukde
12-13-2007, 03:24 AM
Thanks Simon for the reply But where to put the code?

Bob Phillips
12-13-2007, 04:00 AM
IN the userform initialize event, or maybe activate.

Reset it in the procedure Simon gave.

Simon Lloyd
12-13-2007, 04:54 AM
Thanks for the 'nod' Bob i dont know much but what i do know is usually useful (in the event of no other help! ;) )

Private Sub UserForm_Initialize()
Application.OnKey "{ESC}", "YOUR PROCEDURE NAME"
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.OnKey "{ESC}"

End Sub