Me.Dirty = false

G

Guest

Good Morning,

I have been using me.dirty = false to force a save. It seems to work well,
but I have a form, and after I am sure I have entered a record I want to add
records to another table. So I enter code into the AfterUpdate Event, but so
I don't add the records unless I ensure the record has been added, I have
code in the Before Update event. I assumed when I used the Me.Dirty = false
the before update event would fire and then the After update would fire. If
the insert doesn't crash it all works good.
The specific problem I am having is when the user adds a record and that PK
is already used, I want to in the Before update pop up a message telling them
that Unit ID is taken please try again.

Thanks
 
D

Dirk Goldgar

Rick Vooys said:
Good Morning,

I have been using me.dirty = false to force a save. It seems to work
well, but I have a form, and after I am sure I have entered a record
I want to add records to another table. So I enter code into the
AfterUpdate Event, but so I don't add the records unless I ensure the
record has been added, I have code in the Before Update event. I
assumed when I used the Me.Dirty = false the before update event
would fire and then the After update would fire. If the insert
doesn't crash it all works good. The specific problem I am having is
when the user adds a record and that PK is already used, I want to in
the Before update pop up a message telling them that Unit ID is taken
please try again.

Thanks

This would normally be fairly straightforward, but I'm a little
uncertain what you're doing because you said, "after I am sure I have
entered a record I want to add records to another table." If you're
talking about updating a table different from the form's recordsource,
the advice I'm giving below is probably off target. For the moment, I'm
going to assume you're talking only about allowing or cancelling updates
to this form's recordsource table.

If the form is Dirty and you execute Me.Dirty = False, the form's
BeforeUpdate event will fire, and *if* the update is successful, the
AfterUpdate event will fire. So if the record can't be saved -- whether
it's because you cancel the update, or for some other reason -- the
AfterUpdate event won't fire.

You can cancel the update from the BeforeUpdate event, using code along
these lines:

'----- start of example code -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

' Note: I'm assuming for the moment that you only want
' to prevent the update if you're adding a new record.
' That could be wrong. If it is, then remove the
' "If Me.NewRecord" test.

If Me.NewRecord Then

If Not IsNull( _
DLookup("UnitID", "YourTable", _
"UnitID=" & Me!UnitID))
Then
Cancel = True
MsgBox "Sorry, that Unit ID has already been used. " & _
"Please try again.", _
vbExclamation, "Duplicate Unit ID"
End If

End If

End Sub
'----- end of example code -----

That code ought to prevent the addition, but if the update is cancelled,
you'll have an error raised at the point where you execute the statement
"Me.Dirty = False", so you have to trap the error at that point and
ignore it.
 
G

Guest

thanks Dirk

Dirk Goldgar said:
This would normally be fairly straightforward, but I'm a little
uncertain what you're doing because you said, "after I am sure I have
entered a record I want to add records to another table." If you're
talking about updating a table different from the form's recordsource,
the advice I'm giving below is probably off target. For the moment, I'm
going to assume you're talking only about allowing or cancelling updates
to this form's recordsource table.

If the form is Dirty and you execute Me.Dirty = False, the form's
BeforeUpdate event will fire, and *if* the update is successful, the
AfterUpdate event will fire. So if the record can't be saved -- whether
it's because you cancel the update, or for some other reason -- the
AfterUpdate event won't fire.

You can cancel the update from the BeforeUpdate event, using code along
these lines:

'----- start of example code -----
Private Sub Form_BeforeUpdate(Cancel As Integer)

' Note: I'm assuming for the moment that you only want
' to prevent the update if you're adding a new record.
' That could be wrong. If it is, then remove the
' "If Me.NewRecord" test.

If Me.NewRecord Then

If Not IsNull( _
DLookup("UnitID", "YourTable", _
"UnitID=" & Me!UnitID))
Then
Cancel = True
MsgBox "Sorry, that Unit ID has already been used. " & _
"Please try again.", _
vbExclamation, "Duplicate Unit ID"
End If

End If

End Sub
'----- end of example code -----

That code ought to prevent the addition, but if the update is cancelled,
you'll have an error raised at the point where you execute the statement
"Me.Dirty = False", so you have to trap the error at that point and
ignore it.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 

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