Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

basic addition on a form

Status
Not open for further replies.

tonyx666

MIS
Apr 13, 2006
214
GB
i have four fields, price1,price2,price3 and total

i made a form.. made four controls named as above.

i want the total to automatically be the total of the three prices, and change whenever anything is updated.. like in excel..

can i use an afterupdate command on the price1,2 and 3 controls, or can i just use one bit of code in the total control.. ?
 
Would recommend you use an UNBOUND field for the calculation. So, if Total is a field in your table, don't use.

Instead, just drag an unbound field from the toolbox. In the data source row type:

=[price1] + [price2] + [price3]

If you think there is a possibility of a NULL being in any of those fields use this formula (good practice anyway)

=NZ([price1]) + NZ([price2]) + NZ([price3])

The NZ converts nulls to "0" so formula doesn't bomb
 
tonyx666,

In the Control Source property of the total field:
Code:
= Nz([price1], 0) + Nz([price2], 0) + Nz([price3], 0)
HTH,

Ken S.
 

If you do need to save it in the table, you need to make sure it updates.

In the AfterUpdate event for prices1, price 2 and price 3 add the code

price4= Nz([price1], 0) + Nz([price2], 0) + Nz([price3], 0)


A more efficient way would be to have this code in its own functions:

private sub PriceUpdate()
price4= Nz([price1], 0) + Nz([price2], 0) + Nz([price3], 0)
end sub

and then in the Afterupdate of Price 1,2 and 3 add

PriceUpdate

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top