Calculation not updating (1 Viewer)

Newbie8

Banned
Local time
Today, 07:42
Joined
Jun 29, 2019
Messages
30
I have several textboxes that uses afterupdate events to perform calculation. Let's say I initially enter "1" in txtNum1, "2" in txtNum2, and txtTotal = "3." Then I realize I made a mistake in "txtNum1," and meant to enter "4." I change the value to "4", but txtTotal still stay at "3". What are some possible solutions to get txtTotal to update instantly. I tried forcing a refresh using requery, but it didnt do anything for me.
 

bob fitz

AWF VIP
Local time
Today, 15:42
Joined
May 23, 2011
Messages
4,721
What is the "Control Source" property setting for txtTotal
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 09:42
Joined
Feb 28, 2001
Messages
27,175
AfterUpdate might be the wrong event because the form's .AfterUpdate only fires after you save the form's content. Changing a value doesn't do an update. "Update" in a form context is quite specific and involves recordset changes.

If you want this to trigger at the level of controls, then fire on the .LostFocus event of each relevant control. That would fire if you clicked in the text boxes in random order.

If it is a complex computation, you can make a subroutine in the form's class module to perform the computation and then call the sub from each of the relevant controls' .LostFocus events. It might also help if for those elements that will participate in the computation that you give them default values of 0. If there is a default value in place then you could even just have an inline statement like

Code:
[txtTotal] = CStr( Val(  [txtNum1] ) + Val( [txtNum2] ) )

Note that without default values in place, you would have a more complex computation because of having to worry about the text boxes being empty or blank at the time.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:42
Joined
Feb 19, 2002
Messages
43,264
The only event that works for calculations such as these is the FORM's BeforeUpdate event.

When you put this code in a control's Before or After update event, you need to have it in at least TWO event procedures and you need to be prepared for calculation errors since for any new record, one of the two controls will ALWAYS start out as null unless you set a default.

The Form's BeforeUpdate event is the last event to run before a dirty record is saved. It ALWAYS runs. It can NEVER be bypassed. If a record is dirty when the form closes or focus moves to a different record, Access will ALWAYS run this event. Make it your friend. It is the best, most reliable event in which to include validation and calculation. There are always cases where you want to see the calculated value earlier, the best solution for that is to put the calculation in your query (keeping in mind that one value will always be null for new records so you have to test carefully).
 

Gasman

Enthusiastic Amateur
Local time
Today, 15:42
Joined
Sep 21, 2011
Messages
14,270
Hi Pat,

I think your post might not reach the O/P, due to this post today.?

https://www.access-programmers.co.uk/forums/showthread.php?t=306530

The only event that works for calculations such as these is the FORM's BeforeUpdate event.

When you put this code in a control's Before or After update event, you need to have it in at least TWO event procedures and you need to be prepared for calculation errors since for any new record, one of the two controls will ALWAYS start out as null unless you set a default.

The Form's BeforeUpdate event is the last event to run before a dirty record is saved. It ALWAYS runs. It can NEVER be bypassed. If a record is dirty when the form closes or focus moves to a different record, Access will ALWAYS run this event. Make it your friend. It is the best, most reliable event in which to include validation and calculation. There are always cases where you want to see the calculated value earlier, the best solution for that is to put the calculation in your query (keeping in mind that one value will always be null for new records so you have to test carefully).
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:42
Joined
Feb 19, 2002
Messages
43,264
I think perhaps he expected too much for the money he pays us.
 

Users who are viewing this thread

Top Bottom