Date Range for Query

R

RLN

I am modifying a query where I need to provide the user with a "From" and a
"To" date input boxes, then run the query based on those two dates as filter
criteria.
The user also wants to return the total number of calendar days *and* total
number of business days between those two dates the user chose.

Does anyone have some VBA examples of how to do this?

Can this date range also be used on a report object or can this only be done
on queries?

many thnx....
 
P

pietlinden

I am modifying a query where I need to provide the user with a "From" anda
"To" date input boxes, then run the query based on those two dates as filter
criteria.
The user also wants to return the total number of calendar days *and* total
number of business days between those two dates the user chose.

Does anyone have some VBA examples of how to do this?

Can this date range also be used on a report object or can this only be done
on queries?

many thnx....

Easiest way is to have the user enter the dates on an unbound form.
You can use whatever calendar UI you want. Stephen Lebans has a nice
one at his website. www.lebans.com
Then you would create a button to open your report and pass the filter
when you open the report.

dim dtStart as Date
dim dtEnd as Date
dtStart = me.txtStartDate
dtEnd = me.txtEndDate
strFilter="BETWEEN #" & dtStart & "# AND #" & dtEnd &"#"

Then you could create unbound controls on your report that do the date
math for you.
There's a BusinessDays function at www.datastrat.com in the downloads
section.
Calendar days are easy: DateDiff("d",dtStart, dtEnd)

DoCmd.OpenReport "ReportName", strFilter
 
M

Marshall Barton

RLN said:
I am modifying a query where I need to provide the user with a "From" and a
"To" date input boxes, then run the query based on those two dates as filter
criteria.
The user also wants to return the total number of calendar days *and* total
number of business days between those two dates the user chose.

Does anyone have some VBA examples of how to do this?

Can this date range also be used on a report object or can this only be done
on queries?


Since a report is normally based on a query, filtering the
report's query is the normal way to apply a date range (or
any other kind of) criteria. A quick and dirty way to do
what you asked is to use parameter prompts in the query by
setting the date field's criteria to something like:

Between [Start Date] And [End Date]

A report header text box can then display the calendar days
can by using an expression like:
=DateDiff("d", [End Date], [Start Date])

"Business days" is not a well defined term and can be very
complex and highly dependent on your business's definition
of them. If you can totally ignore holidays, then you can
use an text box expression like:
=5 * [txtCalendarDays] / 7
but, if that's not sufficent, you will have to maintain a
table of your business's holidays over a significant number
of years. You can then create a function that uses a
recordset to calculate the number of non-weekend holidays
within a date range. This is definitely not a simple
problem.

Before long, you will find that parameter prompt queries are
not the panacea thay may at first appear to be. The full
approach is to create a form with text boxes for users to
enter the parameter values along with a button to p[en the
report. With this technique, the query criteria would look
like:
Between Forms!theform.txtStart And Forms!theform.txtEnd
 
R

RLN

Thanks for your help. I appreciate it.I had forgotten about the DateDiff function. It's been a long time and I
spaced that one.
 
R

RLN

Thanks for the tips here, I am grateful for your help.
Sounds like calculating business days could get cumbersome when trying to
factor in national holidays, etc. I'll stick with just calculating the days
between two dates and call that good for phase I of this project.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top