使用Excel VBA将参数传递给存储过程的问题

时间:2014-09-24 18:27:12

标签: vba excel-vba stored-procedures parameter-passing excel

似乎是一个常见问题,但我无法使用任何示例来处理我的代码。

如果这是微不足道的事,请提前抱歉,因为我是DBA而不是开发人员。

我的存储过程需要两个参数LaborRateEndDateLaborRate是十进制数据类型,将从工作表BOMSum和单元格G1中获取其变量。 EndDate是日期数据类型,将从工作表BOMSum和单元格G2获取其变量。即使在查看本网站上的教程和其他建议之后,我的所有尝试都失败了。

这是我开始使用的非常简单的代码,它实际上运行正确但运行时没有指定任何参数:

Sub Execute_SP()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command

Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=SQLOLEDB;Data Source=<ServerName>;Initial Catalog=<DB>;User ID=<DB_User>;Password=<pwd>"
conn.Open

Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "CostingInfo"

cmd.Execute
conn.Close

Set conn = Nothing
Set cmd = Nothing
ActiveWorkbook.RefreshAll
End Sub

这是我现在所拥有的。我试图从单元格G1和G2中传递两个参数LaborRateEndDate

Sub Execute_SP()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim prm1 As ADODB.Parameter
Dim prm2 As ADODB.Parameter

Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=SQLOLEDB;Data Source=<ServerName>;Initial Catalog=<DB>;User ID=<DB_User>;Password=<pwd>"
conn.Open

Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = CostingInfo
prm1 = cmd.CreateParameter("@LaborRate", adDecimal, adParamInput)
prm1.Precision = 28
prm1.NumericScale = 4
cmd.Parameters.Append prm1
prm1.Value = ActiveSheet.Range("G1").Text
prm2 = cmd.CreateParameter("@endDate", adDate, adParamInput)
prm2.Value = ActiveSheet.Range("G2").Text
cmd.Parameters.Append prm2
cmd.Execute
conn.Close

Set conn = Nothing
Set cmd = Nothing
ActiveWorkbook.RefreshAll
End Sub

我在精度和对象变量方面遇到错误,或者没有设置块变量。我很难接受VBA节目。

编辑:经过Rude Dawg的一些建议,现在这里是我的代码。我现在收到了#34;语法错误或访问冲突&#34;错误,调试时指向具有问题的cmd.Execute部分:

Sub Execute_SP()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim prm1 As ADODB.Parameter
Dim prm2 As ADODB.Parameter

Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=SQLOLEDB;Data Source=<ServerName>;Initial Catalog=<DB>;User ID=    <DB_User>;Password=<pwd>"
conn.Open

Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = CostingInfo
set prm1 = cmd.CreateParameter("@LaborRate", adDecimal, adParamInput)
prm1.Precision = 28
prm1.NumericScale = 4
prm1.Value = Sheets("BOMSum").Range("G1").VALUE
cmd.Parameters.Append prm1
set prm2 = cmd.CreateParameter("@endDate", adDate, adParamInput)
prm2.Value = Sheets("BOMSum").Range("G2").VALUE
cmd.Parameters.Append prm2
cmd.Execute
conn.Close

Set conn = Nothing
Set cmd = Nothing
ActiveWorkbook.RefreshAll
End Sub

2 个答案:

答案 0 :(得分:1)

尝试这个轻微的mod进入你的条目。

set prm1 = cmd.CreateParameter("@LaborRate", adDecimal, adParamInput)
prm1.Precision = 28
prm1.NumericScale = 4
cmd.Parameters.Append prm1
prm1.Value = Sheets("BOMSum").Range("G1").VALUE
set prm2 = cmd.CreateParameter("@endDate", adDate, adParamInput)
prm2.Value = Sheets("BOMSum").Range("G2").VALUE
cmd.Parameters.Append prm2

另外,为什么你在拉动细胞之前附加prm1&#34; G1&#34;值?这是一个轻微的错字吗?

答案 1 :(得分:1)

结束发现问题。

在我的键盘上猛击头后,我发现我的存储过程名称周围没有双引号。

如果有兴趣的话,这是我的最终结果:

Sub Execute_SP()
Dim conn As ADODB.Connection
Dim cmd As ADODB.Command
Dim prm1 As ADODB.Parameter
Dim prm2 As ADODB.Parameter
Dim rs As ADODB.Recordset

Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=SQLOLEDB;Data Source=<ServerName>;Initial Catalog=<DB>;User ID=    <DB_User>;Password=<pwd>"

conn.Open

Set cmd = New ADODB.Command
cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
Set prm1 = cmd.CreateParameter("@LaborRate", adDecimal, adParamInput, 14,     ThisWorkbook.Sheets("BOMSumTbl").Range("G1").Value)
prm1.Precision = 28
prm1.NumericScale = 4
cmd.Parameters.Append prm1
Set prm2 = cmd.CreateParameter("@EndDate", adDate, adParamInput, 7,     ThisWorkbook.Sheets("BOMSumTbl").Range("G2").Value)
cmd.Parameters.Append prm2
cmd.CommandText = "CostingInfo"
Set rs = cmd.Execute(adCmdStoredProc)
conn.Close

Set conn = Nothing
Set cmd = Nothing
Set rs = Nothing
ActiveWorkbook.RefreshAll
End Sub