How Best to Disable <ESC> Key on Form

M

MJ

My problem surrounds the <ESC> key function by design within Access. In my
office we have a major departmental database being accessed by 20-30 users at
any given time.
1. This is NOT an issue of the number of simultaneous users.
2. It IS a problem of users ACCIDENTALLY hitting <ESC> ocassionally
while they are entering data into this database.
+ The users are using this database and another commercial database
(non-Access, which has different functional use for the <ESC> key
than in Access) toggling back and forth between these databases.

While my director would like the <ESC> disabled for this database, I am
thinking that I might be better if we were able to "fix" the real issue by
disabling it on select forms, one at a time as needed.

One user suggested that it might be useful to have a "check" message box
seeking the user's confirmation BEFORE clearing in the event the <ESC> key is
hit. If the user does NOT confirm, then data remains as entered.

What would be the BEST way to approach this fix?

Thank you in advance for your assistance.
 
J

Joop Muis \(@Home\)

You could just enter the following code to your entry form:

Private EscapePressed As Boolean

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
EscapePressed = (KeyCode = 27)
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If EscapePressed Then
If MsgBox("Leave this form?", vbExclamation + vbOKCancel,
"Attention!!!") = vbCancel Then
Cancel = 1
EscapePressed = False
End If
End If
End Sub

Regards

JM
 
M

MJ

JM,

Thank you for your reply. I must be missing somethng though, when I do a
debug on the code you sent, I get a "compile error: user-defined type not
defined". What am I missing?

Thanks again for the inputs,
 
J

John W. Vinson

JM,

Thank you for your reply. I must be missing somethng though, when I do a
debug on the code you sent, I get a "compile error: user-defined type not
defined". What am I missing?

Thanks again for the inputs,

Just use AS Integer - since you're using an Access Form rather than a
Microsoft Forms form (I presume).

You'll also need to set the form's KeyPreview property to Yes.
 
J

John Spencer

If you want to disable the ESCape key on a form you can use

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then
'Debug.Print "Escape key pressed"
KeyCode = 0 'Cancel the key stroke
End If
End Sub

You will need to set the form's Key Preview property to True.

If you want the user to have options you need to insert something where I have
the commented out Debug. Perhaps lines like the following

If MsgBox("Undo changes?",vbYesNo,"What?") = vbNo Then
KeyCode = 0
End If


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
M

MJ

JM,

Changed the MSForms.ReturnInteger to Integer and fixed the compile error
(per John W. Vinson's suggestion) and set KeyPreview to Yes, but did not get
the results I was expecting.
1) The KeyDown SP seems to have broken all movement from field to field on
the form.
2) When <ESC> key was pressed, never got to the error message box.

Do you have any further suggestions?

Thank you for your time and inputs.
 
M

MJ

John,

Your reply did get me "closer" to the the results I was looking for, but not
quite there yet.

1) Your code for SR KeyUP with modification for msgbox...

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then
'Debug.Print "Escape key pressed"
If MsgBox(" Do you want to CLEAR changes?", vbExclamation +
vbOKCancel, "<ESC> Key Hit... What?") = vbCancel Then
KeyCode = 0 'Cancel the key stroke
End If
End If
End Sub

... it recognized that <ESC> key was hit and got me to the msgbox
question, but it DID NOT prevent clearing of data already entered into the
form.

2) Preventing the clearing of data entry is PARAMOUNT, so do you have any
suggestions on how to do that?

Thanks again for the help.
 
M

MJ

JM,

Changed the MSForms.ReturnInteger to Integer and fixed the compile error
(per John W. Vinson's suggestion) and set KeyPreview to Yes, but did not get
the results I was expecting.

1) The KeyDown SP seems to have broken all movement from field to field on
the form.

2) When <ESC> key was pressed, never got to the error message box.

Do you have any further suggestions?

Thank you for your time and inputs.
 
S

Stefan Hoffmann

hi,

What would be the BEST way to approach this fix?
I would take a look at the Form Undo event:

Private Sub Form_Undo(Cancel As Integer)

Cancel = (MsgBox("Undo changes?", vbQuestion+vbYesNo) <> vbYes)

End Sub



mfG
--> stefan <--
 
J

John Spencer

Try moving the code to the KeyDown event instead of the KeyUp event. Doing
that seems to work for me. Evidently the undo takes place before the keyup
event occurs.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 27 Then
If MsgBox(" Do you want to CLEAR changes?", _
vbExclamation + vbOKCancel, _
"<ESC> Key Hit... What?") = vbCancel Then
KeyCode = 0 'Cancel the key stroke
End If
End If
End Sub


John Spencer
Access MVP 2002-2005, 2007-2010
The Hilltop Institute
University of Maryland Baltimore County
 
M

MJ

John,

That was it exactly!

I came to the same conclusion this morning after looking a little deeper
after reviewing your earlier inputs against JMs. Already have the change in
place ready for release at COB this evening.

Thanks again for your inputs they helped out greatly!
 
D

De Jager

MJ said:
My problem surrounds the <ESC> key function by design within Access. In
my
office we have a major departmental database being accessed by 20-30 users
at
any given time.
1. This is NOT an issue of the number of simultaneous users.
2. It IS a problem of users ACCIDENTALLY hitting <ESC> ocassionally
while they are entering data into this database.
+ The users are using this database and another commercial
database
(non-Access, which has different functional use for the <ESC>
key
than in Access) toggling back and forth between these
databases.

While my director would like the <ESC> disabled for this database, I am
thinking that I might be better if we were able to "fix" the real issue by
disabling it on select forms, one at a time as needed.

One user suggested that it might be useful to have a "check" message box
seeking the user's confirmation BEFORE clearing in the event the <ESC> key
is
hit. If the user does NOT confirm, then data remains as entered.

What would be the BEST way to approach this fix?

Thank you in advance for your assistance.
 

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