How can I trap clicking of 'Cancel'/hitting CTRL + Z?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • julietbrown
    New Member
    • Jan 2010
    • 99

    How can I trap clicking of 'Cancel'/hitting CTRL + Z?

    If user uses CTRL+S to save, instead of my Save buttons that is fine, because the before and after update subs run for both ways of saving.

    However, if user uses CTRL+Z, instead of my Cancel buttons, disaster strikes!
    In one form in particular, it's a big problem. By the time a whole record has been put in, if the user clicks Cancel quite a lot of processing needs to be undone, and it all works fine, but this code is in the Cancel sub. If the user uses CTRL+Z the Cancel sub is bypassed and none of the undo-processing is done.

    Trouble is, Cancel does not trigger 'before' or 'after update', or, as far as I can ascertain, any other Form event (I've put msgboxes in all sorts of hopeful places!), so there there seems nowhere to put my 'undoing' code.

    Can anyone suggest any way of trapping the CTRL+Z? I've messed about with KeyPreview and KeyDown, but I although I can make a 'beep' when CTRL is pressed I can't seem to pick up the Z as well, or stop the whole CTRL+Z being acted on?
  • Jim Doherty
    Recognized Expert Contributor
    • Aug 2007
    • 897

    #2
    Hi Juliet

    Set the KeyPreview' property of your form to 'Yes' then enter the following code in the 'KeyDown' property of your form. It will test for that combination and if true set the keycode to zero essentially cancelling out the keystroke.

    Code:
    Dim intCtrlDown As Integer
    intCtrlDown = (acCtrlMask)
    If intCtrlDown And KeyCode = vbKeyZ Then
    Msgbox "Ctrl+Z was pressed"
    KeyCode = 0
    End If

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32653

      #3
      I'm not sure I fully follow the logic of what you're trying to achieve here Juliet, but the following template should enable you to proceed. You probably won't need both Cancel and Ctrl-Z, but it's clear how to fix for your particular requirement.
      Code:
      'Form_KeyDown only runs if the form's .KeyPreview property is True.
      Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
          Select Case True
          Case ((Shift = acCtrlMask) And (KeyCode = vbKeyZ))
              'Your Ctrl-Z action here
          Case ((Shift = 0) And (KeyCode = vbKeyCancel))
              'Your Cancel action here
          End select
      End Sub

      Comment

      • julietbrown
        New Member
        • Jan 2010
        • 99

        #4
        Thank you very much. That's great. This is a nice bit of code for my 'how to' library!

        However, whenever one of you experts says "I'm not sure I fully follow the logic of what you're trying to achieve here Juliet", I usually start to think it's probably something mad I'm trying to do, and re-think the overall approach, as I did here.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32653

          #5
          Always a pleasure :)

          In truth, that is a wise reaction generally, though sometimes it's as simple as difficulties of communication across a forum. Talking directly is so much easier and clearer, and having the database in front of you to refer to, so much easier again. We nevertheless must deal with the forum interface in most situations, so we live with a bit of confusion from time-to-time.

          PS. If you've managed to find a more appropriate approach then I applaud you. I always see that as an important sign of growth, or development may be a better word.

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            There is a very simple, code-less, solution (6 characters) which can enable you to negate the Ctrl+Z sequence and that is the AutoKeys Macro. If you are interested, let me know. I'm not a hugh Macro fan, but AutoKeys is a specialized Macro, and I do not think that they are going anytime soon.

            Comment

            Working...