我有一个用Access VBA编写的函数。此功能的目的是执行简单的线性插值。它使用ZeroRates为输入的记录集中最近的“MaturityDates”日期插入指定日期的“ZeroRate”值。功能如下:
Function CurveInterpolateRecordset(rsCurve As Recordset, InterpDate As
Date) as Double
Dim Q As Long
Dim x1 As Date, x2 As Date, y1 As Double, y2 As Double, x As Date
CurveInterpolateRecordset = Rnd()
If rsCurve.RecordCount <> 0 Then
Q = 1
rsCurve.MoveFirst
x1 = CDate(rsCurve.Fields("MaturityDate"))
y1 = CDbl(rsCurve.Fields("ZeroRate"))
If InterpDate = CDate(rsCurve.Fields("MaturityDate")) Then CurveInterpolateRecordset = CDbl(rsCurve.Fields("ZeroRate")): Exit Function
rsCurve.MoveNext
Do While (CDate(rsCurve.Fields("MaturityDate")) <= InterpDate)
If rsCurve.EOF Then CurveInterpolateRecordset = CDbl(rsCurve.Fields("ZeroRate")): Exit Function
If InterpDate = CDate(rsCurve.Fields("MaturityDate")) Then CurveInterpolateRecordset = CDbl(rsCurve.Fields("ZeroRate")): Exit Function
If InterpDate > CDate(rsCurve.Fields("MaturityDate")) Then
x1 = CDate(rsCurve.Fields("MaturityDate"))
y1 = CDbl(rsCurve.Fields("ZeroRate"))
End If
rsCurve.MoveNext
If rsCurve.EOF Then CurveInterpolateRecordset = y1: Exit Function
Loop
x2 = CDate(rsCurve.Fields("MaturityDate"))
y2 = CDbl(rsCurve.Fields("ZeroRate"))
CurveInterpolateRecordset = y1 + (y2 - y1) * CDate((InterpDate - x1) / (x2 - x1))
End If
End Function
现在,该函数从rsCurve.MoveFirst定义的记录集的开头开始,并从那里移动到记录集,直到记录集rsCurve.EOF结束。它会对这些值进行插值。但是,我不希望它总是从记录集的开头开始。
相反,我希望它从记录集中的特定点开始。我在记录集中有一列名为“MarkAsOfDate”的日期。我希望它只插入MarkAsofDate等于某个用户输入值的位置,而不是对整个记录集进行插值。因此,对于2015年7月31日,我希望使用MarkAsofDate列等于7/31/2015的行中的值进行插值。如何更改rsCurve.MoveFirst来完成此操作?我尝试使用Do While命令,但没有成功。
答案 0 :(得分:1)
尝试这样的事情:
Set rsCurve = db.OpenRecordset("SELECT * FROM MyTable WHERE MyDate >= #" & txtUserInputDate.Value & "# ORDER BY MyDate ASC")
您必须更改明显的信息,例如表名,文本框名称和字段名称,但这是一般的想法。