ESC - unload form

P

Patti

How would I code each form so that it closes if the user hits the esc key
while it has focus? I'm guessing the I'd put a procedure in the On Key Down
event, but I don't know the exact syntax.

Thanks!
 
B

Brendan Reynolds

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.
 
R

Rick Brandt

Patti said:
How would I code each form so that it closes if the user hits the esc key
while it has focus? I'm guessing the I'd put a procedure in the On Key Down
event, but I don't know the exact syntax.

Thanks!

That would be the way to do it, but I would advise against it. The
<Escape> key already has a well known defined use in Access which is to
cancel entries in the form that have not been saved. If you change this
behavior you run the risk of confusing and confounding your users.
 
P

Patti

Actually, I don't want to lose the ability to "undo" the last uncommitted
change on the form by hitting the esc key. Is it possible to make it fire
only if there are no populated (but uncommitted) fields on the form?
Otherwise, I'd want to code a key combo to unload the form.

Thanks again.

Patti
 
R

Rick Brandt

Patti said:
Actually, I don't want to lose the ability to "undo" the last uncommitted
change on the form by hitting the esc key. Is it possible to make it fire
only if there are no populated (but uncommitted) fields on the form?
Otherwise, I'd want to code a key combo to unload the form.

Were you aware that there is already a key combo for this? <Ctl-F4>.
 
W

Wayne Morgan

Yes, but as Rick mentioned, I don't recommend it and there is already a key
combination to do this. However, for future reference (should it come in
handy for something else):

If Not Me.Dirty Then
'run the code here
End If
 
P

Patti

Thanks for all the quick replies! I wasn't aware of the combo Rick
provided. I would definitely use that myself, but I wonder if the users
would be able to remember. What I am trying to avoid is the user having
multiple forms clogging up the screen. They can always hit the close button
in the corner, but they don't seem to want to bother. How do you deal with
that issue?

I'm also curious why you don't recommend the method below? Is it only
because there is already a key combo?

I appreciate the insight!

Patti
 
W

Wayne Morgan

I don't recommend it because as Rick pointed out, it is not the way a user
who is used to Access is going to expect it to work. Also, there are times
you press the Esc key twice, the first press undoes the current control if
it's dirty, the second undoes the entire record. It is not unusual to see
someone wanting to clear the entire record press the Esc key multiple times
without counting to verify they stop after two. It can easily be pressed 5
or 6 times in less than a second. All presses after the second one currently
don't do anything. To all of a sudden have the form start disappearing would
be a BIG aggrevation.

As far as the users remembering the key combo of Ctrl+F4, once they use it a
few times, they'll remember it. Getting them to start using it is the hard
part. As far as multiple forms open, you could hide the switchboard or
whatever you're using until the form that it opened is closed, this way they
won't have access to the next form until they close the first one. However,
there are times that it may be advantagous to have multiple forms open as
long as you aren't letting them lock records for everyone else while the
form sits open. You will also find that the clutter of multiple forms will
bother some users and not bother others. Those that it bothers will learn to
close the unneeded forms.
 
P

Patti

Thanks for the explanation Wayne!


Wayne Morgan said:
I don't recommend it because as Rick pointed out, it is not the way a user
who is used to Access is going to expect it to work. Also, there are times
you press the Esc key twice, the first press undoes the current control if
it's dirty, the second undoes the entire record. It is not unusual to see
someone wanting to clear the entire record press the Esc key multiple times
without counting to verify they stop after two. It can easily be pressed 5
or 6 times in less than a second. All presses after the second one currently
don't do anything. To all of a sudden have the form start disappearing would
be a BIG aggrevation.

As far as the users remembering the key combo of Ctrl+F4, once they use it a
few times, they'll remember it. Getting them to start using it is the hard
part. As far as multiple forms open, you could hide the switchboard or
whatever you're using until the form that it opened is closed, this way they
won't have access to the next form until they close the first one. However,
there are times that it may be advantagous to have multiple forms open as
long as you aren't letting them lock records for everyone else while the
form sits open. You will also find that the clutter of multiple forms will
bother some users and not bother others. Those that it bothers will learn to
close the unneeded forms.
 

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