Solved

DoCmd.OpenReport with Where Condition on Date

Posted on 2010-11-17
14
1,825 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
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.

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

777 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