简体   繁体   English

如何从VBA使用datetime参数调用SQL Server存储过程?

[英]How to call SQL Server stored procedure with datetime parameter from VBA?

I have a SQL Server stored procedure that accepts two input parameters of datetime datatype. 我有一个SQL Server存储过程,该存储过程接受datetime数据类型的两个输入参数。 I want to call it from Excel VBA. 我想从Excel VBA调用它。

VBA doesn't have a datetime type so I've tried something like this: VBA没有日期时间类型,因此我尝试了以下操作:

Dim spCommand As ADODB.Command
Set spCommand = New ADODB.Command
spCommand.ActiveConnection = cnn
spCommand.CommandText = "myProcedureName"
spCommand.CommandType = adCmdStoredProc
spCommand.Parameters.Refresh
spCommand.Parameters(1).Value = "6/1/2016"      'DateValue("2016-06-01") + TimeValue("00:00:00")
spCommand.Parameters(2).Value = "6/1/2016"     'DateValue("2016-06-01") + TimeValue("23:59:59")
spCommand.Execute

But I get this error: 但是我得到这个错误:

在此处输入图片说明

How can I solve it? 我该如何解决?

EDIT 1 编辑1
After followeing what @Joe suggested, I opened my debugger and get this, but error is still there: 遵循@Joe的建议之后,我打开了调试器并得到了它,但是错误仍然存​​在:

在此处输入图片说明

Use the DateSerial function: 使用DateSerial函数:

spCommand.Parameters(1).Value = DateSerial(2016,1,6)

and, if you need it, optionally the TimeSerial function: 并且,如果需要,还可以选择使用TimeSerial函数:

spCommand.Parameters(2).Value = DateSerial(2016,1,6) + TimeSerial(23,59,59)

VBA doesn't have a datetime type VBA没有日期时间类型

Yes, it does: 是的,它确实:

Dim dtDateTime as Date
dtDateTime = DateSerial(2016,1,6) + TimeSerial(23,59,59)

UPDATE 更新

Following your edit, I see that the Parameters collection has three items. 编辑之后,我看到Parameters集合包含三个项目。 The first is a return parameter, and the second and third are input parameters of type adDBTimeStamp. 第一个是返回参数,第二个和第三个是adDBTimeStamp类型的输入参数。

Also the Command.NamedParameters property is false, so you probably can't use named parameters as you seem to be attempting to do in the screenshot accompanying your edit. 同样, Command.NamedParameters属性为false,因此您可能无法使用命名参数,因为您似乎试图在编辑随附的屏幕快照中使用命名参数。

UPDATE 2 更新2

VBA collections are indexed starting at 1, not 0, so you should be specifying your parameters as: VBA集合的索引从1开始,而不是0,因此您应将参数指定为:

 spCommand.Parameters(2).Value = ... spCommand.Parameters(3).Value = ... 

I think the above is wrong. 我认为以上是错误的。 Standard VB/VBA collections are indexed, starting at 1. I believe the rationale, mistaken in my view, was that 1-based indexes are "more intuitive". 从1开始对标准VB / VBA集合进行索引。我认为,基于我的观点的误解是基于1的索引“更直观”。

When ADO was developed, I believe Microsoft realized that 1-based collections had been a mistake, and decided to make ADO collections 0-based. 在开发ADO时,我相信Microsoft意识到基于1的集合是一个错误,因此决定使ADO集合基于0。 This makes them inconsistent with standard collections, but "more intuitive" to, say, javascript developers. 这使它们与标准集合不一致,但对javascript开发人员来说“更直观”。 What an inconsistent mess. 真是一团糟。

If I'm right (and it's a long time since I used ADO, and I haven't tested this), then you need: 如果我是对的(并且自从使用ADO已经很长时间了,并且还没有测试过),那么您需要:

spCommand.Parameters(1).Value = ...
spCommand.Parameters(2).Value = ...

which should eliminate your 3265 error, but doesn't solve your original problem. 这样可以消除3265错误,但不能解决您原来的问题。 Your debug trace appears to show that the parameters have been set to the expected values. 您的调试跟踪显示似乎已将参数设置为预期值。 I can only suggest you try to generate an MCVE , including a simplified version of the Stored Procedure in question. 我只能建议您尝试生成MCVE ,包括所讨论的存储过程的简化版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Crystal Reports 到 SQL Server 存储过程的日期时间参数格式 - Datetime parameter format from Crystal Reports to SQL Server stored procedure 使用PHP的输出参数调用SQL Server存储过程 - Call SQL Server stored procedure with output parameter from PHP 在 R 中使用参数调用 SQL Server 存储过程 - call a SQL Server Stored Procedure with Parameter in R 从 VBA 运行 SQL 服务器存储过程 - Run SQL Server stored procedure from VBA SQL Server存储过程中的datetime参数选择数据 - SQL Server stored procedure datetime parameter on selecting data 使用 Access 2003 中的日期时间参数执行 MS SQL Server 存储过程 - Execution MS SQL Server stored procedure with datetime parameter from Access 2003 如何从 PowerShell 调用 SQL Server 存储过程? - How do I call a SQL Server stored procedure from PowerShell? 如何在 Xamarin 中从 Android 调用 SQL Server 存储过程 - How to call SQL Server stored procedure from Android in Xamarin 如何从Excel(Windows身份验证)调用SQL Server中的存储过程? - How to call stored procedure in SQL Server from Excel (Windows Authentication)? 使用excel参数并且不使用VBA运行Sql Server存储过程 - Running Sql server Stored Procedure with excel parameter and without using VBA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM