Hi people. I've been writing a little application and I want the user to be able to exit a form by pressing ESC key. I have tried a this so far but no joy:

Private Sub frmMenu_KeyPress(KeyAscii As Integer)
    If KeyAscii = 27 Then
        KeyAscii = 0
    End If
End Sub

I have tried a few different things but can't seem to get it to work. Any help would be appreciated. Thanks in advance

11 Years
Discussion Span
Last Post by leoimperial

If you're trying to unload a form, your code does not show any part of unloading. You can add the Unload Me somewhere in your code.


to exit the form by hitting the escape key .... u need to change the attributes ... no code required.
change the esacpe key to true in the attributes


hey frmMenu is the name of the form

Yes, I know. I tried creating a project with a form named frmMenu and placed the code. It didn't work. I replaced frmMenu_KeyPress with Form_KeyPress and it worked. Just sharing my experience.:)


ohhho ........... then jus try it stanwaka. These thinghs tend to work out sometimes. ANd v cant explain it. Even this might work.


You can place a Command Button in the form and set it's Cancel Property to True. In the Button_Click Event write
Unload Me

To give the illusion of the Escape Key working directly on the form, set the Button's visible property to false

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.