views:

509

answers:

3

Hi,

How to compare a date/time via VBA with a date/time in an Access DB?

The query I use

adoRS.Open "SELECT * FROM currentpositions WHERE ((currentpositions.
[dateLT])=" & "#" & date_from_message & "#" & ")", adoConn, adOpenStatic,
adLockOptimistic

I only achieve to compare a date.
Anybody an idea?

Regards
Camastanta

+2  A: 
adoRS.Open "SELECT * FROM currentpositions WHERE DateValue(currentpositions.
[dateLT]) = DateValue(" & "#" & date_from_message & "#)", adoConn, adOpenStatic,
adLockOptimistic

See, if the above helps.
DateValue extracts the date part from a given date/time. So, it can be used to compare date, ignoring the time part.

shahkalpesh
+1  A: 

IMHO you should never build your SQL statements using string concatenation. Instead use parameterized SQL queries. This will save you from problems like the one you are facing with date/time comparison.

Jakob Christensen
I disagree. Using parameterized SQL queries is much harder to work with when you have to go back to maintain and test the code.
Tony Toews
I would say it depends on where the value you're plugging into the string is coming from. If it's coming from a control the user can't type free text into, you're completely safe in Access. A text field with a date mask or that can only be edited with a date picker control will never be subject to SQL injection in any form.
David-W-Fenton
@Tony: Why do you find it harder to maintain?@David: I don't think it is just a matter of SQL injection. It also saves you from date formatting problems etc. Also, if your Access application is backed by a SQL Server, parameterized queries allow SQL Server to cache the query plans which gives you better performance.
Jakob Christensen
A: 

Does your system have dates in either yyyy-mm-dd or dd-mm-yyyy format? If so see Return Dates in US #mm/dd/yyyy# format to convert your dates in mm-dd-yyyy so Access can work with them properly.

Tony Toews