If Me.dirty then Me.dirty = False

ions

Access User
Local time
Today, 03:28
Joined
May 23, 2004
Messages
823
Dear Access Expert,

Does the code

If Me.dirty then Me.dirty = False

always trigger the form's BeforeUpdate event so I can do my Validation rules?

Thanks
 
It triggers it ONLY if there is anything to update. If nothing has been done to the form, that code will not get past the If Me.Dirty because it already is false.
 
Thanks Bob.
 
Bob what is the difference between a form's After Insert and After Update events?

When would you use After Insert vs After Update?

Thanks
 
After Insert is distinct from AfterUpdate -- and it occurs only when you "dirty" a brand new record (as opposed to starting to edit an existing record). When you dirty a new record, you are "inserting" a new record and that is when BeforeInsert/AfterInsert events fires. So by the time you see your first character appear on the screen, both event already has happened.

One possible idea to help you see how events fire in sequence, other than looking in the help document -- there was a magic keyword that would get the right document that gives you list of event sequence but I can't remember it at the moment -- is to add messagebox to each event you're interested then futz with the form a bit to see how it fires and when.

Hope that helps.
 
Thanks Banana.

Let me know if I understood correctly.

Assume record is Dirty.

If Me.NewRecord = True and you Save ->Insert events will be fired.

If Me.NewRecord = False and you Save -> Just the Update events fire. The insert Events do not fire.

Thank you.
 
No, it's BEFORE the record is dirty.

Insert a cursor on the blank new record on a form. Press a key.

BeforeInsert fires.
AfterInsert fires.
Dirty fires.

(this is off the top of my mind, and I encourage you to test & validate this)
 
The sequence of events is:

Form BeforeInsert
Form Dirty
Form BeforeUpdate
Form AfterUpdate
Form AfterInsert

If you're updating an existing row, BeforeInsert and AfterInsert do not occur. BeforeInsert occurs on a new row the instant the user types any character or chooses a value from a bound combo box or list box.
 
Thank you TexasInParis.

Have you ever used the Insert Events for a practical purpose?
 
All the time in subforms.

Private Sub Form_BeforeInsert(Cancel As Integer)
If IsNull(Me.Parent.Pkey) Then
MsgBox "You must enter data in X (the parent table) " & _
before creating a Y (the subform table). Press Esc " & _
"to clear your edit.", vbInformation
Cancel = True
End If
End Sub
 
Ok I see.

But I don't quite understand.

Wouldn't it be better to do this on the On Enter event of the subform control and if the Parent.Pkey is empty set the focus to it.

That way the user doesn't have to hit Esc?

Thank you
 
Actually, Dirty doesn't get set, so telling the user to press Esc is unnecessary. After disabling the Form_Current event in Northwind 2007 Order Details, this works just fine:

Code:
Private Sub Form_BeforeInsert(Cancel As Integer)
    If IsNull(Me.Parent![Order ID]) Then
        MsgBox "You must enter Order information before you can select products."
        Me.Parent![Customer ID].SetFocus
        Cancel = True
    End If
End Sub

I have very rarely used the Enter event for anything. The one example I have in my books is to use Enter for the first control on the second page of a multi-page form to align to the top of that page.
 

Users who are viewing this thread

Back
Top Bottom