Command buttons have Default and Cancel properties. If set to True ('Yes' in
the Properties window) the Default property causes the Click event of the
command button to fire when Enter is pressed, and Cancel causes the Click
event of the command button to fire when Escape is pressed. So one easy way
to achieve what you want is to add a button to the form with DoCmd.Close in
its Click event and set the Cancel property to True.
Alternatively, you could add the following code to the KeyDown event of the
form. You'll also need to set the KeyPreview property of the form to True
('Yes' in the Properties window).
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
DoCmd.Close acForm, Me.Name
End If
End Sub
--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com
The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.