473,385 Members | 1,829 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,385 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 5793
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.