Question Me.Dirty Runtime Error

froggiebeckie

Registered User.
Local time
Today, 12:15
Joined
Oct 11, 2002
Messages
104
Morning, all.
In a single user db, I've created an unbound form with Start and End date fields and several buttons to open reports.
My intention was to let my co-worker open the form, enter the Start and End dates once, then open several different reports, all using the same date range.
Everything works great, until I try to close the form using a "Close" button.
If I just "X" out of it, all is well,
But if I try to close the form using a "Close" button, I get a Runtime Error, related to Me.Dirty.

This is what highlites (in Bold & Italics) :

Private Sub Command15_Click()
On Error GoTo Err_Command15_Click


If Me.Dirty Then Me.Dirty = False
DoCmd.Close

Exit_Command15_Click:
Exit Sub

Err_Command15_Click:
MsgBox Err.Description
Resume Exit_Command15_Click

End Sub

I could avoid the issue by just "X"ing out, but I'm curious as to why I can't use the Close Form button.
Any idea why?

Thanks,
BeckieO
 
What is the error message, that you receive? If the Form's dirty property is changed it returns an empty string. Why do you want it to set to False? Just Close should work..
 
pr2-eugin,
I put the button on the form, using the command button wizard, the way I've done countless times before. I've found that when using the "X" to close, some folks are prone to hit the wrong "X" and accidently close the whole database, instead of the form, so I try to put a Close button on all of my forms.
I don't want the form to save, but apparently just entering the dates triggers the On Dirty.
The error message is
Run-time error '2455':
You entered an expression that has an invalid reference to the property Dirty.

Then gives me the option to End, Debug or Help
 
@froggiebeckie

Hi!

I think your problem is related to using

Code:
If Me.Dirty Then Me.Dirty = False

in an Unbound form. Please try and close the form without testing for me.dirty and see if error goes away when you close the form or you can use a bound form and your me.dirty test will work.
 
I open the form.
Doesn't seem to matter whether or not I put the cursor in one of the date fields.
If I try to close it with the button, I get the error.

I've also tried to remove the If Me. Dirty...code. That just stops the button from working.
 
I'm thinking it's because my form is unbound.
The same button on bound forms works just fine.
 
Well, poooooo.
Sorry to take up your time.
While I was reading my previous response, I realized that the Event Procedure was inserted by the wizard so,
Delete the Event Procedure from ON CLICK
and replace with a macro to close form.

Works like a charm.

Thanks again,
BeckieO
 
Although some people recommend using
If Me.Dirty Then Me.Dirty = False
as a way of forcing Access to save a dirty record, I don't like it because it is confusing. When I first saw the code, I thought the programmer was actually stopping the save rather than forcing it. So my suggestion is to use:
DoCmd.RunCommand acCmdSaveRecord
which is clear, to save the current record unless you run into a situation where it causes a problem.
And as you have found out, it doesn't work on unbound forms.
 
I said - others recommend the Me.Dirty solution.

My decision to stay with "DoCmd.RunCommand acCmdSaveRecord" is based on clarity of code plus how often do you think you have code running in a form that doesn't have focus? I'd guess that it would be pretty rare. You would need to be automating one form from another and actually updating records in the other form. I'll pretend I'm from Missouri on this one. Show me a real life example. I will say that I have run into a situation where the DoCmd method threw an error but Me.Dirty didn't. That's not the same thing as DoCmd not working and not throwing an error.

The important point in Allen's article is that you MUST save the record explicitly before attempting to close the form. This is real and it happens in any form that has a close button that doesn't explicitly save before closing. If you don't explicitly save and an error prevented the record from being saved, the change will be silently discarded and the user will never know.
 

Users who are viewing this thread

Back
Top Bottom