VBA中的数据转换类型错误

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

标签: sql-server vba excel-vba ms-access excel

我正在尝试访问Access VBA中表的元素作为函数的一部分。但是,我收到一条错误消息"数据类型转换错误"。我无法弄清楚我在这里做错了什么。

以下子例程填充了我要访问的表的元素," HolderTable"。

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

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

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

Dim vol As Long

vol = EWMA(0.94)

Debug.Print vol


End Sub

这是EWMA的功能,它给出了错误信息。基本上它只是在HolderTable的元素上设置一系列简单的操作,它们由SampleReadCurve子例程中派生的值填充。

Function EWMA(Lambda As Double) As Double

    Dim Price1 As Double, Price2 As Double
    Dim vInterpRate() As Variant
    Dim SumWtdRtn As Double
    Dim I As Long
    Dim m As Double
    Dim rec As Recordset
    Dim BucketTermAmt As Long

    BucketTermAmt = 3

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

m = BucketTermAmt

Set rec = CurrentDb.OpenRecordset("SELECT InterpRate FROM HolderTable")

Do While rec.EOF = False

rec("InterpRate") = vInterpRate

    Price1 = Exp(vInterpRate(I - 1, 1) * (m / 12))

    Price2 = Exp(vInterpRate(I, 1) * (m / 12))

    LogRtn = Log(Price1 / Price2)

    RtnSQ = LogRtn ^ 2

    WT = (1 - Lambda) * Lambda ^ (I - 2)

    WtdRtn = WT * RtnSQ

    SumWtdRtn = SumWtdRtn + WtdRtn

Loop

EWMA = SumWtdRtn ^ (1 / 2)

End Function

HolderTable有两个字段,BucketDate和InterpRate,两者都有数据类型&#34;短文本&#34;。我在

行获得了数据类型转换错误消息
   Price1 = Exp(vInterpRate(I - 1, 1) * (m / 12))

更改数据类型仍会在运行时产生相同的错误消息。我做错了什么?

1 个答案:

答案 0 :(得分:3)

原件: 你确定它不应该是:

vInterpRate = rec("InterpRate")

而不是

rec("InterpRate") = vInterpRate()

修:

dim x as integer
Set rec = CurrentDb.OpenRecordset("SELECT InterpRate FROM HolderTable")
x = 0
Do While rec.EOF = False
redim(vInterpRate, x+1) 'need to preserve don't remember if this is default in vba
  vInterpRate(x) = rec("InterpRate")
  x = x + 1
  rec.next
Loop

for i = 1 to x do

  Price1 = Exp(vInterpRate(I - 1, 1) * (m / 12))

  Price2 = Exp(vInterpRate(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

代码中可能存在错误,因为我没有vba编译器,而且自从我使用它以来已经很长时间了,但我认为这应该指向你正确的方向,如果我正在解释你正在寻找的东西。