Expand my Community achievements bar.

How to add several numeric fields into one Total?

Avatar

Level 2

Hi,

I have been through some of the forum posts and attempted to do this unsuccessfully. How can I add several numeric fields, and total that number in one field?

To take it a step further, I need to find an average value as well. I have about 18 numeric fields I want to add into one field.

Thanks,

2 Replies

Avatar

Level 10

Hi,

You can achieve this using FormCalc or Javascript.

One aspect to bear in mind is how you have named the numeric fields. For example if you gave them the same name "amount", then in the hierarchy you would see the instances of each field: amount[0], amount[1], amount[2],...,amount[17].

Now using FormCalc the following script in the calculate event of the total field will sum all of the instances:

$ = Sum(amount[*])

This uses the * wildcard.

Now you will need to extend this if the objects are in different subforms. There is an example here for referencing objects: http://assure.ly/kUP02y. Download the PDF example and open in Acrobat/Reader.

Hope that helps,

Niall

Avatar

Level 5

Hi,

Assuming all your fields have a different name ( e.g. TextField1, TextField2.....)

You code could look something like this

this.value = TextField1.value + TextField2.value + TextField3.value + etc..........

and place this code in the calculate event of the field you want to total.

To get the average you just need to divide the answer using standard coding techniques

this.value = totalField.value / TotalNumberOfFields

and place this code in the calculate event of your average field.

If you fields have the same names then it becomes a little more complicated and although I have placed this on the calculate event, it could easily be run by a button on the exit even of a field.

( I have assumed all the fields are in the same subform for the code)

hope this helps

Regards

Malcolm