Solved

AfterUpdate event executes prior to the code in its AfterInsert event

Posted on 2012-03-12
9
555 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now