Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dates don't match 1

Status
Not open for further replies.

OrthoDocSoft

Programmer
Joined
May 7, 2004
Messages
291
Location
US
Folks,

I have a table "testsdrawn" with a column named "DateDrawn" which holds the "date and time" data type. The date "7/12/2009" is in all three records, and was written there by:

MyRecordset.Fields("DateDrawn") = Date

which works well.

Then I try to query the DB for records like this:

strSQL = "SELECT * FROM testsdrawn WHERE PatientID... blah blah " AND DateDrawn = " & Date

MyRecordSet.Open(strSQL), MyConnection, adoOpenKeyset _
adLockOptimistic

When I delete the date part, the query works fine and returns the correct PatientID, so that part is fine. When I add the lines "AND DateDrawn = " & Date," I get zero records returned (and no sql query errors).

"Date" is a function that returns today's date, and the dates in my column are all of "date and time" type, and are today's date.

Why doesn't this query find them?

Thanks,

Ortho

[lookaround] "you cain't fix 'stupid'...
 
use the Access Date() function instead
Code:
SELECT [i]columns[/i] 
  FROM testsdrawn 
 WHERE PatientID... blah blah 
   [blue]AND DateDrawn >= Date()
   AND DateDrawn  < DateAdd("dd",1,Date())[/blue]
:-)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Another option is this solution which corrects for different date formats:
Code:
strSQL = "SELECT * FROM testsdrawn WHERE PatientID... blah blah " AND DateValue(DateDrawn) = #" & Format(Date, "mm\/dd\/yyyy") & "#"

Duane
Hook'D on Access
MS Access MVP
 
dhookom,

your solution worked like a charm.

r937,

I had lots of formating problems using yours in VB6 (which I failed to tell you about), so it didn't work, but I appreciate you trying.

Star for dhookom.

Thanks, folks

Ortho

[lookaround] "you cain't fix 'stupid'...
 
Why not simply this ?
strSQL = "SELECT * FROM testsdrawn WHERE PatientID... blah blah AND DateDrawn=Date()"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 



I would recommend against using ...
Code:
Where [YourDateField] [red][b]=[/b][/red] Date()
as dates can contain TIME values as well, making this expression FALSE.

Rather...
Code:
Where Int([YourDateField]) = Date()
will include only the DATE component of your field.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top