【问题标题】:Moving through the Recordset in Access VBA在 Access VBA 中移动记录集
【发布时间】:2015-10-26 14:21:08
【问题描述】:

我有一个使用 Excel VBA 计算波动率的简单函数。它将一列数字(零)和两个日期作为输入。代码是:

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

使函数能够工作的主要功能是 For 循环。我想使用记录集对象在 Access VBA 中重新创建它。记录集具有与 Excel 电子表格相同的字段。不过,我不确定如何转换代码。这是我到目前为止所拥有的:

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

该函数在 Access 的早期子例程中调用。为了在 Access 中移动记录集,我缺少什么,类似于在 Excel VBA 中循环电子表格?

【问题讨论】:

    标签: ms-access vba recordset


    【解决方案1】:

    最简单的方法是使用GetRows 从您的记录集中拉出一个数组:

    Recordset.GetRows Method

    那么新代码几乎是您经过验证的代码的复制粘贴,基本上是这样开始的:

    vZeros = rsCurve.GetRows(rsCurve.RecordCount)
    

    作为旁注,您在这里不需要 CDate:

    mat = rsCurve.Fields("MaturityDate").Value
    

    【讨论】:

      【解决方案2】:

      这里有一些关于使用记录集的基础知识。

      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
      

      手动添加记录

      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
      

      您也可以使用表格查询来填充它。

      移动到记录集的开头

      If rs.EOF = False Then
          rs.MoveFirst
      End If
      

      遍历记录集

      Do While rs.EOF = False
      
              msgbox(rs.Fields("ColumnName2").Value)
      
      rs.MoveNext
      Loop
      

      【讨论】:

      • 谢谢。我对某一点感到困惑。如果我想在特定间隔 [ I = 2 to UBound(vZeros,1) ] 内循环记录集,只需移动到记录集的开头并循环就足够了吗?
      • 这只是一般记录集导航的一个示例。你可以用它做任何你想做的事情。
      • @varunchandra:通常,您对记录集使用 SELECT 查询,通过使用适当的 WHERE 条件,返回正是要循环的记录
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 2011-06-18
      • 2015-02-09
      • 2014-01-15
      相关资源
      最近更新 更多