Escape to undo form update

M

MikeB

One can press "Esc" twice to undo updates to a record displayed in a
form. Or have a button that calls DoCmd with some parameters (the
button wizard takes care of this). Unfortunately, this doesn't undo
changes to unbound controls (unless I'm missing something).

Aside: I'm not sure if an unbound control is the same as a calculated
control. I think it is, since in an unbound control, I'm responsible
for determining the value displayed in the control.

I can call a sub to calculate the correct value in the unbound control
from the button_click event. To reset the control after double escape,
all I can figure out is that I have to set the form property so the
form gets passed an event notifier for all keypress events and then I
have to write an event handler for the form to look at all (each and
every) keypress on the form. And some convoluted code to make sure
that I'm trapping double "Esc" and nothing else.

Am I understanding this correctly? If so, are the keypress events
still passed through my code after I do stuff, or do I have to call
something special to pass on the keypress and the key that was
actually depressed? I'm particularly concerned with "special" keys
such as Alt, Cntrl, Fn, etc....


Thanks.
 
K

Ken Sheridan

A calculated control is an unbound control, but an unbound control is not
necessarily a calculated control. A calculated control 'pulls' in its value
by means of an expression as its ControlSource property, e.g. = [NetPrice] *
(1+[TaxRate]), so it alaways refelects the values from which it is computed.
An unbound control might have its value assigned by code in an event
procedure for instance, or it might be a control such as a combo box used for
navigating to a record. The value in a control of this type does not persist
if the form is closed and then opened again at the same record (unless the
event procedure is executed again).

Calculated controls should take care of themselves in the event of a form
being undone, so its unbound controls whose values are assigned to them by
code or by a user editing the value in the control are what concerns you
here. If you merely want to empty the controls then its very simple:

1. Set the Tag property of each relevant control to UndoMe.

2. In the form's Undo event procedure put:

Dim ctrl As Control

For Each ctrl In Me.Controls
If ctrl.Tag = "UndoMe" Then
ctrl = Null
End If
Next ctrl

Ken Sheridan
Stafford, England
 
M

MikeB

A calculated control is an unbound control, but an unbound control is not
necessarily a calculated control. A calculated control 'pulls' in its value
by means of an expression as its ControlSource property, e.g. = [NetPrice] *
(1+[TaxRate]), so it alaways refelects the values from which it is computed.
An unbound control might have its value assigned by code in an event
procedure for instance, or it might be a control such as a combo box used for
navigating to a record. The value in a control of this type does not persist
if the form is closed and then opened again at the same record (unless the
event procedure is executed again).

Calculated controls should take care of themselves in the event of a form
being undone, so its unbound controls whose values are assigned to them by
code or by a user editing the value in the control are what concerns you
here. If you merely want to empty the controls then its very simple:

1. Set the Tag property of each relevant control to UndoMe.

2. In the form's Undo event procedure put:

Dim ctrl As Control

For Each ctrl In Me.Controls
If ctrl.Tag = "UndoMe" Then
ctrl = Null
End If
Next ctrl

Ken Sheridan
Stafford, England

Ken, Thank you so much. I completely missed that Undo event on the
form. That was exactly what I was looking for.

Now a weird one. I was pretty sure that one had to hit the "Esc" twice
to get a record changes undone, but now I notice that I simply have to
hit the "Esc" once and then the Undo event is driven. Am I
hallucinating?
 
J

John Spencer

If you are on a form and in a control that has changed the first Escape
will undo the change to the control. And the second will undo the
changes to the form.

If the control with the focus has not been changed or does not have an
associated value (e.g. a button) then the first escape will undo the
changes to the button.

As to your mental state - I am not medically qualified to make a judgement.

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================
 
M

MikeB

John, point well taken regarding my mental state. However, I feel
reassured that there *are* instances where I have to press Esc twice
for the record changes to be undone.

Now I have a more technical question.

If I have made changes to the control's value and I press Esc is the
Undo event for the form triggered as well? Is there any way to tell
the difference between whether the undo event is triggered by a
control undo or by a form (or record?) undo?

Thanks.
 
J

John Spencer

I don't know. Perhaps you can test and see. And then report your
results back to the newsgroup.

'====================================================
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================
 
M

MikeB

I don't know. Perhaps you can test and see. And then report your
results back to the newsgroup.

Fair 'nuff. Right now I'm slightly overwhelmed by the number and
complexity of the various options available, but I'll try and work my
way through them in a somewhat methodical approach.

I just found the AllowUpdate which I think can replace my home-written
code to lock and unlock the controls. Then there is the AllowPreview
which passes keypress events to the form before passing them to the
control (and I've not yet determined that Esc isn't trapped before the
form (or the control) sees the keypress event). Then also the form
and the control both have an Undo event and then there is the issue
with whether the focus is in the control at the time I press the Esc.
Pretty complicated.

I've pretty much fallen into the abyss here, the complexity of stuff
is just spiraling away from me. I'm seriously looking at my spare
change to see if perhaps I can pay someone to help me write this,
which will be sad as I'm simply doing this to learn and for my little
school chess club.

One of the other school coaches has forked over $99 and bought pre-
made software. Perhaps I'll capitulate as well.
 
J

John Spencer

Well, $99 if the software works for you would be well worth it.

On the other hand, if you are using this as an exercise to learn about
Access perhaps you don't want to count the dollar cost.

Yes, Access has a lot of complexity, BUT you don't have to use all of
it. Personnally, I am still learning. About the time I get up to a
good level of understanding, MS introduces a new version with new
capabilities. I don't always agree with them when they add new options
- some of them seem to make life harder instead of easier. I do feel
that I need to get a basic understanding of the new options.

That said, I am just starting to play with Access 2007.

Anyway, keep coming back for advice. Most of us love to help.

Perhaps, I'll get a chance to investigate if the undo event gets
triggered if you undo a control. I would think the answer would be NO,
but I have to check.

'====================================================
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
'====================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top