Close Form Using ESC Key

Ashfaque

Search Beautiful Girls from your town for night
Local time
Tomorrow, 03:50
Joined
Sep 6, 2004
Messages
897
Hi, :)

I want to assign ESC key to close the form. I know the below way keeping Key Preview = Yes in forms property and KeyDown even with below code.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyF2
Docmd.OpenForm "fsdfdsfdsf"
End Select
End Sub

But I need to close the current form using ESC key only. How can it be done?

Please note my each form has set Pop Up and Modal =Yes.

Regards,
Ashfaque
 
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
try the following

Select Case KeyCode
Case vbKeyEscape
docmd.close acForm, me.formname
End Select
End Sub
 
Thanks Allan,

I tried this before but it produces error.

Ashfaque
 
Ashfaque

The only error I can see in this code is there is no ' before try the following


Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
try the following

Select Case KeyCode
Case vbKeyEscape
docmd.close acForm, me.formname
End Select
End Sub

I have tested the code and it works fine after you put in the ' or remove the text.
 
Thanks guys,

It producing "Run-time error 2001"

I think my form need to save info on it before closing....

Ashfaque
 
Thanks guys,

It producing "Run-time error 2001"

I think my form need to save info on it before closing....

Ashfaque

The ECS means to close the form an undo any changes. This is a Windows Application standard and one that I urge you to follow.

If ECS close the form then you need to use:

Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode
    Case vbKeyEscape

       If Me.Dirty Then Me.Undo   ' Cancel any changes

       docmd.close acForm, me.formname

End Select
End Sub
 

Users who are viewing this thread

Back
Top Bottom