Solved

Trouble with date comparison in Access 2007

Posted on 2007-12-04
8
936 Views
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

rs.Close
 

End Sub

Open in new window

0
Comment
Question by:mistryn
  • 4
  • 2
  • 2
8 Comments
 
LVL 75
ID: 20404142
Maybe:

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

mx
0
 
LVL 75
ID: 20404160
or

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

or


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

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

mx
0
 

Author Comment

by:mistryn
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?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 49

Accepted Solution

by:
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.

/gustav
0
 
LVL 75
ID: 20404534
"any advice?"

Pretty much what gustav just said ....

mx
0
 

Author Comment

by:mistryn
ID: 20458225
Thanks again gustav. The format function worked perfectly!
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 20458411
You are welcome!

/gustav
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now