I am using SQL inside an Excel Sheet using the following provider
Set m_Connection = CreateObject("ADODB.Connection")
m_Connection.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=Yes;"";"
I have two textboxes (from and to) where users type two dates From, To, to select the records corresponding at this date selection
I am facing the following problems:
First of all I tried this code, I was getting all the fields whatever are the dates dates from the sheet, except if I have a blank sheet, the ones where the cells are empty inside the column date are empties, so I wasn't getting an error, but my condition between two dates doesn't work
SELECT [ListName] AS [List name]
FROM [Database$]
WHERE CDATE([ClipDate]) BETWEEN #01/09/2017# AND #14/03/2019#;
I tried not to use the BETWEEN, but I got same wrong results (all records except empty ones are selected whatever is the ClipDate)
SELECT [ListName] AS [List name]
FROM [Database$]
WHERE CDATE([ClipDate]) >= #01/09/2017# AND CDATE([ClipDate]) <= #14/03/2019#;
I tried the following one, by replacing the # by "", this time I get a good date selection (the ones that I want), but I get an error saying "Invalid Use of Null" if any of the cells at the column Date is empty.
SELECT [ListName] AS [List name]
FROM [Database$]
WHERE CDATE([ClipDate]) >= "01/09/2017" AND CDATE([ClipDate]) <= "14/03/2019";
I tried at the end this, and got the same result as previous one , Invalid Use of Null in case of blank cell
SELECT [ListName] AS [List name]
FROM [Database$]
WHERE CDATE([ClipDate]) BETWEEN "01/09/2017" AND "11/03/2019";
here is the screen of From, to
In my Excel sheet I have these dates
I think that the problem is that the CDATE is not working on NULL values which is normal and I have to find a way to convert only if not null, but I don't know how.
Anyone can help please ?


If Cell.Value <> vbNullStringwould do itMySql = "CDATE([ClipDate]) BETWEEN " & QUO & CDate(Search.txtClipFrom.value) & QUO & " AND " & QUO & CDate(Search.txtClipTo.value) & QUOBefore this I add a where and the select of course... and the QUO means a constant containing """" to add quotes and the code that I provided above is the result of the Debug.print of my variable I think what I need in my code is to tell it, don't convert NULL value into dates in selectMySql = "[ClipDate] >= #" & CDate(Search.txtClipFrom.value) & "# AND [ClipDate] <= #" & CDate(Search.txtClipTo.value) & "#"MySql = "[ClipDate] BETWEEN #" & CDate(Search.txtClipFrom.value) & "# AND #" & CDate(Search.txtClipTo.value) & "#"but still would like to know is there a way to say convert to date the clipdate if not null and don't covert if null ? in my sql ?