我现在已经搜索了几天(作为总菜鸟(。
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
上面的代码基于Internet上其他位置的项目。
如果使用=
或!=
,它将值传递给命令文本,但如果您使用包含<
或>
我得到的错误是
[express.error]我们不能应用运算符&lt;到类型文字和日期
我要问的是什么会给我与>=
和<=
相同的结果。
预先感谢
neil。
您是否尝试使用查询中的撇号转换?
Convert(datetime , &SellStartDate&)
Convert(datetime ,' &SellStartDate& ')
这是SQL在有或没有撇号的情况下转换的方式。
Convert(datetime, '2010-04-01' ) -> 2010-04-01 00:00:00.000
Convert(datetime, 2010-04-01 ) -> 1905-06-29 00:00:00.000