Avatar of munequito
munequito
Flag for United States of America asked on

AfterUpdate event executes prior to the code in its AfterInsert event

Hi Experts,

I am stuck and need some help. I have an MS Access form I will call the parent form. In its detail section I have two subforms I will call the header subform and the detail subform.

The problem I am having trouble understanding is this: When I INSERT a brand new record in my header subform, the code in its AfterUpdate event executes prior to the code in its AfterInsert event. This does not sound right to me.

Any help or insight would be appreciated.
Microsoft Access

Avatar of undefined
Last Comment
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

8/22/2022 - Mon
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

Well, AU does occur before AI ... just how it works


http://office.microsoft.com/en-gb/access/HP051867611033.aspx



The BeforeUpdate and AfterUpdate events for the controls on the form and for the new record occur after the BeforeInsert event and before the AfterInsert event.
Jeffrey Coachman

<the code in its AfterUpdate event executes prior to the code in its AfterInsert event. This does not sound right to me.>
Sounds perfectly logical to me...
Update the record, then insert it

This is because sometimes you are simply editing records, hence, only an update happens, but no insert happens.
Again, sounds perfectly logical to me...

You can see the Access help files for more specific info on each event.


Can you explain why:   "This does not sound right to me" ?

Remember, it is not inherently clear what your Header and Footer Subforms are doing...?

JeffCoachman
munequito

ASKER
I have been thinking of a form insert the same way I think of a database insert; that is, I am thinking that you do one or the other but not both.

So, is there a way to distinguish a form update on a newly inserted record from a form update on an existing record?
Your help has saved me hundreds of hours of internet surfing.
fblack61
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

sure, like so

Private Sub Form_AfterUpdate()
    If Me.NewRecord = True Then
               ' new rocord
    Else
          ' existing record
    End If
End Sub
munequito

ASKER
Thanks DatabaseMX, I used your logic in my afterupdate and afterinsert events as shown below. However, in both cases the value of Me.newrecord = 0.

Private Sub Form_AfterInsert()
    If Me.NewRecord = True Then
          ' new record - audit
          Call AuditFormInsert("Customer", Me, "CUSTID", custid)
    Else
          ' existing record - do not audit
    End If

End Sub

Private Sub Form_AfterUpdate()

    If Me.NewRecord = True Then
          ' new record - do not audit
    Else
          ' existing record - audit
          Call AuditFormUpdate("Customer", Me, "CUSTID", custid)
    End If


End Sub
ASKER CERTIFIED SOLUTION
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jeffrey Coachman

We are getting confused now.\
What are these new "Calls"

Certain things can be checked for in AfterInsert/AfterUpdate.

Did you read the link MX Posted?

Perhaps you could *First* take a step backs and explain what you have here, and what your ultimate goal here is ...?
Perhaps there is a more efficient approach...

But I'll let MX handle this from here...


JeffCoachman
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
munequito

ASKER
Thanks DatabaseMX, that resolved my issue. I truly appreciate your help.
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)

You are welcome ...

mx