【发布时间】:2016-02-10 19:43:36
【问题描述】:
我在 Access VBA 中有以下代码。
Public Sub CalculateVol()
Dim vol As Double
Dim rs As Recordset
Dim rs2 As Recordset
Dim iRow As Long, iField As Long
Dim strSQL As String
Dim CurveID As Long
Dim MarkRunID As Long
Dim MaxOfMarkAsofDate As Date
Dim userdate As String
DoCmd.RunSQL "DELETE * FROM HolderTable"
'Clears out the old array from the holder table.
Dim I As Integer
Dim x As Date
userdate = InputBox("Please Enter the Date (mm/dd/yyyy)")
x = userdate
Dim BucketTermAmt As Long
BucketTermAmt = InputBox("Please Enter the Term Amount")
For I = 0 To 76
MaxOfMarkAsofDate = x - I
strSQL = "SELECT * FROM VolatilityOutput WHERE CurveID=" & Forms!Volatility.cboCurve.Value & " AND MaxOfMarkAsofDate=#" & MaxOfMarkAsofDate & "# ORDER BY MaxOfMarkasOfDate, MaturityDate"
Set rs = CurrentDb.OpenRecordset(strSQL, Type:=dbOpenDynaset, Options:=dbSeeChanges)
Set rs2 = CurrentDb.OpenRecordset("HolderTable")
If rs.RecordCount <> 0 Then
rs.MoveFirst
rs.MoveLast
Dim BucketTermUnit As String
Dim BucketDate As Date
Dim MarkAsOfDate As Date
Dim InterpRate As Double
Dim b As String
b = BucketTermAmt
BucketTermUnit = Forms!Volatility.cboDate.Value
BucketDate = DateAdd(BucketTermUnit, b, MaxOfMarkAsofDate)
InterpRate = CurveInterpolateRecordset(rs, BucketDate)
rs2.AddNew
rs2("BucketDate") = BucketDate
rs2("InterpRate") = InterpRate
rs2.Update
End If
Next I
vol = EWMA(0.94)
Forms!Volatility!txtVol = vol
Debug.Print vol
End Sub
基本思想是用户为 MaxofMarkAsofDate 输入一个日期。然后代码在 VolatilityOutput 表中找到 MarkAsofDate 的实例,并将其用作计算 InterpRate 的参考点。它将这个数字存储在 HolderTable 中。然后它循环相同的过程,除了使用用户输入的 MarkAsofDate 的前一天,然后是前一天,依此类推,总共 76 次。
第一部分工作正常,但循环给我带来了麻烦。如果它没有在表格中找到用户输入的日期,它将跳过它,但仍将其计为循环。因此,虽然我想要 76 个数据点,但我可能只得到 56 个,例如,如果它跳过 20 个日期。所以我想阻止它跳过,或者只是继续循环直到 HolderTable 总共有 76 个数字。我该怎么做?
【问题讨论】: