Solved

AfterUpdate event executes prior to the code in its AfterInsert event

Posted on 2012-03-12
9
605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 
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 - Microsoft MVP, Access and Data Platform) 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

738 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