Change in a field does not trigger "afterupdate" event

mafhobb

Registered User.
Local time
Today, 07:05
Joined
Feb 28, 2006
Messages
1,249
Hi all,

I've got a form with several fields that get updated regularly. These fields have a "Before Update" event that changes the value of another field (called "DateModified")

Code:
Private Sub Rudder_3D_Rate_BeforeUpdate(Cancel As Integer)
    On Error GoTo BeforeUpdate_Err
    
    'Set bound controls to system date
    DateModified = Date
BeforeUpdate_End:
    Exit Sub

The "datemodified" fields basically gets updated every time one of the original fields gets modified, so that way I know when the change was made. This works well and I can see how the "datemodified" field changes immediately after any original field is modified.

To add more functionality to the database, I am now trying to send an automatic e-mail to the people who needs to get notified of the change. First, however, I just want to get a message box to verify that the code works so I have added the following code to the "afterupdate" event to the "datemodified" field:

Code:
Private Sub DateModified_AfterUpdate()
MsgBox "Need to send e-mail to PS"
End Sub

However, nothing happens. I can see the "datemodified" field getting modified, but the message box does not show up. I have tried adding this to a "change" and "beforeupdate" events, but nothing...

What is happening? Why are these events being ignored even when the field does get automatically updated?

Help would be greatly appreciated...I have been scratching my head for a while...

mafhobb
 
The on change and afrter update only apply if the said control has the focus, in your case it doesn't. You need to do something with the control that performs the origninal update.

David
 
I see.

I can do that. I have 39 fields that cause the "datemodified" to change. Is there any way to simplify what I need to do instead of adding the same piece of code (send e-mail to such and such people) to all 39 fields?

mahobb
 
If you are using a bound form then you could use the OnCurrent event to compare the original value tothe current value and respond accordingly. I Think. Don't use bound forms much.

Maybe someone else has a better solution.

David
 
You could use a subroutine and call it every time you have an update.
 
Hi Insane,

I think I like the subroutine solution better. I do not have much experience with them, though.

Do you know of any place I can find a good example?

mafhobb
 
If I understand this I need to:

1- Create a class module for the form that contains the code that I need. Let's call that module "Specs Update". I assume the code needs to start with "public sub e-mail" or something similar
2- Call the "e-mail" sub in the afterupdate event of the fields that update the "datemodified" field.

Am I in the ballpark (This will be my first module...)?

mafhobb
 
..or...

Should I just add a "public sub" at the end of the code for that form and then simply call that sub form anywhere in that code?

Can I do that?

mafhobb
 
I see.

I can do that. I have 39 fields that cause the "datemodified" to change. Is there any way to simplify what I need to do instead of adding the same piece of code (send e-mail to such and such people) to all 39 fields?

mahobb

Use the Dirty method and the BeforeUpdate event of the form.

If Me.Dirty Then
Me.DateModified = Date
End If

Before and AfterUpdate events:

The recordsource of the form is available to the Form BeforeUpdate event but not to the Form AfterUpdate event.

ie BeforeUpdate: The following works

If Me.Dirty Then
Me.DateModified = Date
End If

AfterUpdate: The above will not work.
 
If you use the Before Update event of the form you do NOT have to test to see if the form is dirty. The Before Update event ONLY fires if the form has been "dirtied."
 
Hi Insane,

I think I like the subroutine solution better. I do not have much experience with them, though.

Do you know of any place I can find a good example?

mafhobb


Public Sub Update_Mod_Stamp
[DateModified] = Date
MsgBox "Need to send e-mail to PS"
End Sub
On the individual controls, paste Update_Mod_Stamp
If you do not get results, change sub to function. By definition, the Function in this case is incorrect to use because you are not returning a value but sometimes Access just needs it to give you what you want.
 
Hey, that worked.

I've got it!

Thanks for all the help

mafhobb
 

Users who are viewing this thread

Back
Top Bottom