473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?
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

5 5795
Jim Doherty
897 Expert 512MB
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
32,556 Expert Mod 16PB
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
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
32,556 Expert Mod 16PB
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
8,834 Expert 8TB
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

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

Similar topics

13
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to...
4
by: Deano | last post by:
Alot of my forms are bound. I would like to offer a Cancel option so that they can make as many changes as they like and still Cancel out without making any changes. I have one idea of how to...
6
by: Alex K. | last post by:
My MDI form has several child forms in maximized state. Pressing Ctrl-F6 causes next child to show, which I don't like. How can I cancel standard Ctrl-F6 behaviour? Thank you
1
by: amrita | last post by:
hi i have a table in web form wth name table1.i need that wen i click on cancel button all the textboxes in that table becomes empty.ny ideas how to do it,doin in onclien side wud be greatly...
0
by: John Dalberg | last post by:
I have master/detail datagrids where each master row has a detail datagrid. I have included a linkbutton in the child datagrid's footer to add a new row. If I click on the add link in more than one...
4
by: Martin | last post by:
I have a form with two submit buttons: <Save> and <Cancel>. When the user clicks on <Save>, I want to execute a validate function. When he clicks on <Cancel>, I want the form to submit without...
0
by: tim.cavins | last post by:
I am trying a GridView for the first time and it seems to be something that should be EXTREMELY simple. I cannot get the Update or Cancel when trying to edit a row to work. When I click on the...
3
by: Bob Alston | last post by:
Anyone know how to disable or redefine the Ctrl - hotkey which deletes a record? I have a multi page form that uses 14 records, each record handles 1-2 pages of the 18 page form. Occasionally ,...
7
by: John Gault | last post by:
I experimented with a snippet of JavaScript that will display a "Please Wait" message and graphic while the results of a cgi script is running (the script grabs a bunch of data and formats it in a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.