ADODB issue with query on date

webster188

New Member
Joined
Oct 20, 2010
Messages
31
Hello,

Can anyone help me with this issue which is driving me crazy.

I have a worksheet (not formatted as a table) with a huge amount of data.
I wanted to query for data older than a specific date.

I wrote the following function (sorry about the "select case", I still need to find a better solution for this)

Code:
Public Function TotalPIRs(Maand As String)

  Dim EndRow As Integer
  Dim Datum As Date
  
  
  Select Case Maand
    Case "Jan"
        Datum = 43496
    Case "Feb"
        Datum = 43524
    Case "Mar"
        Datum = 43555
    Case "Apr"
        Datum = 43585
  End Select
     
     
  '//Find end of PIRLIST
  EndRow = PIRList.Range("A65536").End(xlUp).Row


  '//Now, search the database for matches on trigger
  '//SQL statement to search the database
  
  strSQL = "SELECT * from [PIRlijst$] WHERE [INITDATE] <= '" & Datum & "'" 


  'now extract data
  closeRS
  OpenDB
        
  rs.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
    
  '//Check if recordset contains data
  TotalPIRs = rs.RecordCount


INITDATE is the header of the worksheet column which contains the date of initiation. The fields in this column are formatted as dates.


In Cell A5 of worksheet "Analysis", I have the text "Jan", in cell B5 of the same worksheet, i have "=TotalPIRs(A5)"
The formula outputs "#VALUE!" and I have no clue why this is.

Note: I also tried following query
strSQL = "SELECT * from [PIRlijst$] WHERE [INITDATE] <= " & Datum

In that case, the formula output is 0 (zero) although I have plenty of rows with INITDATE older than the searchdate.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try:
Code:
    strSQL = "SELECT * from [PIRlijst$] WHERE [INITDATE] <= {ts '" & Format(Datum, "yyyy-mm-dd 00:00:00") & "'}"
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top