Code multiple forms simultaneously

P

Paul3rd

Hello, several of the users of my application have asked that the escape key
be used to close a form.
My application has 45 forms, is there a way I can change the KeyPreview
property to Yes and insert the small bit of code to the KeyPress event of all
the forms simultaneously?
Thanks in advance for any help
Paul
 
J

Jeff Boyce

I suppose one approach might be to create a general code module that closes
a form, then call that procedure from each form.

I'm not aware of a way to do all the forms at once.

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
K

Klatuu

You can't do that all at the same time. I would advise against using the
Escape key. It is special meaning in Access. When a form is open, pressing
it once performs an Undo on the current control. Pressing it twice performs
an Undo on the current record. I have not really tested it, but I think
there may be problems with using the Escape key for closing a form.

There is a way using some other key combination that would allow you to
close the Active form without having to modify code in any of your forms. As
a test, I created and AutoKeys macro with a key combination of <ctrl><Q>.
You enter it in as ^Q. Then for the action, use RunCode and enter
CloseTheForm

Here is the code you would put in a standard module to perform the close:

Public Function CloseTheForm()
DoCmd.Close acForm, Screen.ActiveForm.Name, acSaveNo
End Function
 
L

Linq Adams via AccessMonster.com

Amazingly, there are thousands of Access users who aren't aware of the native
purpose of <Esc> in Access, and who expect it to behave as it does in many
other applications, by backing out of a screen. While "several" of the end
users here specifically requested that <Esc> be used for this purpose, the OP
does, indeed, need to make sure that ***all*** the users are OK with this.
Having said that:

Set Key Preview to Yes

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
If Me.Dirty Then Me.Dirty = False
DoCmd.Close
End If
End Sub

If you use this with an unbound form, omit the line

If Me.Dirty Then Me.Dirty = False
 
K

Klatuu

Certainly that is the usual way to accomplish that. The only problem is the
OP was looking for a way to not have to put the code in every form.
Since the AutoKeys marco will not allow the use of the Escape key, my
solution will not be useful if he has to use the Escape key.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top