Is it Escape, Undo or CancelEvent?

M

Mike Boozer

I have a number of controls that are "enabled=false" when control "new
record" is clicked. I also have some subform controls locked and/or not
visible. Its getting rather complicated and I wonder how much "code" you can
actually put on a control before it pukes. Anyway, when user enters data in
the form field and then changes his mind, I want to mimic the keyboard
escape key and re-enable all of the contols with my custom "cancel" button..
An MVP helped me with part of the solution in which I can tag the controls
and then call the tag on Cancel_Click to re-enable them.This works great
except that the data the user entered is still there. I simply (hah?) want
to return the form back to its original state before the "New record"
control was ever clicked. Everything enabled, Locks set back to original
condition, any data entered Undo, etc. Sort of like the user never even
clicked the "New Record" control. If I could, the cancel button would be a
combination of "keyboard escape key", "acUndo" and re-enabling and setting
locks. Can you code the keyborad escape key in VB? Do you have to basically
build your own "cancel button" to take into account all of the events that
are occurring? I'm a hardcore newbie and can't stop trying. Thanks.
 
W

Wayne Morgan

Me.Undo will act like pressing the Escape key. However, you'll still be at a
new record, just a blank new record. So, by your description I suspect you
would still want the controls set the same until you move to an existing
record. This can be checked in the Form's Current event. This event fires
each time you go from one record to another. In the event you can test for
the record being a new record (If Me.NewRecord Then) and set your controls
appropriately. Use the Else part of the If statement to set them the other
way.

You can put more code in than you probably want to type before it "pukes".
If you are getting into long routines, try looking for ways to shorten it.
Using the Tag option as you mentioned is one way. You could then use a loop
to go through all the controls and check for the Tag option. This should
save a lot of typing.
 
T

TC

(snip)
Anyway, when user enters data in
the form field and then changes his mind, I want to mimic the keyboard
escape key and re-enable all of the contols with my custom "cancel"
button..

As I will eventually tire of saying to the heartless world: there is *no
way* you can do this 100% reliably with an Access form!

Say you start a new record, go to a required new field, type some
characters, and then erase those characters by deleting them, or overtyping
them with spaces. Now try to click your Cancel button. Oops: the button
click event >won't fire<. This is because the focus is trapped within the
dirty, blank, required field. The only way out is to click the form's close
(x) box, or press Esc (manually) once to undirty that field, or twice to
undirecty the whole new record. And if you have to click Esc >before< you
can click your Cancel button, there is no point >having< the Cancel button!

To repeat, there is no way you can code a 100% reliable "Cancel my unsaved
changes" button in an Access form, unless that form is based on fields which
have >no validation - Access or user - of any kind whatever<.

HTH,
TC
 
T

TC

Further to this, I should have clarified that there is no way you can do it
using a command button >on the form<. You can do it using a command button
on a toolbar<, because a toolbar button does not remove the focus from the
current field. But most posters expect to have the button on the form - not
the toolbar.

TC
 
T

Tim

Have you thought about using an unbound form. This allows
the user to complete the form but nothing is saved until
you hit a confirm button, which the updates the underlying
table(s). You can then incorporate a cancel button which
reloads the form as it was when you first started. If you
need to display basic information, you could bring this in
with DLookUp fields.
 
G

Guest

I like Wayne's suggestions, but instead of using an If-
Then-Else statement to set my controls enabled property I
generally just use a boolean test

ctrl1.enabled = NOT me.NewRecord
ctrl2.enabled = NOT me.NewRecord

I've also used a technique where I but all of the code for
enabling and hiding controls in a single subroutine, which
I pass one or more variables. This way, whenever I make a
change that affects any of the controls, I just have to
call a single routine.
 
T

TC

He still can't code a 100% reliable Cancel button if he has any BeforeUpdate
validations!

HTH,
TC
 

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