Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

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.
0
munequito
Asked:
munequito
  • 4
  • 3
  • 2
1 Solution
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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.
0
 
Jeffrey CoachmanCommented:
<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
0
 
munequitoAuthor Commented:
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
sure, like so

Private Sub Form_AfterUpdate()
    If Me.NewRecord = True Then
               ' new rocord
    Else
          ' existing record
    End If
End Sub
0
 
munequitoAuthor Commented:
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
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Yeah, sorry - forgot NewRecord  is False at that point.  So, do this instead:

Option Compare Database
Option Explicit

    Dim fNewRecord As Boolean

Private Sub Form_BeforeUpdate(Cancel As Integer)
    fNewRecord = (Me.NewRecord)
End Sub


Private Sub Form_AfterUpdate()

    If fNewRecord = True Then
          ' new record - audit
          MsgBox "New Record"
    Else
          ' existing record - do not audit
    End If
    fNewRecord = False
   
End Sub
0
 
Jeffrey CoachmanCommented:
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
0
 
munequitoAuthor Commented:
Thanks DatabaseMX, that resolved my issue. I truly appreciate your help.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
You are welcome ...

mx
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now