VBA在存储过程问题中传递日期

时间:2014-05-06 18:39:29

标签: sql vba excel-vba stored-procedures excel

我很难使用SQL存储过程将Date值传递给datetime值。 @DateTime参数在Microsoft SQL Server中的MyDb中采用日期时间格式。当我运行下面的子程序时,我收到一个错误:Parameter type not supported.我认为该错误与adDBTimeStamp参数类型有关。知道如何解决这个问题吗?

Public Sub AddDateTime

Dim DateTime as Date
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String

' Create the connection string

DateTime = Now()

sConnString = "Driver={SQL Server};Server=TheServer3;Database=MyDb;Trusted_Connection=Yes;"

Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
cmd.ActiveConnection = sConnString
cmd.CommandText = "usp_AddNewDateTime"
cmd.CommandType = adCmdStoredProc

Set param = cmd.CreateParameter("@DateTime", adDBTimeStamp, adParamInput, , DateTime)
cmd.Parameters.Append param

Set rs = cmd.Execute

' Clean up

Set conn = Nothing
Set rs = Nothing

End Sub

2 个答案:

答案 0 :(得分:1)

由于您提供Date值,请使用adDBDate

Set param = cmd.CreateParameter("@DateTime", adDBDate, adParamInput, , DateTime)

答案 1 :(得分:0)

简单解决方案 - 必须将DateTime变量转换为Double:CDbl(DateTime)