Page 1 of 1
how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 11:27 am
by CAsba
Hi,
I have 6 fields where the user enters opening bank balances for 6 banks. I need to add the values of the 6 fields to determine if the total is more than zero, which indicates that at least one field is now not "Balance pending". This info is necessary for the next coding step. I have tried
Code: Select all
put field"pg1" + field"pg2" + field"pg3" + field"pg4" + field"pg5" + field "pg6" into field "sum of balances"
and
Code: Select all
put sum(field"pg1", field"pg2") into field "sum of balances"
without success.
How to do it ? Please ?
Re: how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 11:47 am
by Klaus
Did a quick test and this works fine for me:
Code: Select all
...
put field "pg1" + field "pg2" + field "pg3" + field "pg4" + field "pg5" + field "pg6" into field "sum of balances"
...

- Bildschirmfoto 2022-12-05 um 11.48.29.png (18.95 KiB) Viewed 3103 times
Re: how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 11:57 am
by CAsba
I get error message 'operators +: error in left operand.
Could it be because some of the fields conents are not numeric ?
Re: how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 12:03 pm
by stam
yes

I was just going to say you should make sure there is no comma in the values you're trying to add (keeping in mind that the default decimal point is a period, not a comma as it is in many countries. Needless to say this only works on pure numerical values - but you probably beat me to it
The dictionary is always helpful - look up 'sum':
sum(<numbersList>)
where:
numbersList is
- a sequence of parameters, each a number or evaluating to a number or
- a single parameter which is or evaluates to
- a comma-separated list of numbers or
- an array containing only numbers
Re: how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 12:06 pm
by Klaus
CAsba wrote: ↑Mon Dec 05, 2022 11:57 am
I get error message 'operators +: error in left operand.
Could it be because some of the fields conents are not numeric ?
Why didn't you write this important info in your posting?
What stam said, you need to check if the field contains a number.
Re: how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 12:23 pm
by CAsba
Thanks guys, that's set me straight.
Re: how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 3:34 pm
by dunbarx
CAsba.
Do you know how to check that?
Code: Select all
If fld "pg1" is a number and fld "pg1" is a number and...
Craig
Re: how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 3:46 pm
by dunbarx
Just for the exercise, you should do that better, so that any field that is NOT a number is flagged for you.This is best done in a loop, something like:
Code: Select all
on mouseUp
repeat with y = 1 to the number of flds
if fld ("pg" & y) is a number then add fld ("pg" & y) to field "sum of balances"
else
answer "Field" && ("pg" & y) && "needs looking at"
exit repeat
end if
end repeat
end mouseUp
Craig
Re: how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 3:51 pm
by dunbarx
CAsba.
Of course there are likely other fields on the card that are NOT of the "pg" type. Can you see how to only examine those that are?
Craig
Re: how to add the values of a number of fields.
Posted: Mon Dec 05, 2022 7:12 pm
by jacque
Also it is good technique to have a space between the control type and the literal name:
field"pg1" should be: field "pg1"