Afterupdate event

G

Guest

Hi,

I have a simple form with a single record recordset, using Access 2003. For
some reason if data is changed the afterupdate event doesn't fire. The
beforeupdate works fine.

I would assume if the beforeupdate fires, the afterupdate should also fire
(Unless beforeupdate cancel set to true)?

Thanks in advance

swas
 
A

Al Campagna

swas,
If the value of the field is changed manually, both Before and After events should
fire.
Otherwise, "copy exactly" your code for both events, and include it in your post.

If a field value is changed programatically (via code)...
YourField = "ABCDEF"
the AfterUpdate event will not fire.
In that case...
YourField = "ABCDEF"
YourField_AfterUpdate
would cause the AfterUpdate code to fire.

Make sure that the Field Name referred to in the BeforeUpdate sub (which you imply does
fire)
is the same Field Name as in the AfterUpdate Sub.
 
G

Guest

Al,

Thanks for the response. My code is as follows:

Private Sub Form_AfterUpdate()

If Me.NewRecord Then
'Add node to main tree
Me.Parent.AddSiteNode = sID

End If

End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.NewRecord Then
'Add node to main tree
' Me.Parent.AddSiteNode = sID

End If

End Sub

I had the AddSiteNode property set initially in beforeupdate, which adds the
new record to a treeview control on the parent form. Because the new record
hadn't been updated it caused an error (The if then statement in beforeupdate
obviously doesn't do anything, but I left it there to put a debug stop
against).

So I shifted it to the afterupdate event but as I say doesn't fire.

I'm using form_afterupdate not the field / control event, but I don't see
this should matter...?

I don't quite understand the difference between the two examples you show
unless you mean the second example is manually entered by the user.

Thanks for your thoughts.


swas
 
A

Al Campagna

swas,
The BeforeUpdate and AfterUpdate of the Form only fires when the Form itself is
Updated... either by code, or a Refresh, or Requery, or moving to another record.
 
A

Al Campagna

swas,
Disregard my preceding partial post. It was sent before it was completed.
The Form AfterUpdate and BeforeUpdate event only fires when the form is Updated. That
occurs when the form is closed, or you move from one record to another.
I use this code to on every record to set a DateOfCreation field and a DateOfLastEdit
field.

Private Sub Prefix_AfterUpdate()
If (IsNull(DOC)) Then
DOC = Date
DOLE = Date
Else
DOLE = Date
End If
End Sub

As I enter data in the form, those values are not updated immediately, but they do
update when I close the form, or move to another record.
I'm using form_afterupdate not the field / control event, but I don't see
this should matter...?
Yes, it does. The control Before and After fire immediately upon changes in that
field's data.

If you want to "see" the results of your code, while you're on that new record, then
place your code in the AfterUpdate event of an appropriate text control.
 
A

Al Campagna

swas,
Well, I don't think I could have messed this up any more if I tried....
This code runs "immediately" upon editing my Prefix field on the form...
I set it to Prefix/AfterUpdate to match your question.
Private Sub Prefix_AfterUpdate()
If (IsNull(DOC)) Then
DOC = Date
DOLE = Date
Else
DOLE = Date
End If
End Sub

But...

Actually, I use the BeforeUpdate event of the form to run that code, just like your
code (using AfterUpdate would have the same results as far as updating DOC and DOLE)
Private Sub Form_BeforeUpdate(Cancel As Integer)
If (IsNull(DOC)) Then
DOC = Date
DOLE = Date
Else
DOLE = Date
End If
End Sub

Using this code, the 2 fields are only updated when leaving the form, or closing.
Hope I didn't confuse you too much...
 
G

Guest

Al,

Thanks for the reply(s). Yes I understand your comments, so that means we
are either both very smart - or both totally insane... but it's a fine line
regardless :)

I managed to solve my problem by using the beforeupdate event and modifying
the other code that was generating the error. I still don't understand why if
the beforeupdate event fires, the afterupdate event may not always (No cancel
done in beforeupdate). I was always of the understanding that the event
sequence was predetermined.

I guess its one of those catches for new / self taught programmers.

Again, thanks.


swas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top