View Full Version : Function as a query field?


lution
10-25-2007, 06:57 PM
I've inherited a database that used to have a total field in it. I've removed the total but now find myself having to recalculate the total from existing fields in 37 places including 22 queries. Is there any way to create a function that can access the fields in the current records and return the total?

if this was on a form I could do something like:

public function curGetTotal() as currency

curGetTotal = me.curCost + me.curPenalty + me.curForfeiture

end function


But calling this from a query says it doesn't recognize me.

Thanks

pbaldy
10-25-2007, 07:02 PM
If the values will always be in that form and the form will be open:

curGetTotal = Forms!FormName.curCost + Forms!FormName.curPenalty + Forms!FormName.curForfeiture

Otherwise, you'd probably need to change the function so it accepted the values as parameters.

lution
10-25-2007, 07:05 PM
but if I wanted to use the function as part of a query so the query returned the total as one if it's properties?

pbaldy
10-25-2007, 07:18 PM
Presuming the form isn't available, you'd need to modify the function to accept parameters:

public function curGetTotal(Value1 as currency, Value2 as currency) as currency

And pass the parameters when you call it:

NewField: curGetTotal(Field1, Field2)