?
Solved

ESC key processing in a form

Posted on 2003-11-16
17
Medium Priority
?
659 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:ggv
  • 8
  • 7
  • 2
17 Comments
 
LVL 54

Expert Comment

by:nico5038
ID: 9760075
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
 
LVL 54

Expert Comment

by:nico5038
ID: 9760083
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
 
LVL 32

Expert Comment

by:jadedata
ID: 9760192
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ggv
ID: 9762070
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
 
LVL 32

Expert Comment

by:jadedata
ID: 9763192
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
 

Author Comment

by:ggv
ID: 9763950
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
 
LVL 54

Expert Comment

by:nico5038
ID: 9765636
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
 

Author Comment

by:ggv
ID: 9766956
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
 
LVL 54

Accepted Solution

by:
nico5038 earned 400 total points
ID: 9767085
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
 

Author Comment

by:ggv
ID: 9795860
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
 
LVL 54

Expert Comment

by:nico5038
ID: 9796022
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
 

Author Comment

by:ggv
ID: 9845388
So how is it, nico? I sent you an example form to your e-mail.

ggv
0
 
LVL 54

Expert Comment

by:nico5038
ID: 9846315
When I received it, it must be returned as my mailbox is empty at the moment...

Nic;o)
0
 

Author Comment

by:ggv
ID: 9866896
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
 
LVL 54

Expert Comment

by:nico5038
ID: 9868668
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
 

Author Comment

by:ggv
ID: 9872927
I suppose this is equivalent to declaring a variable as Static inside the Sub, isn't it?

ggv
0
 
LVL 54

Expert Comment

by:nico5038
ID: 9876613
Yep, you're getting it clear I C :-)

Nic;o)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question