“Expenses” that contains a field named «AMOUNT», number float type
“Trips” that contains a field named «TIME», number float type
Both databases hold data for various years and have a «DATE» field formatted as MM/DD/YY.
I need to create a formula that will give me the sum of all «AMOUNT» records for a given year and divide that figure by the sum of all «TIME» records for the same year.
I am planning to put the result on a text display field on a form on “Expenses”
Without fully testing this myself, here is how I would approach this problem. If I simply want to assign the result to a variable for use in a form using a procedure I would first get the year to search either with a gettext or possibly a popup menu button on the form.
Next I would get the various related values from the Amount and Time fields of the two databases and total them and then divide them for a final result in a variable I call finalFigure.
let amountTotal=arraybuild(cr(),"Expenses","Amount",{yearvalue(Date)=val(theYear)})
amountTotal=arraynumerictotal(amountTotal,cr())
let timeTotal=arraybuild(cr(),"Trips","Time",{yearvalue(Date)=val(theYear)})
timeTotal=arraynumerictotal(timeTotal,cr())
letfileglobal finalFigure=amountTotal/timeTotal
You could also just daisy-chain this all together an use it as a formula for your Text Display Object as long as you have the proper year set in the theYear variable:
The Text Display should update any time you change the value of theYear and use showvariables theYear. As I said to start with, I have not fully tested this so you might need some adjusting.
The formula assumes you have set a variable (I called mine theYear) to the year you want to use in the formula. If it is a set value like “2019” you would just substitute that for theYear in the formula. If you have the year in some other variable or field you would use that in place of theYear.
What database are you running the procedure in? If it is the Trips database just try message Time to see if you get the same error. If you do I would guess the field name might have an invisible character in it like a leading or trailing space.
I have created a Text Display Field on a form in the Trips database and run message Time.
When the field is set for formula, the error message is “Expression contains an operand when an operator was expected” and when field is set to literal text, obviously, I get “message Time”.
You can not run statements like message in a formula. Just put in Time as the formula. To clarify what we are talking about here I am assuming you are using “Text Display Field” to actually indicate a Text Display Object. The field would be placed in the formula pane of the Text Display Object and the Options would be set to ‘Formula’. Some of the earlier code I suggested trying were statements that would be run from a procedure. Statements can contain formulas but formulas can not contain statements.
This “simple” calculation has added considerable value to Pan X for me so I appreciate your sticking with me until I could sort it out.
Cheers and stay healthy.