detecting that a dirty form is no longer dirty

G

Guest

I've tried the technique in article 210334 to determine
that a form is dirty because I'm trying to control the
enable state of a "Save" command button -- enabled if form
is dirty, disabled if not.

I've noticed a post that using the Timer event as this
article suggests doesn't work reliably, and I've
encountered some strange behaviour myself, presumably from
the Timer event firing every second for each open form.
This same post suggested using the Dirty event for the
form. I found that I can detect that a form becomes dirty
using the dirty event. What's giving me trouble is if the
user starts to enter or update data and dirties the form,
and then presses escape to discard the changes, which un-
dirties the form -- the Dirty event does not allow me to
detect that the form is no longer dirty.

Does anyone have any suggestions? I can detect an un-
dirtied form via the Timer event, but that takes me back
to some tempermental code.

Thanks in advance...
 
K

Ken Snell

You can always check the Dirty state of the form by this VBA code step:

If Me.Dirty = True Then
' form is dirty
Else
' form is not dirty
End If
 
G

Guest

Ken --

Thanks for the quick reply. I've got a follow-up
questions.

Your code sample is what I've run from the Timer event,
but with the problems that using Timer creates. If you
run that code from the form's OnDirty event, the event
fires when the form becomes dirty, but not after the form
un-dirties.

Can you think of a way to detect that form is no longer
dirty without relying on the timer event?

Thanks,
Mark
 
P

Paul Overway

There is no "OnUndirty" event...the timer is your only option. But what's
the harm in leaving the control enabled? If the form isn't dirty, you're
not making any changes. IMO, the record selector serves the exact same
purpose as your save button...and it knows when the form is dirty or not.
Why reinvent the wheel?
 
K

Ken Snell

I assume that you know which controls can be changed and make the form
dirty. So use the AfterUpdate event of each of those controls to run the
code that you wish to run when the form is made Dirty.
 
P

Paul Overway

The trouble with that is if the user undoes changes (i.e., Esc, Undo), there
is no event to trap the change in state. The form isn't dirty anymore, but
the button would indicate that it is.
 
K

Ken Snell

Yes, I see that my focus is a bit off base from the poster's original
question -- namely, how to determine when the form is no longer dirty.

There is a form Undo event that may be useful.

However, a solution may be to use the OnExit event of each control to run
the code that enables/disables the button. Even if the person is headed
towards clicking on the save button when the form isn't dirty because an Esc
or something was done in that textbox, as the control is exited, the code
would run before the button can get the focus and thus have its Click event
occur.
 
P

Paul Overway

Form Undo event? Is that new? Hmm...have to check my 2003 version.

Your OnExit solution would work better than OnTimer...could be a lot of
coding though (depends on number of controls).
 
B

Brendan Reynolds

The Undo event has been around for longer than that. I'm not certain, but I
*think* that forms got an Undo event in Access 2000 while controls got their
own Undo events in Access 2002.

In my experience, though, these events are not useful. To implement control
enabling/disabling dependably, we need Before Undo and After Undo events,
but we have only one. I can't remember now whether the Undo event fires
before or after changes are discarded, but I do very clearly remember trying
to make it work and eventually giving up on it.

Using the Timer event works, but you need to use a short timer interval,
about 1/4 second, and I don't feel comfortable hogging all that processor
time for a non-essential feature.

--
Brendan Reynolds (MVP)
http://brenreyn.blogspot.com

The spammers and script-kiddies have succeeded in making it impossible for
me to use a real e-mail address in public newsgroups. E-mail replies to
this post will be deleted without being read. Any e-mail claiming to be
from brenreyn at indigo dot ie that is not digitally signed by me with a
GlobalSign digital certificate is a forgery and should be deleted without
being read. Follow-up questions should in general be posted to the
newsgroup, but if you have a good reason to send me e-mail, you'll find
a useable e-mail address at the URL above.
 

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