Question

I spent much time trying to figure out how to extract date part in ado recordset filter expressions connecting to Jet engine working with an mdb file. The problem is that many things mentioned about access flavor of sql (date function for example) don't work there raising errors. Formatting dates with #mm/dd/yyyy hh:mm:ss# in comparisons works but gives incorrect results. Is there reliable source of information of what kind of expressions work for filters and what functions I can use?

UPDATE

The version used is when I choose Microsoft JET 4.0 OLE DB Provider. Generally one would expect that filter criteria can use the same syntax as the parts of the queries coming after WHERE keyword in SQL queries. My task was to compare date parts of time stamps and I finally decided to use query instead of filtered table, but the following example works when it's the part of the sql query (after WHERE) and raises "The application is using arguments that are of the wrong type, are out of acceptable range, or are in conflict with one another" error when it's the contents of the filter

format(TimeStamp,"yyyy/mm/dd")=format(#04/11/2013#,"yyyy/mm/dd")

So I see there's obvious differences between WHERE and filter syntax, but I could not find detailed explanation what exactly are they.

Was it helpful?

Solution

I'm actually quite surprised that WHERE Format([TimeStamp]... works in an ADO query against the Access Database Engine (ACE), but apparently it does.

I certainly agree that specific details on using some Microsoft features can be difficult to find in Microsoft's documentation. I guess that helps keep sites like Stack Overflow in business. ;)

As for your .Filter question, using Format() in this context does fail, presumably because Format() is a VBA function and is not (always) available to an expression outside of the Access application itself. However, the following test shows that...

rst.Filter = "[TimeStamp] >= #2013/04/11# AND [TimeStamp]<#2013/04/12#"

...does work. (When no time is specified for a DateTime value then midnight - 00:00:00 - is assumed.)

Test data:

ID  TimeStamp
1   2013-04-10 21:22:00
2   2013-04-11 02:34:56
3   2013-04-11 04:45:15

Test code:

Sub foo()
Dim con As ADODB.Connection, rst As ADODB.Recordset

Set con = New ADODB.Connection
con.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=C:\Users\Gord\Desktop\Database1.accdb;"
Set rst = New ADODB.Recordset

Debug.Print "Test 1: WHERE Format([TimeStamp]..."
rst.Open _
        "SELECT * FROM [TimeStampData] " & _
        "WHERE Format([TimeStamp], ""yyyy/mm/dd"") = Format(#2013/04/11#, ""yyyy/mm/dd"")", _
        con, adOpenKeyset, adLockOptimistic
Debug.Print "Records returned: " & rst.RecordCount
rst.Close
Debug.Print

Debug.Print "Test 2: Filter"
rst.Open "SELECT * FROM [TimeStampData]", con, adOpenKeyset, adLockOptimistic
Debug.Print "Total records: " & rst.RecordCount
rst.Filter = "[TimeStamp] >= #2013/04/11# AND [TimeStamp]<#2013/04/12#"
Debug.Print "Filtered records: " & rst.RecordCount
rst.Close

Set rst = Nothing
con.Close
Set con = Nothing
End Sub

Test results:

Test 1: WHERE Format([TimeStamp]...
Records returned: 2

Test 2: Filter
Total records: 3
Filtered records: 2

OTHER TIPS

A short note on the (VBA) ADO filter syntax (applies also to DAO):

The filter should be specified as: "[Fieldname] = " Where Fieldname is an existing name of a field in the recordset and can be anything that can be represented by a string. A non-string is allways converted to a string as the filtervalue will be transformed into an explicit SQL WHERE statement (Allways a string).

Valid filters would be:

rst.Filter="[TimeStamp] = #2013/04/12#" '(Mind the hashes as a date is expected. Peculiarly all localised notations are accepted!)

rst.Filter="[TimeStamp] = #" & strDatevalue & "#" 'Where strDatevalue is a datevalue as text.

Hence this will work:

rst.Filter="[TimeStamp] = #" & format(#04/11/2013#,"mm/dd"/yyyy) & "#" 
'Mind: Access expects here an American standard date format, i.e. month/day/year
'(In that case you could even leave the hashes away!)

IF
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top