These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

If Calculation

jpcutler85
Registered: Nov 16 2009
Posts: 38
Answered

I have field C that is the sum of either field A or field B, not the sum of field A + Field B.
 
I currently have (field A * 0.18) as a simple field notation in Field C. But if I then add in:
 
(field A * 0.18) (field B * 0.18) the calculation does not work. How do I write so that field C is a calculation of either Field A or field B?
 
Thanks!

My Product Information:
Acrobat Pro 9.3.1, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
What's the condition?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

jpcutler85
Registered: Nov 16 2009
Posts: 38
Sorry i dont understand what you mean by "condition"
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You wrote: "I have field C that is the sum of either field A or field B..."
This implies that there's a condition when it should be the one or the other, but you never say which condition that is.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

jpcutler85
Registered: Nov 16 2009
Posts: 38
Field C is either the sum of field A or B, depending on which field the user chooses. The user could choose Field A or Field B. Once chosen they input a value which in turn is multiplied by 0.18 and then added to Field C
try67
Expert
Registered: Oct 30 2008
Posts: 2398
So the condition is that the field is not empty?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
We know you have 3 fields - 'A', 'B', and 'C'. The user enter data into fields 'A' or 'B'. Your form then calculates field 'C''s value based on the the value of 'A' or 'B'. But you have not clearly described the how one determines which value, 'A' or 'B' to use. And you do not provide any guidance on what to do when both 'A' and 'B' have a value. Note that 'A or B' also includes the situation of entering data into both 'A' and 'B'.

We maybe confused by your use of the word 'sum' to describe mutually exclusive multiplications.

What do you want done under the following different conditions:


  • No value for both 'A' and 'B'
  • A value for 'A' and no value for 'B'



  • No value for 'A' and a value for 'B'



  • Values for both 'A' and 'B'

George Kaiser

jpcutler85
Registered: Nov 16 2009
Posts: 38
The user chooses which field to use "A" or "B"

To put it in actual terms Field "A" is: Delivery cost without installation Field B is: Delivery cost with installation. The user chooses which field they would like and enter a value which is determed by the user. This value is then * 0.18 which creates sum (total) in Field "C"

The user cannot enter values in both "A" and "B"

No value for both 'A' and 'B' - 'C' is 0
A value for 'A' and no value for 'B' - 'C' is A * 0.18
No value for 'A' and a value for 'B' - 'C' is B * 0.18
Values for both 'A' and 'B' - not possible

Hope that clears things up :)
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
The following custom calculation script will work in an Acrobat created form.
// define variables for field values var A_Name = "Afield"; // enter the name for field A between the quotation marksvar B_Name = "Bfield"; // enter the name for field A between the quotation marks// do not change any code below this lineevent.value = 0; // null out C fields valuevar nA_Value = this.getField(A_Name).value; // get the value of the A fieldvar nB_Value = this.getField(B_Name).value; // get the value of the B field// test for unlikely condition of field A and B having dataif (nA_Value.toString() != "" & nB_Value.toString() != "") {// alert to unusual conditionapp.alert({cMsg: "You can not have a value in both fields!", nIcon: 0, nType: 0, cTitle: "Data Entry Error"});}// check for A valueif(nA_Value.toString().length != 0) {// field C is value of field A * 0.18event.value = nA_Value * 0.18;} else {// then field B only has a valueevent.value = nB_Value * 0.18;}
Comments about the line of code or what the script is doing appear after the '//' character string.

George Kaiser

jpcutler85
Registered: Nov 16 2009
Posts: 38
Great Thanks! Works perfectly. Although a small issue. When you put a value in field A, and then a value in Field B a warning pops up saying "You can not have a value in both fields" but the value still remains in Field B. Is there a way to reset the value back to 0 in Field B? and vise versa?

Another issue I have run into is within my order form. The user clicks a button which then brings up the option to add qty. This qty is added to a field within the order form for the corresponding product. Within the order the form the qty field can also be modified without clicking the original button.

But my issue is, there is a second section where the qty can be chosen which is a field. So button A allows you to choose qty and field B also allows you to choose qty. How do I make it so that you can select button A to add qty as well as modify the qty field within the order form which in turn updates the qty in field B?

Hope that makes sense, if not i will try and explain it further.

Thanks again for your help!
jpcutler85
Registered: Nov 16 2009
Posts: 38
I have picked up another error in regards to your script. In order for the overall total to recognise a value in either AField or BField. a value must be entered in both.

AField = 1
BField = 0

If AField = 1
BField = "nothing"

the overall total does not recognise the calculation.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Your are right. Here is the revised code that performs only the calculation when there is only an entry in "A" or "B".
// define variables for field values var A_Name = "Field.A"; // enter the name for field A between the quotation marksvar B_Name = "Field.B"; // enter the name for field A between the quotation marks// do not change any code below this lineevent.value = 0; // null out C fields valuevar nA_Value = this.getField(A_Name).value; // get the value of the A fieldvar nB_Value = this.getField(B_Name).value; // get the value of the B field// test for unlikely condition of field A and B having dataif (nA_Value.toString() != "" & nB_Value.toString() != "") {// alert to unusual conditionapp.alert({cMsg: "You can not have a value in both fields!", nIcon: 0, nType: 0, cTitle: "Data Entry Error"});} else {// perform calculation since there is only one value entered// check for A valueif(nA_Value.toString().length != 0) {// field C is value of field A * 0.18event.value = nA_Value * 0.18;} else {// then field B only has a valueevent.value = nB_Value * 0.18;}}

George Kaiser

jpcutler85
Registered: Nov 16 2009
Posts: 38
Hi thanks for getting back to me but I sitll have the same problem :( the overall total will not appear unless a 0 value has been entered into the other field.

Is there a way to write it so that if a value is put in to field A, a value of 0 is put into field B and vise versa?

Thanks!