Solved

DoCmd.OpenReport with Where Condition on Date

Posted on 2010-11-17
14
1,743 Views
Last Modified: 2012-05-10
I'm working in VB2008, but using Microsoft.Office.Interop.Access.Application to print reports from Access.

I have a report with a query as the recordsource. I'm trying to fill my report based on a date field (RptDate) in the query. I have tried many different ways to pass the date parameter in the "Where condition" to the report, but without success. The report is printed either with all records in the query or no records.

For example, one of the attempts:

Dim Date1 As String
Date1 = dtp1.Value.ToShortDateString()
DoCmd.OpenReport "rptReport", AcView.acViewNormal, ,  "RptDate= #" & Date1 & "#"

This example results in a report showing all records in the query instead of only records with the date requested.

Can someone provide the correct method to use?
0
Comment
Question by:Tim313
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
ID: 34158180
Try using this format for date
Date1 = Format(dtp1.Value, "yyyy-mm-dd")
0
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 34158221
You must also sometimes use a Formatted field in the report's query to be able to fully compare dates. For example, instead of this:

SELECT CustID, CustName, OrderDate FROM SomeTable

You'd use

SELECT CustID, CustName, Format(OrderDate, 'mm/dd/yyyy') As MyOrderDate FROM SomeTable

And then you'd filter like this:

Dim Date1 As String
Date1 = Format(dtp1.Value.ToShortDateString(), "mm/dd/yyyy")
DoCmd.OpenReport "rptReport", AcView.acViewNormal, ,  "MyOrderDate= #" & Date1 & "#"

There's also DateSerial you can use to build your data correctly.


0
 
LVL 30

Expert Comment

by:hnasr
ID: 34158904
Set a break point on this line
DoCmd.OpenReport "rptReport", AcView.acViewNormal, ,  "RptDate= #" & Date1 & "#"

When the program stops at the line, type ? Date1 in immediate window.
Compare the format you get with that of SomeTable, by showing the table in datasheet view.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 34159226
What regional settings for dates are you using?  in your query, you need to make sure the date is formatted as "mm/dd/yyyy", as LSM has indicated in his examples.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34159321
fyed

Why not the first comment, which uses yyyy-mm-dd and will work regardless of regional setting?
0
 

Author Comment

by:Tim313
ID: 34159924
With dtp1.Value = 11/16/2010 and Dim Date1 As String, ? Date1 shows "11/16/2010"
With dtp1.Value = 11/16/2010 and Dim Date1 As Date, ? Date1 shows #11/16/2010#
The "RptDate" column in the table (datasheet view) shows mm/dd/yyyy
The format for the data table field "RptDate" and the "dtp1 are both set to ShortDate.
 
If I add the WHERE clause to the query (example: WHERE tblReport.RptDate=#11/16/2010#), the query returns only records with RptDate = 11/16/2010...

Any ideas?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34159990
> The format for the data table field "RptDate" and the "dtp1 are both set to ShortDate.

That doesn't matter one bit.  The format only applies to display when used in reports, or in Access datasheet.  In a WHERE clause, it works on the date "value", some internal numeric.

> If I add the WHERE clause to the query (example: WHERE tblReport.RptDate=#11/16/2010#), the query returns only records with RptDate = 11/16/2010...

What is the problem with that statement please?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 84
ID: 34160150
"If I add the WHERE clause to the query (example: WHERE tblReport.RptDate=#11/16/2010#), the query returns only records with RptDate = 11/16/2010..."

Isn't that what you would expect?


0
 

Author Comment

by:Tim313
ID: 34160646
Yes, what I would expect... just to confirm the query works given the date parameter. It also works if I use: WHERE tblReport.RptDate = ? and 11/16/2010 is entered into the "Enter Parameter" box. Why doesn't the report return the same results using the DoCmd.OpenReport statement? My query returns all records for the fields I need in the report, I thought the report would then filter the applicable records based on the "Where Condition" of the DoCmd.OpenReport statement.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34160664
Tim313, based on the first comment, have you tried this?

Dim Date1 As String
'Date1 = dtp1.Value.ToShortDateString()
Date1 = Format(dtp1.Value, "yyyy-mm-dd")
DoCmd.OpenReport "rptReport", AcView.acViewNormal, ,  "RptDate= #" & Date1 & "#"

Yes, your expectation would be correct, the criteria makes up a WHERE clause for the recordset behind rptReport.
0
 

Author Comment

by:Tim313
ID: 34160863
Thanks to all for your response. I'm home now and won't return to work until 6AM EST. I'll try your suggestions first thing...
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 34160876
cyber,

my bad.  This is the second time in the last month that an assumption I had about dates proved to be incorrect.
0
 

Author Comment

by:Tim313
ID: 34163469
LSM - Regarding your suggestion:

>You'd use
>SELECT CustID, CustName, Format(OrderDate, 'mm/dd/yyyy') As MyOrderDate FROM SomeTable
>And then you'd filter like this:
>Dim Date1 As String
>Date1 = Format(dtp1.Value.ToShortDateString(), "mm/dd/yyyy")
>DoCmd.OpenReport "rptReport", AcView.acViewNormal, ,  "MyOrderDate= #" & Date1 & "#"

With dtp1.value = 11/16/2010
Returns Date1 = "mm/dd/yyyy" and causes error.
I made the following change:
from -  Date1 = Format(dtp1.Value.ToShortDateString(), "mm/dd/yyyy")
to -  Date1 = Format(dtp1.Value, "mm/dd/yyyy")
which returned Date1 = #29/16/2010#
also on subsequent attempts the "mm" would change (ie 54, 59)

cyber- Regarding your suggestion:

>Dim Date1 As String
>'Date1 = dtp1.Value.ToShortDateString()
>Date1 = Format(dtp1.Value, "yyyy-mm-dd")
>DoCmd.OpenReport "rptReport", AcView.acViewNormal, ,  "RptDate= #" & Date1 & "#"

With dtp1.value = 11/16/2010
Similar results were returned having the "mm" change with each try.

Each time I ran the code the resulting value of "mm" in "mm/dd/yyyy" or "yyyy-mm-dd" would coinside with the minutes shown on my system's clock...

0
 

Author Comment

by:Tim313
ID: 34165273
Considering the problems I was having with this report, I decided to load a backup of this program that was saved prior to the creation of the report in question. I re-created the query and report and the problem did not reappear. This is not the first time I've had to resort to a backup & rewrite for some strange problem. I suspect I need to reinstall the VS2008 application...

Although LSM Consulting & cyperkiwi did not supply a direct solution, trying your suggestions lead me to believe that the problem required a rewrite. I'm splitting the points.
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

12 Experts available now in Live!

Get 1:1 Help Now