我正在尝试使用VBA中的嵌入式SQL访问SQL数据库。问题在于定义我想要访问的条件(ID > @Identifie
是导致问题的部分)。当我运行代码时,我得到必须声明标量变量@Identifie
。但是,当我转到我的本地窗口时,@ Isdentifie被正确声明并具有我在单元格K6中指定的值。此外,如果我将条件@Identifie
中的ID > @Identifie
替换为某个值,我的代码运行完美,因此没有其他错误。似乎我的问题来自追加方法,但我无法弄清楚我做错了什么。
这是我的代码:
(我在这篇文章中删除了连接字符串,但这显然不是问题):
Option Explicit
Option Base 1
Sub LoadData()
Application.ScreenUpdating = False
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim lastID As Double
Dim parametre As New ADODB.Parameter
Dim Last_total_ID As Double
Last_total_ID = ActiveWorkbook.Worksheets("Consolidated").Range("K6").Value
cn.ConnectionString = ""
cn.Open
Set parametre = cmd.CreateParameter(Name:="@Identifie", Type:=adDouble, Direction:=adParamInput)
parametre.Value = Last_total_ID
cmd.Parameters.Append parametre
cmd.ActiveConnection = cn
cmd.CommandText = "Select ID, Issuer,LaunchDate,SettleDate,CADAmount,Description,Price,Currency, Maturity,IssuerID, Coupon FROM dbo.tblHistoricalIssuanceStats WHERE (IsProvy = 1) AND (ID > @Identifie)"
Set rs = cmd.Execute
答案 0 :(得分:0)
为什么不:
cmd.CommandText = "Select ID, Issuer,LaunchDate,SettleDate,CADAmount,Description,Price,Currency, Maturity,IssuerID, Coupon FROM dbo.tblHistoricalIssuanceStats WHERE (IsProvy = 1) AND (ID > " & Last_total_ID & ");"
我认为您不需要使用ADODB参数。