[英]Moving through the Recordset in Access VBA
I have a simple function using Excel VBA for calculating volatility. 我有一个使用Excel VBA来计算波动率的简单函数。 It takes as inputs a column of numbers (Zeros) and two dates.
它以一列数字(零)和两个日期作为输入。 The code is:
代码是:
Function EWMA(Zeros As Range, Lambda As Double, MarkDate As Date, MaturityDate As Date) As Double
Dim vZeros() As Variant
Dim Price1 As Double, Price2 As Double
Dim SumWtdRtn As Double
Dim I As Long
Dim m As Double
Dim LogRtn As Double, RtnSQ As Double, WT As Double, WtdRtn As Double
vZeros = Zeros
m = Month(MaturityDate) - Month(MarkDate)
For I = 2 To UBound(vZeros, 1)
Price1 = Exp(-vZeros(I - 1, 1) * (m / 12))
Price2 = Exp(-vZeros(I, 1) * (m / 12))
LogRtn = Log(Price1 / Price2)
RtnSQ = LogRtn ^ 2
WT = (1 - Lambda) * Lambda ^ (I - 2)
WtdRtn = WT * RtnSQ
SumWtdRtn = SumWtdRtn + WtdRtn
Next I
EWMA = SumWtdRtn ^ (1 / 2)
End Function
The main feature enabling the function to work is the For loop. 使功能起作用的主要功能是For循环。 I want to re-create this in Access VBA using recordset objects.
我想使用记录集对象在Access VBA中重新创建它。 The recordset has the same fields as the Excel spreadsheet.
记录集具有与Excel电子表格相同的字段。 I'm not exactly sure how to convert the code over, though.
不过,我不确定如何转换代码。 Here is what I have so far:
这是我到目前为止的内容:
Function EWMA(rsCurve As Recordset, InterpRate As Double, Lambda As Double) As Double
Dim vZeros() As Variant
Dim Price1 As Double, Price2 As Double
Dim SumWtdRtn As Double
Dim I As Long
Dim mat As Date
Dim mark As Date
Dim LogRtn As Double, RtnSQ As Double, WT As Double, WtdRtn As Double
CurveInterpolateRecordset = Rnd()
If rsCurve.RecordCount <> 0 Then
vZeros = CVar(rsCurve.Fields("CurveInterpolateRecordset"))
mat = CDate(rsCurve.Fields("MaturityDate"))
mark = CDate(rsCurve.Fields("MarkDate"))
m = Month(mat) - Month(mark)
For I = 2 To UBound(vZeros, 1)
Price1 = Exp(-vZeros(I - 1, 1) * (m / 12))
Price2 = Exp(-vZeros(I, 1) * (m / 12))
LogRtn = Log(Price1 / Price2)
RtnSQ = LogRtn ^ 2
WT = (1 - Lambda) * Lambda ^ (I - 2)
WtdRtn = WT * RtnSQ
SumWtdRtn = SumWtdRtn + WtdRtn
Next I
EWMA = SumWtdRtn ^ (1 / 2)
End If
Debug.Print EWMA
End Function
The function is called in an earlier subroutine in Access. 该函数在Access的更早的子例程中调用。 What am I missing in order to move through the recordset in Access, similar to looping through the spreadsheet in Excel VBA?
为了在Access中的记录集之间移动,我想念什么(类似于在Excel VBA中循环浏览电子表格)?
Here are some basics about using a recordset. 这是有关使用记录集的一些基础知识。
Dim rs As New ADODB.Recordset
'Add fields to your recordset for storing data.
With rs
.Fields.Append "Row", adInteger
.Fields.Append "ColumnName2", adChar, 30
.Fields.Append "ColumnName3", adInteger
.Open
End With
Add records to it manually 手动添加记录
rs.AddNew
rs.Fields("Row").Value = 1
rs.Fields("ColumnName2").Value = "Put some value in"
rs.Update
rs.AddNew
rs.Fields("Row").Value = 2
rs.Fields("ColumnName2").Value = "Put another value in"
rs.Update
You can also populate it with a query of a table. 您也可以使用表查询来填充它。
Move to the begining of the recordset 移至记录集的开头
If rs.EOF = False Then
rs.MoveFirst
End If
Loop through the recordset 遍历记录集
Do While rs.EOF = False
msgbox(rs.Fields("ColumnName2").Value)
rs.MoveNext
Loop
The easiest method would be to use GetRows
to pull an array from your recordset: 最简单的方法是使用
GetRows
从记录GetRows
提取数组:
Recordset.GetRows Method Recordset.GetRows方法
Then the new code would be nearly a copy-n-paste of your proven code starting with basically this: 那么新的代码将几乎是您经过验证的代码的“ n复制”粘贴操作,基本上是从以下代码开始的:
vZeros = rsCurve.GetRows(rsCurve.RecordCount)
As a side note you wouldn't need CDate here: 附带说明一下,您在这里不需要CDate:
mat = rsCurve.Fields("MaturityDate").Value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.