在Access VBA中使用集合

我有以下的子程序在Access VBA中:

Sub SampleReadCurve() Dim rs 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 CurveID = 15 Dim I As Integer Dim x As Date userdate = InputBox("Please Enter the Date (mm/dd/yyyy)") x = userdate 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) If rs.RecordCount <> 0 Then rs.MoveFirst rs.MoveLast Dim BucketTermAmt As Long Dim BucketTermUnit As String Dim BucketDate As Date Dim MarkAsOfDate As Date Dim InterpRate As Double BucketTermAmt = 3 BucketTermUnit = "m" BucketDate = DateAdd(BucketTermUnit, BucketTermAmt, MaxOfMarkAsofDate) InterpRate = CurveInterpolateRecordset(rs, BucketDate) Debug.Print BucketDate, InterpRate End If Next I End Function 

这个子程序推出了一个包含date和相​​关速率的76个数字的2×2列表。 我想将这个值列表作为一个Collection存储,这样我就可以将它用作另一个函数的input。 是否有可能使用集合来做到这一点? 什么是适当的语法?

我同意评论说数据字典可能是要走的路。 原因是,如果需要的话,你可以通过字典来实现循环。 您将需要添加对Microsoft脚本运行时的引用。 这里是一个简单的例子:

 Public Function a() Dim dRates As New Scripting.Dictionary Dim key As Variant dRates.Add #1/1/2016#, 1 dRates.Add #2/1/2016#, 1.5 dRates.Add #3/1/2016#, 2 'You can either access the rate directly with the key: Debug.Print dRates(#2/1/2016#) 'Or you can loop through the keys/values For Each key in dRates.Keys Debug.Print key & " - " & dRates(key) Next 'Or, you can pass the entire collection to a function Call b(dRates) End Function Public Function b(d As Scripting.Dictionary) For Each key In d.Keys Debug.Print key & " - " & d(key) Next End Function 

这将提供以下输出:

 1.5 1/1/2016 - 1 2/1/2016 - 1.5 3/1/2016 - 2 1/1/2016 - 1 2/1/2016 - 1.5 3/1/2016 - 2