Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I stop ESCAPE

Status
Not open for further replies.

Appollo14

Technical User
Joined
Sep 4, 2003
Messages
182
Location
GB
Hi,

Can you tell me how to prevent the user using the escape key on a form.

Regards,
Noel.
 
Set the forms key preview propety to yes, and in the forms keydown event, something like this:

[tt] if keycode = vbkeyescape then
keycode = 0
end if[/tt]

Setting keycode = 0 cancels the key, here ESC.

Roy-Vidar
 
Depending on where the user has the focus, you can catch it in the KeyDown event. I'd put it in the Form_KeyDown and then any textbox's keydown or any other fields on your form. Once they hit the escape key, it will trigger the KeyDown event and you can catch the specific keycode for the escape key (integer value of 27) and do whatever processing you need to at that point.

Let me know how it goes.
ethorn10
 
Looks like you beat me to the punch while I was typing my book. haha.
 
Soory to be a pain, but this just isnt doin what i expected. On a form I have an autonumber, to save writing a complicated rollback procedure i was hoping that I could stop the user pressing the escape key and therefore cancelling the number. Unfortunately if the escape key is pressed the number is still cancelled. Where am i going wrong.

regards,
Noel.
 
In my tests, the above traps the ESC key if it's pressed, but the real challenge here, is probably usage of autonumber for something it's probably not intended to.

If you search TT with keyword autonumber, I think you'll find that usage of autonumbers are not exactly encouraged by other members.

Some of the reasons for that, you are experiencing, others have to do with possible problems with concurrent users. To sum it up, I think there seems to be some kind of agreement that you should never use autonumbers when the number is supposed to have any business meaning.

I think you should look at other means of generating your unique number. Another member, MichalRed has a very good faq on the topic faq700-184. Sometimes generating the number using for instance DMax in the forms before update event might also be sufficient.

Roy-Vidar
 
Hi Guys,
Sorry i dint make myself too clear. The autonumber i was refering to is actually one that I generate from a function and not the DB autonumber. The number I generate is created on a button press. What i was hoping to do was to stop the user from pressing the escape key after this number had been generated because if the do the form is cleared and the number lost.
Re-reading the above I wonder if I've put the event in enough places - I've only put it on the keydown event of the form at the moment thinking this would be a catch all - one poster did actually tell me to put it onto controls aswell. I'll give that a go and let you know how things go.
 
Appollo14,
There's no need to put the Keydown Event on all the controls of the form. Just the form is enough. However, any sub-forms will need there own as well.
Cheers.
 
Hi Edski,

Ive got the event on the form but it does nothing to help me - escape key still cancels inputs. Ive put the code in as stated ie

if keycode = vbkeyescape then
keycode = 0
end if

and set the keypreview to yes. Any ideas where i've gone wrong?

Regards,
Noel.
 
Did you put the code in the Form's On Key Down event?
Is the focus in the form when you press <Esc> ?
 
Yes Edski,

The form has focus and the code is on the keydown event.

Regards,
Noel.
 
Noel,
That's good. I needed to get the real dumb question out of the way first :-)

Now, can you go into the VB editor and click on the line
if keycode = vbkeyescape then
then hit F9. Open the form and press <Esc>. Access will take you into the VB editor again and stop at the red dot (hopefully). Does it? If yes, press F8 to continue. Does it go to the next line? If not, what is the value of keycode?
(F5 to continue with the code execution).
Any luck?
 
Should you not be successful thru the ESC trapping approach, perhaps take a look at how and when you create your number (and the other implications of "missing" a number...).

For instance, could it be possible to generate it at a later time? Using the before update event of the form to generate it, or perhaps perform both a validation and save operation on the whole record thru your "create number" button? Still you'd probably have challenges when a record is deleted (if allowed).

More thoughts on the ESC path:
There are more ways of undoing than the ESC key, for instance hitting the Undo button or using CTRL+Z (last one could be trapped with keycode/shiftmask using something like this:

[tt]If KeyCode = vbKeyZ And (Shift And acCtrlMask) <> 0 Then
KeyCode = 0
End If[/tt]

(I know it's been asked and answered, but it might sound like you're using the KeyUp event and not the KeyDown - just to be 100%;-))

Else there are probably also more ways of canceling a record (and the users are often very good at finding such...).

You could also look thru the code and see if any other events issues an undo command.

Roy-Vidar
 
Hi Guys,

I'm feelin a little sheepish at the mo. I did have the event on the wrong one. I'd actually got it on the KEY PRESS event and not on the KEY DOWN.

Thanks for your perseverance.

Regards,
Noel.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top