简体   繁体   中英

Excel [Expression.Error] We cannot apply operator < to types Text and Date

I have been searching for several days now (as a total noob).

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

The above code was based on an item found elsewhere on the internet.

It passes the values to the CommandText if you use = or != but not if you use anything containing < or >

The error I get is

[Expression.Error] We cannot apply operator < to types Text and Date

What I am asking is what would give me the same result as >= and <= without the error.

Thanks in advance,

Neil.

Did you try the conversion with apostrophes in the query?

Convert(datetime , &SellStartDate&)

Convert(datetime ,' &SellStartDate& ')

This is how SQL converts with and without the apostrophes.

Convert(datetime, '2010-04-01' ) -> 2010-04-01 00:00:00.000

Convert(datetime, 2010-04-01 ) -> 1905-06-29 00:00:00.000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM