从VBA执行SQL Server SP - 参数错误

时间:2014-04-18 19:04:57

标签: sql-server excel vba excel-vba vb6

我收到错误"参数类型错误,超出可接受的范围,或彼此冲突"尝试从Excel / VBA调用SQL Server存储过程时。

无法弄清楚如何从VBA拨打电话?

存储过程参数

CREATE PROCEDURE [dbo].[sp_Productivity_GetIndividuals] (
@startDate DATE,
@endDate DATE)
....



VBA代码

Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command

With cmd
    .ActiveConnection = con
    .CommandText = "sp_Productivity_GetIndividuals"
    .CommandType = adCmdStoredProc
    .Parameters.Append .CreateParameter("@startDate", adDate, adParamInput, , "3/1/2014")
     .Parameters.Append .CreateParameter("@endDate", adDate, adParamInput, , "3/31/2014")
    Set rs.ActiveConnection = .Execute
End With

1 个答案:

答案 0 :(得分:1)

尝试将日期传递为“2014-03-01”,它会起作用。如果您以excel日期参数开头,请使用FORMAT转换为字符串,如:

.Parameters.Append .CreateParameter("@startDate", adDate, adParamInput, , Format(now(),"yyyy-mm-dd"))