gpt4 book ai didi

sql-server - VBA 中的数据转换类型错误

转载 作者:行者123 更新时间:2023-12-04 22:01:10 27 4
gpt4 key购买 nike

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

以下子例程填充了我尝试 Access 的表的元素“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,它们的数据类型都是“短文本”。我在该行收到数据类型转换错误消息
   Price1 = Exp(vInterpRate(I - 1, 1) * (m / 12))

运行时更改数据类型仍会导致相同的错误消息。我究竟做错了什么?

最佳答案

原来的:
你确定它不应该是:

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 编译器,而且我已经有很长时间没有使用它了,但我认为如果我正在解释你的意思,这应该为你指明正确的方向寻找正确。

关于sql-server - VBA 中的数据转换类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34798594/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com