0

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

1
  • What format are your dates in? Commented Dec 21, 2009 at 18:49

3 Answers 3

3
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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

I disagree. Using parameterized SQL queries is much harder to work with when you have to go back to maintain and test the code.
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.
@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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.