Access Event for Escaping out of a form

Posted on 2012-09-10
Last Modified: 2012-09-16
Hi all,

I have an label that displays a certain text  when a field value on a bound form is true, and a different text when the value is false . The label works as a toggle. When the label is clicked the fld value turns true and the lable says "On". Clicked again, the field value is false and the label says "Off".

My problem comes when someone hits the Esc key in the middle of updating.

If they click the label when the value is false it goes from "Off" to "On". This is before the form is updated. If they hit the Esc key, the value reverts back to False, but the Label stays "On".

Does anyone know how to trap for the Esc key? Or how to handle the situation?

Any help would be greatly appreciated.

Question by:eantar
    LVL 75

    Expert Comment

    by:DatabaseMX (Joe Anderson - Access MVP)
    Like so:

    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If KeyCode = vbKeyEscape Then KeyCode = 0
    End Sub

    Be sure the Form's KeyPreview property is set to Yes.

    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    My guess is that you could also force a save after the lable is clicked :
        Docmd.Runcommand accmdsaverecord

    Why not just use a Checkbox control for True and False (where you have many more events, like "AfterUpdate", where things like this typically happen)
    ...Instead of clicking the label
    (which may not be obvious to a new user)
    LVL 5

    Author Comment

    But guys, is there no way to trap for escaped entry? No event? Current doesn't fire. I wondered if there was a new event in 2010. Just curious.
    LVL 5

    Accepted Solution

    Hey guys,

    Found a solution. Converted the label to a text box, which requeries when you hit the Esc key. The text box's control source is set to a function that does all the work and set's the text accordingly.

    All the best,
    LVL 5

    Author Closing Comment

    This was the only way I could make it work.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
    Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
    Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
    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…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now