+ Reply to Thread
Results 1 to 7 of 7

Userform and "ESC" - how to prevent?

  1. #1
    Registered User
    Join Date
    01-30-2009
    Location
    Estonia
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19

    Userform and "ESC" - how to prevent?

    When user presses "ESC" key on an open userform, data he has changed in controls (simple textbox for example) gets resetted. Right? I'm seeing such behavior in Excel 2003. How about other versions?

    I'd like to see some documentation about what exactly is going on, and how to prevent such unexpected feature (that forces data loss), but could not find.

    I mean that i have no commandbutton with cancel=true. By default I want to keep the data user has entered/changed. Unfortunately this data reset finds place before any trappable event, or am i wrong?

    I have not found any working way to capture keypresses on userform level. Userform has _KeyPress and other events, but those are not firing? If there is one, that would be helpful. Perhaps even on system level with some APIs? Altough that sounds way too complicated for a problem that should need only one little-known flag set. Perhaps there is such property somewhere?




    --
    Please pardon my language, this is my first public writing in english.
    Last edited by hannes001; 02-06-2009 at 12:38 PM. Reason: Got enough hints. Thanks everyone!

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Userform and "ESC" - how to prevent?

    Setting the Cancel property of a CommandButton to True will cause that button's code to be run when the Escape key is pressed.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Registered User
    Join Date
    01-30-2009
    Location
    Estonia
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19

    Re: Userform and "ESC" - how to prevent?

    Yes,
    but unfortunately all changes get lost before that event fires. At least for me. Now, if i could capture data before it gets lost, or cancel that kind of action completely..

    I think i could use a global array for all values i want to keep, and update this array on every _change or such event, but even such ugly workaround might get even uglier - this unfortunate data reset causes those events fire and i need to somehow determinate whether this event was caused by user input or "ESC".. I'm talking theoretically now, i don't even want to try this approach.


    But thanks for taking your time. I'll try to patch together a simple example for illustration.


    EDIT:
    Now, tabula rasa method is always helpful. I'm sorry, I was little too quick before. Headache didn't help either. This resetting apparently does not apply to whole form as i said before, only to currently active input. That is not half as bad as before I had too much code around that acted upon some input and mixed things up.

    But unwanted behaviour is this nevertheless. I guess i could trap _KeyPress event (for every input..) and cancel "ESC" there. Or move focus. Suddenly it seems that it should be very well documented, because different controls (textbox vs checkbox) seem to act differently.. I try again.
    Attached Files Attached Files
    Last edited by hannes001; 02-02-2009 at 11:56 AM.

  4. #4
    Registered User
    Join Date
    08-18-2008
    Location
    Manila
    MS-Off Ver
    Office 2003, Office 2007
    Posts
    26

    Re: Userform and "ESC" - how to prevent?

    Hi,

    I believe that the pressing the ESC key while the object in focus (i.e. an object that is used for inputing data) will trigger an UNDO function. Good examples are textboxes, the cells in an Excel sheet and the box where you type your reply here in ExcelForum.

    I still also can't find decent documentation about this ESC Dilemma.

    Anyway, I can help you trap the ESC Keypress. Suppose you have a Textbox, use the Keydown event:

    Please Login or Register  to view this content.
    Keypress event is not reliable when using VBA.
    Last edited by LordMarcus; 02-03-2009 at 01:37 AM.

    __________
    LordMarcus

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Userform and "ESC" - how to prevent?

    Hello hannes001,

    To do this is requires advanced level programming called Sub-Classing. This is a technique that allows you eavesdrop on messages being sent to a particular window and either trapping the message or passing it on. In this case the message of interest is IDCANCEL. This is in the wParam word sent along with the WM_COMMAND message to the TextBox.

    This behavior is by design. The TextBox can operate in 2 modes: Single line or Multi-line. In single line mode the Enter key determines when the input is complete. In multi-line mode, we want to include the Enter key as the new line character. This is where the Escape key comes in. Pressing the Esc key in multi-line mode signals the end of input. In single line mode, the Esc key functions as Cancel operation. Deleting the current input and restoring the previous input, if any.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    01-30-2009
    Location
    Estonia
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19

    Re: Userform and "ESC" - how to prevent?

    First, thanks to LordMarcus. It was useful tip, i didn't know that you can change KeyCode there... Anyway this is a workaround i can deal with. Altough i might have dozens of such textboxes, some generated on runtime... I personally don't like lots of repeating code. At least it is simple and somewhat readable.

    But,
    Leith Ross, i'm pondering whether i dare to ask for a example? You make it sound like this technique requires pages and pages of complex API wrappers, several class modules and loads of arcane tricks.. In that case, let it be. For now.
    I already have numerous classes, pages of spaghetti code and several bruises on my forehead.

    Thanks for explanation though, now it sounds rather logical. What other controls might behave in such way? I'd be thankful for a recommendation for a book/webpage about VBA events and userforms where such rather little-documented-features, unexpected or not, are dissected.

  7. #7
    Registered User
    Join Date
    08-18-2008
    Location
    Manila
    MS-Off Ver
    Office 2003, Office 2007
    Posts
    26

    Re: Userform and "ESC" - how to prevent?

    Your most welcome hannes001.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1