使用Access VBA循环访问表

时间:2016-12-13 14:50:12

标签: sql ms-access access-vba

目前在Access VBA中我有一个表格的过滤器" VolatilityOutput"在Access中,如下:

Dim CurveID As Long
CurveID = 15
Dim strSQL As String
strSQL = "SELECT * FROM VolatilityOutput WHERE CurveID=" & CurveID & " AND MaxOfMarkAsofDate=#" & MaxOfMarkAsofDate & "# ORDER BY MaxOfMarkasOfDate, MaturityDate"

CurveID是表VolatilityOutput中的一个字段。现在CurveID = 15是硬编码的。每个曲线ID对应于另一个字段CurveShortName中的标题。数据存储在此表中," tblCurve":

enter image description here

设置CurveID后,代码会对该特定数据执行一些计算。

现在不是一次选择一个CurveID(例如CurveID = 15),而是想更改此代码,以便它对表中存储的每个CurveID执行计算。因此,对于表中的每个CurveID,它将对CurveID = 13,然后CurveID = 14,CurveID = 15等进行计算。如何更改上面的代码,使其遍历表格,而不是一次输入一条曲线?

编辑更新:完整代码发布在此处

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.

CurveID = 15

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 150

MaxOfMarkAsofDate = x - I


strSQL = "SELECT * FROM VolatilityOutput WHERE CurveID=" & CurveID & " 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 = "m"
    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)

Debug.Print vol


End Sub

1 个答案:

答案 0 :(得分:2)

您可以使用whileDo循环遍历记录集。类似的东西:

If rs.RecordCount <> 0 Then
    'set the cursor to the first row
    rs.MoveFirst

    Dim BucketTermUnit As String
    Dim BucketDate As Date
    Dim MarkAsOfDate As Date
    Dim InterpRate As Double


    Dim b As String

    'loop the rows
    Do Until rs.EOF 'endoffile



        b = BucketTermAmt

        BucketTermUnit = "m"
        BucketDate = DateAdd(BucketTermUnit, b, MaxOfMarkAsofDate)
        InterpRate = CurveInterpolateRecordset(rs, BucketDate)

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

        'to the next row for the next loop
        rs.movenext
    Loop


End If

现在你只需要加载rs所有记录,而不只是一个,然后让循环完成工作。

因为你的CurveInterpolateRecordset函数需要一个记录集,并对它做了一些事情(我不打算开始猜测),所以最好添加一个不同的curveID的新记录集然后循环几乎记录集内的整个原始例程。类似的东西:

Public Sub CalculateVol()


Dim vol As Double
Dim rs As Recordset
Dim rs2 As Recordset
Dim rsCurve 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.

CurveID = 15

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")

'get distinct curveIDs for the loop
strSQL = "SELECT CurveID FROM VolatilityOutput GROUP BY CurveID;"

'open up distinct curveID recordset
Set rsCurve = CurrentDB.OpenRecordset(strSQL, Type:=dbOpenDynaset, Options:=dbSeeChanges)

'loop through each distinct curveID
Do Until rsCurve.EOF

    'Loop for your dates
    For I = 0 To 150
        MaxOfMarkAsofDate = x - I

        'Changed this to the new curveID from the rsCurve recordset.
        strSQL = "SELECT * FROM VolatilityOutput WHERE CurveID=" & rsCurve.Fields("CurveID") & " 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 = "m"
            BucketDate = DateAdd(BucketTermUnit, b, MaxOfMarkAsofDate)
            InterpRate = CurveInterpolateRecordset(rs, BucketDate)

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


        End If
    Next I

    'move to next curveID
    rsCurve.MoveNext
Loop

vol = EWMA(0.94)

Debug.Print vol


End Sub