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

Acrobat form calculations

AdobeUser
Registered: Aug 22 2007
Posts: 24
Answered

Hello, I am having a problem using calculations in an Acrobat form, using ver 8.1.2 pro, in Windows XP. I am rather new to the calculations feature of Adobe Acrobat, but can see it is a powerful tool.

I have a form with two columns the first column is the Odometer Reading column and the second is the Miles Traveled. I need the user to submit the odometer reading when they stop their vehicle and I need the second column to calculate the amount of miles traveled each time they update the entry below. The form has 18 rows in each of the columns.

I figured out that this should be a simple text field and should perform a simple calculation (field2-field1), in the Odometer Reading column. This part is no problem and it works great. (In my situation in the second Miles Traveled field the simple calcuation is (or2-or1), in the third Miles Traveled field, the simple calcuation is (or3-or2) and this pattern is followed all the way down the column.)

What I have discovered is the problem is that as odometer entries are added to the column, the calculation is performed and the results are added in the appropriate Miles Traveled column, as desired, but in addition, another negative number is added to the Miles Traveled field just below. This number seems to be a negative of the Odometer Reading field entry, but it is added to the Miles Traveled column. This in itself is not a big issue, but the number seems to stay until the next odometer reading is added and then it performs the calculation and adds this negative number to the field below again. If the entire form is always completed, it seems when the form runs out the fields it works fine and this negative result does not appear. The problem is, this form will have variable entries, one time there may only be two odometer reading entered and other times there might be 18 odometer readings entered. This negative number prints if the form is printed and cannot be deleted manually.

Is there a way to keep this additional negative number from appearing on the form in the Miles Traveled column? This is especially important if no further entries will be made on the form. Is it possible to have a field for this number to appear which in invisible, but still have the desired results displayed in the proper position?

Thanks AdobeUser

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Just think about how your calculation script for each field is being computed for each entry. Since there is no entry, the value is null or zero, you are subtracting the previous row value from the current row value or subtracting from zero.

You will need to write a custom calculation script to to only perform the calculation for a row if the odometer reading for the row has a value.

The following code can be used in each Miles Traveled field and you only need to change the 'Index' value to the row number.
var Index = 1; // change for columnfunction Milage(indx) {// function to compute milage from current index and previous indexvar milage = ''; // variable for reusltsif(this.getField('or' + indx).value != 0 & indx != 1) {// compute if current index milage is not zero and not first indexmilage = this.getField('or' + indx).value -  this.getField('or' + (indx - 1)).value;}return milage; // return computed milage};// compute milage using Index valueevent.value = Milage(Index);

George Kaiser

AdobeUser
Registered: Aug 22 2007
Posts: 24
Thank you for the quick reply.

I am totally new at working with this, but tired copying the language suggested, but it would not calculate.

