Dirty Expression

R

Rose

Throughout a database, I have used a command button in forms to close the
form when clicked. While it has worked for a long time, recently when I add
a new Command button to do the same thing in a new form, I get the message
“You entered an expression that has an invalid reference to property Dirtyâ€
How do I figure out the code that is causing the problem? The old ones are
still working I am just not able to add new ones that close a form. However,
I can add new command buttons that open forms, print, etc, so I am not sure
where I went wrong.
Thanks for your help.
 
L

Linq Adams via AccessMonster.com

You don't give your code for closing the form, but I assume it involves
checking to see if the current record is, in fact, dirty and forcing the
saving of the record before closing the form. This is done to ensure that any
validation code be run. This code usually follows the syntax

If Me.Dirty Then
Me.Dirty = False
End If

DoCmd.Close

I suspect that your form is unbound, and unbound forms don't have a Dirty
Property, and thus will pop an error with this code. I understand that this
code is used by the command button wizard in ACC2007 when generating a
"CLOSE" button. Go into the code window to the OnClick sub for your button
and remove the

If Me.Dirty Then
Me.Dirty = False
End If

part of the code.

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via AccessMonster.com
 
A

Allen Browne

Unbound forms don't have a Dirty property.

Presumably you have some code that you call from command buttons on several
forms throughout the database. So you have a function like the one below in
a standard module, and you call it either by setting the close button's On
Click property to exactly this (don't replace the "Form" part):
=CloseForm([Form])
or by setting the property to:
[Event Procedure]
and then adding this line to the code:
Call CloseForm(Me)

Public Function CloseForm(frm As Form) As Boolean
On Error Goto Err_Handler
'Purpose: Close a form, saving a record if necessary
'Return: True if the form closed.
'Note: Avoids this bug: http://allenbrowne.com/bug-01.html

'If it's a bound form, save if necessary.
If frm.RecordSource <> vbNullString Then
If frm.Dirty Then frm.Dirty = False
End If
'Close the form.
DoCmd.Close acForm, frm.Name
'If we got here without error, the form closed successfully.
CloseForm = True

Exit_Hander:
Exit Function

Err_Handler:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume Exit_Handler
End Function
 
J

J. Denby

Your information was very helpful. Has this problem been fixed yet. I have
many users that rely on the Command Button Wizard to work correctly. They
are NOT programmers. Any help would be greatly appreciated.
 
A

Allen Browne

No. This has not been fixed, and is not likely to be (given how long the
problem has existed.)

Just add the lines to the code.
 

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