Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the ESC key to close forms 2

Status
Not open for further replies.

jcg6

Technical User
Feb 18, 2002
50
US
I would like to be able to close all the forms in my database when the ESC key is pressed. So, if a form is open and I press ESC the form will close. I have quite a few forms in my database, so I don't know if I need to insert some code in all of them, or if I could use a module?

If you could provide the code necessary to close the forms with the ESC key that would be fantastic!! I would appreciate any help that you could offer. Thanks much!! :)
 
See FAQ faq702-2071 for another possibility. To do it with the escape key, set the forms Key Preview property to Yes and in the On Key Press event put something like the following:

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyEscape Then
DoCmd.Close acForm, YourFormName
End If
End Sub

I can't check this out and it is off the top of my head, but the syntax is pretty close.

Good Luck! Please remember to give helpful posts the stars they deserve! This makes the post more visible to others in need![thumbsup]
 
Remember, becasue the user asks for something and the fact that it can be done; does not necessary mean you should do it. The ESC Key is used in forms with sub-forms (data sheets for example) to clear a record or entry. Personally, I consider the ESC Key a reserve key used by MS Access. In a Windows environment, the behavior is usally event driven via a mouse... That is why I have a close button and turn on the "X" to close the form... Just a thought... With that said, here is the code you need...

Set Form Property KeyPreview to YES

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then
Call Command0_Click
End If
End Sub


Private Sub cmd_Exit_Form()
On Error GoTo Err_cmd_Exit_Form_Click
DoCmd.Close

Exit_cmd_Exit_Form_Click:
Exit Sub

Err_cmd_Exit_Form_Click:
MsgBox Err.Description
Resume Exit_cmd_Exit_Form_Click

End Sub
Steve Medvid
"IT Consultant & Web Master"
e-Mail: Stephen_Medvid@GMACM.com

Chester County, PA Residents
Please Show Your Support...
 
The keypress event of the form will not fire if a control on the form has the focus, so I don't see how either of these solutions will work. Am I missing somehting?

On the other hand, I agree with the second poster that it's probably a bad idea to make this happen. People get used to what different keys do, that's what programming standards are for. Imagine someone trying to market word processing software in which the key combo of CTRL-S deleted everything in the document. <G>.

Put a close button on all of your forms. Make the caption &quot;&Close&quot;. Then they can close your form by hitting alt-c, just as in most windows applications you can, in effect, click a button by typing ALT and the underlined letter on the button.

Hope this helps.

Jeremy =============
Jeremy Wallace
Designing, Developing, and Deploying Access databases since 1995.
 
Jeremy

The other solutions both mention that the form's KeyPreview property to has to be set to Yes. This allows the form to intercept key strokes before the controls get them.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top