A very interesting problem. Using Northwind's Orders and Order Details tables as a test bed:
(1) Created tblFormula, which looks like this:
dteStart strFormula strDescription
8/1/94 TestA([UnitPrice])
8/10/94 TestB([UnitPrice])
(2) Using your examples, created two functions
Code:
Function testA(UnitPrice As Currency) As Currency
'formula to use when date is from 8/1 - 8/9/94 (see tblFormula)
testA = ((UnitPrice) * 0.56) + 4.56
End Function
'
Function testB(UnitPrice As Currency) As Currency
'formula to use when date >= 8/10/94 (see tblFormula)
testB = (3.05 + (UnitPrice)) * 0.32
End Function
(3) Created code which::
· Evaluated the OrderDate of each record,
· Determined which formula to apply based on the OrderDate
· Created a string incorporating the OrderDate (e.g. "TestA(#8/8/94#)
· Used the Eval function to return the value of the formula applied
against UnitPrice, which was used to represent vPriceOfGas.
· Displayed the output in the debug window.
(4) Using this method it would possible to add additional formulae, just
by adding additional records to tblFormula and creating corresponding
Functions.
(5) If you'd spend a few minutes setting this up (only need to create tblFormula
then copy the code to a new module), you could then test it from the debug
window with: ? myformula(#8/9/94#, #8/25/94#) <enter>
Code:
Function MyFormula(enterstartdate As Date, enterenddate As Date) As Currency
Dim db As Database
Dim rs As Recordset
Dim rs2 As Recordset
Dim strSQL As String
Dim dteHold As Date
Dim TB, NL, Header, Body, fmt
Dim UnitPrice As Currency, widget As Variant
'
fmt = "$###,###,##0.00" ' Define money format.
TB = Chr(9) ' Define tab.
NL = Chr(13) & Chr(10) ' Define newline.
Header = "Order" & TB & "Date" & TB & TB & "Price" & TB & TB & "NewPrice" & Chr(13)
Set db = CurrentDb
strSQL = "SELECT dteStart, strFormula, strDescription" _
& " FROM tblFormula" _
& " ORDER BY dteStart DESC;"
Set rs = db.OpenRecordset(strSQL)
strSQL = "SELECT DISTINCT Orders.OrderID, Orders.OrderDate, Products.ProductName, [Order Details].UnitPrice" _
& " FROM Products INNER JOIN (Orders INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID) ON Products.ProductID = [Order Details].ProductID" _
& " WHERE (((Orders.OrderDate) Between #" & enterstartdate & "# And " _
& "#" & enterenddate & "#));"
'
Set rs2 = db.OpenRecordset(strSQL)
rs2.MoveFirst
Debug.Print Header
Do While Not rs.EOF
dteHold = rs2!OrderDate
rs.MoveFirst
Do While dteHold > rs!dteStart
rs.MoveNext
If rs.EOF Then
rs.MovePrevious
Exit Do
End If
Loop
UnitPrice = rs2!UnitPrice
'build the statement for evaluation
'note that rs!strFormula is going to return either "TestA()" or "TestB()"
'next line returns either "TestA(" or "TestB("
widget = Left(rs!strFormula, InStr(rs!strFormula, "("))
'next line incorporates the UnitPrice and closes the parenthesis
widget = widget & UnitPrice & ")"
'next line is the key to the process since it applies
'the appropriate formula, based on date and unit price
widget = Format(Eval(widget), fmt)
'this prepares a string to be printed in the debug window
Body = rs2!OrderID & TB & rs2!OrderDate & TB & TB _
& Format(rs2!UnitPrice, fmt) & TB & TB & widget & TB & Left(rs!strFormula, 5)
Debug.Print Body
rs2.MoveNext
If rs2.EOF Then Exit Do
Loop
rs.Close
rs2.Close
db.Close
Set db = Nothing
'
End Function