在Access VBA中存储数组

时间:2016-01-05 14:19:58

标签: arrays excel vba excel-vba access-vba

我在Access VBA中编写了一个子程序,此处显示为参考:Looping Through Dates in strSQL in Access VBA。它如下:

Combinations
#  Comb_1 Comb_2
#A      4      2
#B      2      4
#C      1      1

运行此子程序可计算76个数字的范围。我想获取这76个数字,并在以下函数中将它们用作“ZeroCurveInput”。

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

我最初在Excel VBA中编写了此函数,我正在尝试将其移植到Access VBA。在Excel中,我只是读取76列数作为范围并将其作为变量存储为数组,然后在函数中使用它。但是我不能在Access中使用range属性做类似的事情,我不知道该用什么代替。

如何将数字存储为数组,然后将它们传递给函数?

2 个答案:

答案 0 :(得分:1)

有几种不同的方法可以做到这一点,包括按照你的建议使用数组。如果您只需要76个数字,则可以使用集合执行此操作。该系列的优点是您不需要提前知道其中有多少物品。

以下是使用集合的简单工作示例:

var Text2D = require('three-text2d').Text2D

var text = new Text2D("Hello world!", { align: textAlign.right, font: '30px Arial', fillStyle: '#000000', antialias: true })
scene.add(text) 

答案 1 :(得分:1)

您也可以简单地将SampleReadCurve中的数字写入表格,然后循环遍历表格,就像您在数组中循环一样。所以,你的sub会改变这个(顺便说一下,这有用吗?你正在创建一个Sub并结束一个函数......):

Sub SampleReadCurve()

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

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)
Set rs2 = CurrentDb.OpenRecordset("MyNewTable")

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
    rs2.AddNew
    rs2("BucketDate") = BucketDate
    rs2("InterpRate") = InterpRate
    rs2.Update

End If

Next I


End Function

然后您的EWMA功能如下所示:

Function EWMA(Lambda As Double) As Double

    Dim Price1 As Double, Price2 As Double
    Dim SumWtdRtn As Double
    Dim I As Long
    Dim m As Double
    Dim rec as Recordset

    Dim LogRtn As Double, RtnSQ As Double, WT As Double, WtdRtn As Double

m = BucketTermAmt

Set rec = CurrentDB.OpenRecordset("SELECT InterpRate FROM MyNewTable")

I = 2
Do While rec.EOF = False

    Price1 = Exp(-rec("InterpRate")(I - 1, 1) * (m / 12))
    Price2 = Exp(-rec("InterpRate")(I, 1) * (m / 12))
    LogRtn = Log(Price1 / Price2)
    RtnSQ = LogRtn ^ 2
    WT = (1 - Lambda) * Lambda ^ (I - 2)
    WtdRtn = WT * RtnSQ
    SumWtdRtn = SumWtdRtn + WtdRtn
    I = I + 1

Loop

EWMA = SumWtdRtn ^ (1 / 2)

End Function