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

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

Wait. Shouldn't frmMenu_Keypress be just Form_Keypress ?

Wait. Shouldn't frmMenu_Keypress be just Form_Keypress ?

hey frmMenu is the name of the form

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

//Try this but the focus must be on that object
If KeyAscii = 27 Then
unload me
End If

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.