By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

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

P: 99
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?
May 6 '10 #1

✓ answered by NeoPa

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.
Expand|Select|Wrap|Line Numbers
  1. 'Form_KeyDown only runs if the form's .KeyPreview property is True.
  2. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  3.     Select Case True
  4.     Case ((Shift = acCtrlMask) And (KeyCode = vbKeyZ))
  5.         'Your Ctrl-Z action here
  6.     Case ((Shift = 0) And (KeyCode = vbKeyCancel))
  7.         'Your Cancel action here
  8.     End select
  9. End Sub

Share this Question
Share on Google+
5 Replies


Jim Doherty
Expert 100+
P: 897
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.

Expand|Select|Wrap|Line Numbers
  1. Dim intCtrlDown As Integer
  2. intCtrlDown = (acCtrlMask)
  3. If intCtrlDown And KeyCode = vbKeyZ Then
  4. Msgbox "Ctrl+Z was pressed"
  5. KeyCode = 0
  6. End If
May 6 '10 #2

NeoPa
Expert Mod 15k+
P: 31,494
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.
Expand|Select|Wrap|Line Numbers
  1. 'Form_KeyDown only runs if the form's .KeyPreview property is True.
  2. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  3.     Select Case True
  4.     Case ((Shift = acCtrlMask) And (KeyCode = vbKeyZ))
  5.         'Your Ctrl-Z action here
  6.     Case ((Shift = 0) And (KeyCode = vbKeyCancel))
  7.         'Your Cancel action here
  8.     End select
  9. End Sub
May 6 '10 #3

P: 99
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.
May 15 '10 #4

NeoPa
Expert Mod 15k+
P: 31,494
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.
May 15 '10 #5

ADezii
Expert 5K+
P: 8,638
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.
May 15 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.