Solved

AfterUpdate event executes prior to the code in its AfterInsert event

Posted on 2012-03-12
9
580 Views
Last Modified: 2012-03-12
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
Comment
Question by:munequito
  • 4
  • 3
  • 2
9 Comments
 
LVL 75
ID: 37710682
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37710925
<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
 

Author Comment

by:munequito
ID: 37711083
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 75
ID: 37711112
sure, like so

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

Author Comment

by:munequito
ID: 37711228
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
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 37711272
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 37711290
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
 

Author Closing Comment

by:munequito
ID: 37711310
Thanks DatabaseMX, that resolved my issue. I truly appreciate your help.
0
 
LVL 75
ID: 37711413
You are welcome ...

mx
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question