When you say I must change the 'Index' value to the row number what does this involve? I believe I can do this, but just need a few more pointers. I appreciate your guidance. Thanks again.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You can not use the 'simplified field notation', you have to use the 'Custom calculation script' option. See [link;http://www.acrobatusers.com/tutorials/2006/form_calculations]How to do (not so simple) form calculations[/url] by Thom Parker on how to enter the script.

I use the variable 'Index' to create the necessary field names to be used in the computation, this assumes your field names are structured for this type of naming.

From you post your odometer fields are named 'or1', 'or2', 'or3', etc to 'or#'. For the mileage between 'or2' and 'or1' you set the 'Index' value to numeric 2 so the ending field name is 'or2' ('or' + Index) and the starting field name is 'or1' ('or' + (Index - 1)). For the mileage between 'or3' and 'or2' you would use an Index value of 3. So with your naming convention the mileage for the nth 'or' field will use the nth value for the index value and use the current 'or' and the previous 'or' fields. If you did not name your fields in this manner, then this will not work and will require some modificaiton.

George Kaiser

AdobeUser
Registered: Aug 22 2007
Posts: 24
Hello again, I have not been having success with this script. This is what I have tried:

var Index = 2; // change for column
function Milage(indx) {
// function to compute milage from current index and previous index
var milage = ''; // variable for reuslts
if(this.getField('or1' + indx).value != 0 & indx != 1) {
// compute if current index milage is not zero and not first index
milage = this.getField('or2' + indx).value - this.getField('or1' + (indx - 1)).value;
}
return milage; // return computed milage
};
// compute milage using Index value
event.value = Milage(2);

This script would be custom calculation in the first miles traveled field which needs to be computed. With this, I do not receive an script error message, but when I test it in the form, I don't get the mileage results either. This one uses the index value of 2 as suggested. I probably have something wrong here. Can you advise? Thanks AdobeUser
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You have not copied the code as I posted it. You have the wrong field name computation in the 'if' statement, the field names in the computation, and the value passed in the function.

The only line that needs to be changed in the code I posted is the first line, and the only change for that line is the number.

All the rest of the code uses this number to calculate the field names for the current row and the previous row.

For row '2' the code is:
var Index = 2; // change for row///// do not change any code below this line//function Milage(indx) {// function to compute milage from current index and previous indexvar milage = ''; // variable for reusltsif(this.getField('or' + indx).value != 0 & indx != 1) {// compute if current index milage is not zero and not first index// compute current row (indx) odometer less previous (indx - 1) row odometermilage = this.getField('or' + indx).value -  this.getField('or' + (indx - 1)).value;}return milage; // return computed milage};// compute milage using Index valueevent.value = Milage(Index);

For row '3':
var Index = 3; // change for row///// do not change any code below this line//function Milage(indx) {// function to compute milage from current index and previous indexvar milage = ''; // variable for reusltsif(this.getField('or' + indx).value != 0 & indx != 1) {// compute if current index milage is not zero and not first index// compute current row (indx) odometer less previous (indx - 1) row odometermilage = this.getField('or' + indx).value -  this.getField('or' + (indx - 1)).value;}return milage; // return computed milage};// compute milage using Index valueevent.value = Milage(Index);

And so on.

George Kaiser

AdobeUser
Registered: Aug 22 2007
Posts: 24
Thank you very much, that did it, and it works great!
AdobeUser
Registered: Aug 22 2007
Posts: 24
Hi again, After applying the formula to the entire form, it is working great.

I have also had the form add up all of the results in the Miles Traveled Column, (TotalMiles) however, I have noticed that the last traveled distance is not added to the total unless I have reached the end of the form. As a work around, I have repeated my last odometer reading immediately after the actual entry. This causes the formula to result in zero, and then the calculation above is added to the total. I applied a simple calculation formula which adds the results from each miles traveled field to TotalMiles. Is it possible to modify the current formula to permit all totals to be added without repeating the last entry? Would this require a complex calculation to be performed or is a modification needed to the original formula? Perhaps the modification might be needed for the TotalMiles field. Again, any suggestions will be appreciated. AdobeUser
ozkami
Registered: Jun 30 2009
Posts: 1
I am new to Acrobat Adobe v9. I have made a form for invoicing clients.

I have two fields I want to multiply together for a total: "# of pages" x "Unit Price" and I want to create a calculation for the "Total" field.

I tried using the calculation for "product" and choosing the two fields, but then I get an error saying the fields don't exist.

Any advice? Please Help!
schiilligm
Registered: Dec 9 2009
Posts: 7
I am having trouble with a custom calculations. The fields will automatically calculate with random numbers until you press enter by the field. If you change a field above that is added to the total to adjust numbers it won't re-calculate the fields without pressing enter by the field. Is there a way to update calculations automatically without having to re-enter the fields? Is there a way to subtract 0 unless a number is entered?

var nSubTotal = this.getField("num_of_months").value;
if( nSubTotal > 0)event.value = ( this.getField("year_filing_Total").value ) /
this.getField("num_of_months").value;

var i = this.getField("12Average").value;
var j = this.getField("base_year_average").value;
event.value = i-j;

var nSubTotal = this.getField("num_of_months_2").value;
if( nSubTotal > 0)event.value = ( this.getField("base_year_Total").value ) /
this.getField("num_of_months_2").value;

var i = this.getField("12Average").value;
var h = this.getField("Base Year amount_12").value;
var j = this.getField("base_year_average").value;
event.value = (Math.max(g,i))-(Math.max(h,j));


Calculation for Number of Employees
var u = this.getField("Diff_year_average _and_base_year_emp_cnt").value;
var v = this.getField("Decrease_employees_transferred_or_laid_off ").value;
event.value = u-v-0;
try67
Expert
Registered: Oct 30 2008
Posts: 2398
A value is only entered when you exit the field. That's when the calculation take place.

What's the point of subtracting 0 from a value?

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