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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculated field in a Form

Status
Not open for further replies.

Centurin

IS-IT--Management
Oct 19, 2008
7
0
0
US
I have a number of price fields in a form. I want to create a command button that will add them up and put them in a total field. I realize that I can do this with a query, but I need to be able to change the total field if I wish.

My main problem is that I haven't done code in Access in quite a few years and I don't remember much of the syntax. I tried something like

Set PriceTotalBox = Price1Text + Price2Text

However, I know the syntax isn't right. How do I reference objects in the code? I know it uses a dot, like PriceTotalBox.form, but I'm not sure. I'm not even sure where I can name the form object itself.

In addition, because the values are currency, will there be a problem with the addition?

Any help on this would be greatly appreciated. Thanks
 
You could use the before update of the form or after update of individual text boxes to call a function like:
Code:
Sub SumPrices()
    Me.PriceTotalBox = Nz(Price1Text,0) + Nz(Price2Text,0)
End Sub
Your table structure doesn't sound normalized.

Duane
Hook'D on Access
MS Access MVP
 
Do you mean a single form or a continuous form?

A single form would be something like:

[tt]Me.PriceTotalBox=Nz(Me.Price1,0) + Nz(Me.Price2,0) ... + Nz(Me.PriceN,0)[/tt]

Set is only used for objects. You can use bang or dot, dot brings intellisense into play, some people say bang is safer. You can use Me in the form's own module or Form!NameOfForm. If you are not storing the total, that is, the control in which the total is to appear is unbound, you can set the control source to:

= Nz([Price1],0) + ...

Note: No Me

Currency is not a problem unless you have rounding requirements.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top