Escape key

  • Thread starter Thread starter Simonglencross
  • Start date Start date
S

Simonglencross

If I wanted to be able to press the escape key and the active form to then
close what would you suggest I do?

Any help greatfuly received!


Simon
 
You would need to trap and recode the OnKey Press event when the value =
vbKeyEscape.
Keep in mind that Access saves record(s) when a form closes, so if you want
to abort/close without saving, you'll need to handle that as well.

How will you permit users to undo any field/control entry when you recode
<Esc> to close your form?
Many applications use command buttons to "Close & Save" or "Exit w/o Saving"
etc.
-Ed
 
Stolem from a post by Terry Kreft from 1998....

Try these three scenarios out (Counter is a text field on the form which has
the focus)

1) KeyPreview for the form is set to Yes
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then MsgBox "form Key" : KeyCode = 0
End Sub

Private Sub Counter_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then MsgBox "Field key"
End Sub

Pressing the esc key results in a message box popping up saying "form Key"

2) KeyPreview for the form is set to No

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then MsgBox "form Key" : KeyCode = 0
End Sub

Private Sub Counter_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then MsgBox "Field key"
End Sub

Pressing the esc key results in a message box popping up saying "Field Key"

3) KeyPreview for the form is set to Yes
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then MsgBox "form Key"
End Sub

Private Sub Counter_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then MsgBox "Field key"
End Sub
Pressing the esc key results in a message box popping up saying "form Key"
followed by a msgbox saying "Field key"
 
Hi Simon,

If you have a command button on the form that closes it, just set the
button's Cancel property to Yes. Hitting Escape will then have the same
effect as clicking that button.
 
Back
Top