?
Solved

DoCmd.OpenReport with Where Condition on Date

Posted on 2010-11-17
14
Medium Priority
?
2,051 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
[X]
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
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 58

Accepted Solution

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

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Dale Fye
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
 
LVL 85
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 48

Expert Comment

by:Dale Fye
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

801 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