in an access database i have created a function which compares 3 fields in my query:
Fields:
CostMacDON
CostKentFRY
CostBurgKIN
TurnMacDON
TurnKentFRY
TurnBurgKIN
CustMacDON
CustKentFRY
CustBurgKIN
Public function Eval(MacD, KentF, BurgK)
'Note: the real evaluation is a bit more complex, this is just an oversymplified example
if MacD>KentF and MacD>BurgK
Eval="MD is the highest"
else
Eval="MD is NOT the Highest"
endif
end function
to call the function in a access query view i use
Evaluate Cost: Eval(CostMacDON, CostKentFRY, CostBurgKIN)
==== ---- ---- ----
Evaluate Turn: Eval(TurnMacDON, TurnKentFRY, TurnBurgKIN)
==== ---- ---- ----
Evaluate Cust: Eval(CustMacDON, CustKentFRY, CustBurgKIN)
==== ---- ---- ----
BUT...with all this repetition of Cost/Turn/Cust, I was hoping to simplify the queries by adapting the function. After some research, it seems that I should restructure the tables which I really want to avoid. (as this is just a very small part of the puzzle)
I want the users to be able to add other field, so I would also like to avoid complex sql statements. SO.. I would like to call the function like this
Evaluate Cost: Eval("Cost")
Public function Eval(EvalType as variant)
Dim MacD as Variant (??)
Dim KentF as Variant (??)
Dim BurgK as Variant (??)
MacD= EvalType & "MacDON"
KentF= EvalType & "KentFRY"
BurgK= EvalType & "BurgKIN"
' this however gives me the names of the 3 fields, and I want to compare the content !?!
if MacD>KentF and MacD>KentF
Eval="MD is the highest"
else
Eval="MD is NOT the Highest"
endif
end function
Does anybody have an idea if this can be done?
You would have to open a recordset or use DLookup() aggregate function to pull data from table into function.
Probably also need to pass into function ID of record.
Public Function Eval(EvalType As Variant, varID As Variant) As StringExample with DLookup():
MacD = DLookup(EvalType & "MacDON", "tablename", "ID=" & varID)