Obviously there's no simple solution ! But your suggestion implemented
on the form's undo (or keydown) could do the trick.
Actually, I said there *might* be a simple solution. that would involve
KNOWING that the current control have been changed..and as I said if we
can't use the "oldvalue", then we are in trouble. If we can..then the
solution will yield itself, and should not be difficult..
(and, as you will see, this issue of oldvalue does NOT matter, and THEREFORE
the solution is easy!!)
Before reading your answer, I made some tests with a simple subform
containing two bound textboxes. If I change one of the control's value
then hit "Esc", I noted that :
- the control's Keypress event enters with the value 27 (just to make
everybody sure that the form's KeyAscii = 0 is not in effect);
- the control's Undo event don't fire;
Of course!...the instant we start grabbing the keyproess, then those events
are toast, and will not fire. (at least in the case of the Esc key anyway).
Remember, the undo event for a control will ONLY fire if you use Esc key, or
the un-do button the tool bar, but does NOT fire if you use the edit->undo
typing. further, the event is ONLY of use for a single control that has the
focus.
If I enter a value in the first textbox then a value in the second then
hit Esc, both values are resetted and the controls Undo event dont fire.
Yes..the above makes sense. (because we are trapping the esc key, those
events will not fire). further, if you had 50 controls on a screen, you
don't expect 50 undo evens to fire???? (hint: only the control that has the
focus will fire the undo event).
Since each control does have a undo event, and there is a cancel option. So,
in theory, we could add code to each text box, combo box etc..on a
form....such as:
Private Sub Description_Undo(Cancel As Integer)
Cancel = True
End Sub
The problem with the above is that we would have to code this for each
button..and, that not a solution at all. In fact, I did not even suggest the
above, since I do NOT consider it a reasonable solution (but, that was
always a possible fall back position here...and I had assumed this was
obvious).
So, a few things:
We are going to stick to using the keydown event, as it is the best (and
first) event to trap keystrokes.
(so, don't bother with keyspress event)
We really don't want to have to place code in each text box as above
The code that should work a form is as follows:
Keydown event for the FORM...and make SURE that key preview is set = yes....
If KeyCode = vbKeyEscape Then
KeyCode = 0 ' kill the key
On Error Resume Next
Screen.ActiveControl.Undo
End If
That is all we need. In thinking about the above, it really don't matter if
we execute a control.undo command more then once (that is why I talked about
needing to CHECK if the control has been modified...it turns out we don't
have to check). The above makes this easy as pie....