【发布时间】:2018-02-07 11:51:06
【问题描述】:
我已经搜索了好几天了(作为一个菜鸟)。
Private Sub CommandButton1_Click()
Dim SellStartDate As Date 'Declare the SellStartDate as Date
Dim SellEndDate As Date 'Declare the SellEndDate as Date
SellStartDate = Sheets("Summary").Range("C3").Value 'Pass value from
cell B3 to SellStartDate variable
SellEndDate = Sheets("Summary").Range("C4").Value 'Pass value from
cell B4 to SellEndDate variable
'Pass the Parameters values to the Stored Procedure used in the Data
Connection
With ActiveWorkbook.Connections("Query - storm sales
(2)").OLEDBConnection
.CommandText = "SELECT * FROM [storm sales (2)] WHERE ADVICEDATE >=
'&SellStartDate&' AND ADVICEDATE <= '&SellEndDate&'"
ActiveWorkbook.Connections("Query - storm sales (2)").Refresh
End With
End Sub
上面的代码是基于在互联网上其他地方找到的一个项目。
如果您使用 = 或 !=,它会将值传递给 CommandText,但如果您使用包含 < 或 > 的任何内容,则不会传递这些值
我得到的错误是
[Expression.Error] 我们不能将运算符
我要问的是什么会给我与>= 和<= 相同的结果而不会出现错误。
提前致谢,
尼尔。
【问题讨论】:
-
您可以尝试在您的 sql 查询中转换日期。 stackoverflow.com/questions/10643379/…
-
谢谢@Mortaliar - 是的,我试过那个,但它给了我;运行时错误“1004”:不支持命令“SELECT * FROM [storm sales (2)] WHERE ADVICEDATE >= Convert(datetime , &SellStartDate&)”。
-
还尝试了 Convert(datetime , '&SellStartDate&'), Convert(datetime , 'SellStartDate'), Convert(date , '&SellStartDate&') 等:[DataFormat.Error] 我们不能将输入解析为日期值。