On a form there is a Start Date and End Date, both form fields formatted as
Short Date. In the query, the field DatePaid has criteria >=
forms!frmPaymentsSummary.txtStartDate AND <=
forms!frmPaymentsSummary.txtEndDate.
When I run the query with a date range of a single day (to get all payments
processed on that day), only one record is coming up. If I run the date
range on that day and ending on the next, I get all of the records processed
on that day. For example, today 142 records were processed. If I run the
date range of 8/8/07 to 8/8/07, then I get the one record. If I run the
range from 8/8/07 to 8/9/07, then I get the 142 records.
Any ideas?
Consider these examples (substitute the name of a populated table for
'MyTable' in the queries).
Example 1: use closed-closed representation (assumes DATETIME values
are accurate to one second granularity) and BETWEEN which is optimized
for Access/Jet:
SELECT DT1.test_date
FROM
(
SELECT DISTINCT #2001-01-01 00:00:00# AS test_date
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-01 12:00:00#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-01 23:59:59#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-02 00:00:00#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-02 12:00:00#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-02 23:59:59#
FROM MyTable
) AS DT1
WHERE DT1.test_date BETWEEN #2001-01-01 00:00:00# AND #2001-01-01
23:59:59#
Example 2: use closed-open representation which is good for DATETIME's
floating point nature (i.e. no assumption one second granularity):
SELECT DT1.test_date
FROM
(
SELECT DISTINCT #2001-01-01 00:00:00# AS test_date
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-01 12:00:00#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-01 23:59:59#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-02 00:00:00#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-02 12:00:00#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-02 23:59:59#
FROM MyTable
) AS DT1
WHERE #2001-01-01 00:00:00# <= DT1.test_date
AND DT1.test_date < #2001-01-02 00:00:00#;
Example 3: the OP's original, which erroneously returns values for the
next day:
SELECT DT1.test_date
FROM
(
SELECT DISTINCT #2001-01-01 00:00:00# AS test_date
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-01 12:00:00#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-01 23:59:59#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-02 00:00:00#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-02 12:00:00#
FROM MyTable
UNION ALL
SELECT DISTINCT #2001-01-02 23:59:59#
FROM MyTable
) AS DT1
WHERE DT1.test_date >= #2001-01-01 00:00:00#
AND DT1.test_date <= #2001-01-02 00:00:00#;
I use the Example 1 approach because BETWEEN is more human readable
(being optimized is a bonus) and I find open-open representation
(where the end date falls outside the period) non-intuitive. Who
knows, maybe the floating point DATETIME storage will one day be
replaced by scaled integers as per SQL Server (breath not being held
<g>) so why code based on current physical implementation
characteristics?
Jamie.
--