Trouble with date comparison in Access 2007

Posted on 2007-12-04
Last Modified: 2013-11-28
I'm have a bit of trouble with some date comparison code in Access 2007 & VB 6.5. I have a date/time field in a table formatted as dd/mm/yyyy hh:nn:ss. User input is through a form containing a field with the same formatting. I need the form to display a record based on the date/time input on the form.  When i run the code below, the database returns null.

I have even tried copying & pasting the data straight out of the database and replacing the cboQuoteDate variable with it, to no avail. I am aware of the way Access stores dates, but I'm not sure of the functions or methods required to convert them to a usable format. What I am sure of though is that this *is* simply a formatting issue (i hope!).

quote_id & quote_date are is a fields in table Quotation, and cboQuoteDate is a text field on my form. I'm a self-taught newbie when it comes to VB, so please bear with me!
Private Sub btnGetQuote_Click()
'get the quote number
Dim SQL As String
Dim rs As ADODB.Recordset
Dim quoteNo As Variant
SQL = "SELECT quote_id FROM QUOTATION WHERE quote_date = #" & cboQuoteDate.Value & "#"
Set rs = New ADODB.Recordset
rs.Open SQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
quoteNo = rs.GetRows
End Sub

Open in new window

Question by:mistryn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
LVL 75
ID: 20404142

SQL = "SELECT quote_id FROM QUOTATION WHERE quote_date =CDate( #" & cboQuoteDate.Value & "#")

LVL 75
ID: 20404160

SQL = "SELECT quote_id FROM QUOTATION WHERE quote_date = #" & CDate(cboQuoteDate.Value) & "#"


SQL = "SELECT quote_id FROM QUOTATION WHERE quote_date = # " & DateValue(cboQuoteDate.Value) & "#"

LVL 75
ID: 20404169
Sorry ... ignore my very first post ...

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Comment

ID: 20404358
Thanks very much databaseMX!  The cdate function seems to do what I want it to, and perhaps a little more.  It's swapping the day and month fields, so 04/12/2007 17:27:37 becomes 12/04/2007 17:27:37. I've had this problem before, and had to rig the VB code to swap the month/day. I'm sure there's a more elegant way to do this...any advice?
LVL 51

Accepted Solution

Gustav Brock earned 50 total points
ID: 20404466
The way to handle this is, for SQL, always to format to the "reverse" US standard:

strDateTime = Format(cboQuoteDate.Value, "mm\/dd\/yyyy hh\:nn\:ss AM/PM")
SQL = "SELECT quote_id FROM QUOTATION WHERE quote_date = #" & strDateTime & "#"

Never concatenate a string and a date value directly.

LVL 75
ID: 20404534
"any advice?"

Pretty much what gustav just said ....


Author Comment

ID: 20458225
Thanks again gustav. The format function worked perfectly!
LVL 51

Expert Comment

by:Gustav Brock
ID: 20458411
You are welcome!


Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question