AfterUpdate not working

T

Tony_VBACoder

With Access 2002 SP1, can someone tell me why the
AfterUpdate event is not firing for a Text Box when I have
a Command Button with the following code:

Private Sub btnSetDate_Click()

Me.txtDate = Now()

End Sub

The following event will not fire with the above
CommandButton Click event:

Private Sub txtDate_AfterUpdate()

MsgBox "The Time is " & Me.txtDate

End Sub
 
K

Ken Snell [MVP]

Programmatically changing or setting the value of a control *does not* cause
the AfterUpdate, BeforeUpdate, or Change event of that control to occur. If
you want the On After Update property's event procedure's code to run as
part of the Click event from the button, just call that code:


Private Sub btnSetDate_Click()

Me.txtDate = Now()
Call txtDate_AfterUpdate()

End Sub
 
S

Sandra Daigle

Hi Tony,

BeforeUpdate and AfterUpdate events do not fire when you modify the value of
a control via VBA. You would need to do one of the following:

a) Move the code to a separate procedure in the class module and call the
new procedure from both the AfterUpdate event of the textbox and from the
command button. This is my preference if there are more than one or two
lines of code that need to be duplicated.

b) duplicate the code in both places. Ok in my opinion if it is only one or
two commands.


c) call the event procedure from the command button's event procedure. I
don't like to do this because it murks up the call stack. Why is an event
procedure executing when the event didn't actually occur? In reality I don't
know that it matters but in my mind it keeps things organized.
 
G

George Nicholson

From the Help entry for the AfterUpdate event:

"Note: Changing data in a control by using Visual Basic or a macro
containing the SetValue action doesn't trigger these events for the
control."

You can work around this by explicitly calling the event from your code:

Me.txtDate = Now()
Me.txtDate_AfterUpdate

For this to work, you will need to change
Private Sub txtDate_AfterUpdate()
to
Public Sub txtDate_AfterUpdate()
 
T

Tony_VBACoder

Thanks...

Didn't event think of that.

-----Original Message-----
From the Help entry for the AfterUpdate event:

"Note: Changing data in a control by using Visual Basic or a macro
containing the SetValue action doesn't trigger these events for the
control."

You can work around this by explicitly calling the event from your code:

Me.txtDate = Now()
Me.txtDate_AfterUpdate

For this to work, you will need to change
Private Sub txtDate_AfterUpdate()
to
Public Sub txtDate_AfterUpdate()


--
George Nicholson

Remove 'Junk' from return address.





.
 

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

Similar Threads


Top