日期在Access VBA中不正确地循环

时间:2016-03-01 15:58:11

标签: vba ms-access

我在Access VBA中有以下代码:

Public Sub CalculateVol()

Dim vol As Double
Dim rs As Recordset
Dim rs2 As Recordset
Dim rs3 As Recordset
Dim strSQL As String
Dim MaxOfMarkAsofDate As Date
Dim userdate As String

DoCmd.RunSQL "DELETE * FROM HolderTable"

DoCmd.RunSQL "DELETE * FROM volTable"
'Clears out the old array from the holder table.

Dim dateCounter As Integer
Dim count 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")

Do While count < 78

MaxOfMarkAsofDate = x - dateCounter


MaturityTermCount = 3

strSQL = "SELECT * FROM RiskMetricsOutput WHERE MaturityTermCount=" & MaturityTermCount & " AND MaxOfMarkAsofDate=#" & MaxOfMarkAsofDate & "# ORDER BY MaxOfMarkasOfDate"


Set rs = CurrentDb.OpenRecordset(strSQL, Type:=dbOpenDynaset, Options:=dbSeeChanges)
Set rs2 = CurrentDb.OpenRecordset("HolderTable")
Set rs3 = CurrentDb.OpenRecordset("volTable")


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 = "m"
    BucketDate = DateAdd(BucketTermUnit, b, MaxOfMarkAsofDate)
    InterpRate = CurveInterpolateRecordset(rs, BucketDate)

    rs2.AddNew
    rs2("BucketDate") = MaxOfMarkAsofDate
    rs2("InterpRate") = InterpRate
    rs2.Update

count = count + 1


End If

dateCounter = dateCounter + 1



vol = EWMA(0.94) * 1.65

rs3.AddNew
rs3("MaxofMarkasofDate") = MaxofMarkasofDate
rs3("Vols") = vol
rs3.Update

Debug.Print MaxOfMarkAsofDate, vol

Loop

End Sub

基本思想是用户输入MaxofMarkAsofDate的日期。然后,代码在RiskMetricsOutput表中找到MarkAsofDate的实例,并将其用作计算InterpRate的参考点。它将此数字存储在HolderTable中。

然后它循环相同的过程,除了使用用户输入的MarkAsofDate之前的一天,然后是之前的一天,依此类推,总共78次。

然后使用这些InterpRates计算简单波动率,并将波动率和相关日期存储在名为VolTable的表中。

所有这一切都很好,它得到了正确的波动率数字。但是,日期在VolTable中倒退,我无法弄清楚原因。例如,如果我输入日期为8/20/2015,我会在表格中看到正确的波动率,但相关的MarkasofDate将是2015年4月30日。它将显示为最后一个日期,而不是第一个日期。我做错了什么?

编辑 - 记录集

RiskMetrics输出记录的格式如下:返回2000:

BucketID    MaxOfMarkAsOfDate   PriceVolatility MarkRunID    MaturityTermCount  MaturityDate    UnderlyingValue
CA.CAD.GDB      2/26/2016      0.006033          10704              3           5/26/2016        0.461
CA.CAD.GDB      2/25/2016      0.006033          10705              3           5/25/2016        0.47
CA.CAD.GDB      2/24/2016      0.006033          10706              3           5/24/2016        0.465
    CA.CAD.GDB      2/23/2016      0.006033          10707              3           5/23/2016        0.463

输出表volTable如下所示:

MaxOfMarkAsofDate   Vols
8/20/2015          0.0375
8/19/2015          0.0398
8/18/2015          0.0344
8/17/2015          0.0323
8/16/2015          0.0311

....


5/4/2015            0.01313
5/3/2015            0.01123
5/2/2015            0.01564
5/1/2015            0.01709
4/30/2015           0.01706

问题是输出是向后的:0.01706应该是2015年8月20日的数字,0.0375应该是4/30/2015的数字。

1 个答案:

答案 0 :(得分:0)

调整这些行:

x = CDate(userdate)

Dim BucketTermAmt As Long
BucketTermAmt = Val(InputBox("Please Enter the Term Amount"))

Do While count < 78

    MaxOfMarkAsofDate = DateAdd("d", -dateCounter, x)

此外 - 并为逆序添加Desc:

strSQL = "SELECT * FROM RiskMetricsOutput WHERE MaturityTermCount = " & MaturityTermCount & " AND MaxOfMarkAsofDate = #" & Format(MaxOfMarkAsofDate, "yyyy\/mm\/dd") & "# ORDER BY MaxOfMarkasOfDate Desc"

此外,您可能必须重命名变量MaxOfMarkAsofDate以与字段名称冲突。