ESC key processing in a form

I am trying to find a neat way to handle ESC key processing on a form. I would like ESC key to have two functions - when ESC is pressed in a form's non-empty control, I would like to just erase the control's contents. If ESC is  pressed second time in a row, I would like to exit the form. Here's the functon I am calling in Key press event:

Public Sub gvEsc(key, frm)
If key = vbKeyEscape Then
   If frm.ActiveControl <> Null Then
      frm.ActiveControl = Null
      Exit Sub
   Else
      DoCmd.Close acForm, frm.Name, acSaveNo
   End If
End If
End Sub

However this function closes the form immediatelly after the first hit of ESC.
Is there a solution without involving more than one event?

Thanks,
ggv

PS: more points to come for neat solution.
ggvAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nico5038Commented:
I would use the forms Form_KeyPress event to register two consequetive ESC key presses and take action after the second.
To activate this set KeyPreview to "Yes"
Something like:

dim intESC as integer

IF keyascii = 27 then
    if intESC > 0 then
      'close form
    else
      intESC = intESC + 1
else
    intESC = 0
endif


Getting the idea ?

Nic;o)
0
nico5038Commented:
Oops forgot  an endif, try:


IF keyascii = 27 then
    if intESC > 0 then
      'close form
    else
      intESC = intESC + 1
    endif
else
    intESC = 0
endif

Nic;o)
0
jadedataMS Access Systems CreatorCommented:
Hey ggv!

This setting this behaviour to the {ESC} key takes away the users ability to cancel field and form changes.  This is a PROGRAMMED behaviour of Access and should not be tinkered with.  Long time uses of Access expect the {ESC} key to function and behave a certain way.

regards
Jack
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ggvAuthor Commented:
nico5038

I understand in your sample the control's value has to be highlighted in oder for the first ESC to erase the value? Right?

jadedata

What is the expected way of ESC key?  I am not quite familiar with it. My simple reasoning is that it should be natural for ESC key to do some sort of escape. Therefore if pressed in a control - escape from the control (by erasing all input in it), pressed when all the controls are blank - escape from the form itself.

ggv
0
jadedataMS Access Systems CreatorCommented:
The normal key behaviour is
  Single {ESC} = revert current field to original value (Field UNDO)
  Double {ESC} = revert entire form to original value (Form UNDO)
0
ggvAuthor Commented:
This seems to work only in a form with bound fields. I am using unbound fields to establish search criteria and the ESC key works only in the active control. It does not revert all the form fields.

If this is normal key behaviour as you described - would it be not natural to exit the form after the third consecutive press of ESC?
0
nico5038Commented:
Myform code will only trigger the close when the ESC is pressed twice.
That's why the intESC is reset for every keypress <> ESC

By changing:
if intESC > 0 then
into
if intESC > 1 then
you'll need three consecutive presses.

For the single field reset your code to reset the field will suffide.

Clear ?

Nic;o)
0
ggvAuthor Commented:
Nico,

The code is clear and logically should work, but it doesn't (MS Access 2000). It erases the active control, but second ESC does not close the form. It is difficult to trap the reason since it is impossible to debug this type of a sub. Here's my exact code (Key preview=yes):

Public Sub gvEsc(key, frm)
If key = 27 Then
    If intESC > 0 Then
      DoCmd.Close acForm, frm.Name, acSaveNo
    Else
      intESC = intESC + 1
    End If
Else
    intESC = 0
End If

End Sub
0
nico5038Commented:
I created a working form with:

Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then
    If intEsc > 0 Then
      DoCmd.Close
    Else
      Screen.ActiveControl.Value = null
      intEsc = intEsc + 1
    End If
Else
    intEsc = 0
End If
End Sub

Assuming the active field can be set to null, this will do as you want.
It needs to be however the code "behind" the OnKeyPress event !

Nic;o)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ggvAuthor Commented:
Hi Nico,

I am afraid it doesn't work. It erases the input in the control, but doesn't close the form upon second ESC. I am also looking to have the code in a module so that I can call it from any form. Are you using MS Access 2000?

ggv
0
nico5038Commented:
Yep, I did test this with A2000.
Can you drop the compacted and zipped .mdb in my nico5038 mailbox "at" yahoo.com and I'll have a look.

Nic;o)
0
ggvAuthor Commented:
So how is it, nico? I sent you an example form to your e-mail.

ggv
0
nico5038Commented:
When I received it, it must be returned as my mailbox is empty at the moment...

Nic;o)
0
ggvAuthor Commented:
Thanks, Nico. It now works.

Here's the full code Nico suggested (if anyone needs it in the future):

Option Compare Database
Dim intEsc As Integer
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then
    If intEsc > 0 Then
      DoCmd.Close
    Else
      Screen.ActiveControl.Value = Null
      intEsc = intEsc + 1
    End If
Else
    intEsc = 0
End If

End Sub


Nico, could you please briefly explain what is the difference in declaring variable outside the Sub versus inside it?

Thanks!

ggv
0
nico5038Commented:
This is called the "scope" of a variable.
Each time a sub is executed Access will reset the DIM fields in that sub to their initial value.
When declared above the subs the field will be initialized when the form is activated and "remain" it's contents till the form is closed.

Nic;o)
0
ggvAuthor Commented:
I suppose this is equivalent to declaring a variable as Static inside the Sub, isn't it?

ggv
0
nico5038Commented:
Yep, you're getting it clear I C :-)

Nic;o)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.