Displaying dates by weeks in Report

  • Thread starter Thread starter Alaska1
  • Start date Start date
A

Alaska1

I have to display data by weeks in a report. What is the best way to do it?

weeks
5/24/2010 - 5/28/2010 5 time sheets returned
 
On Fri, 28 May 2010 06:10:01 -0700, Alaska1

I use a function that turns a date into a weeknumber in the format
yyyy-ww, which I can then group by. The function uses the Format
function to do this conversion.

-Tom.
Microsoft Access MVP
 
Is that a built in function in access?

Tom van Stiphout said:
On Fri, 28 May 2010 06:10:01 -0700, Alaska1

I use a function that turns a date into a weeknumber in the format
yyyy-ww, which I can then group by. The function uses the Format
function to do this conversion.

-Tom.
Microsoft Access MVP


.
 
Is that a built in function in access?
Yes.
Format([YourDateTimeField], "yyyy-ww")

For the following dates you get --
entrydate Year-Wk
5/22/2010 2010-21
5/23/2010 2010-22
5/24/2010 2010-22
5/25/2010 2010-22
5/26/2010 2010-22
5/27/2010 2010-22
5/28/2010 2010-22
5/29/2010 2010-22
5/30/2010 2010-23
5/31/2010 2010-23

This is based on Sunday being the first day of the week but some payroll
systems have the week starting on other days.

To use week starting on Saturday use this --
Format([entrydate]+1,"yyyy-ww")
 
Thank you it work and i can group on the week but in a report it will read
2010-21

KARL DEWEY said:
Yes.
Format([YourDateTimeField], "yyyy-ww")

For the following dates you get --
entrydate Year-Wk
5/22/2010 2010-21
5/23/2010 2010-22
5/24/2010 2010-22
5/25/2010 2010-22
5/26/2010 2010-22
5/27/2010 2010-22
5/28/2010 2010-22
5/29/2010 2010-22
5/30/2010 2010-23
5/31/2010 2010-23

This is based on Sunday being the first day of the week but some payroll
systems have the week starting on other days.

To use week starting on Saturday use this --
Format([entrydate]+1,"yyyy-ww")

--
Build a little, test a little.


Alaska1 said:
Is that a built in function in access?
 
Thank you for taking the time to answer my question. The Format([The Date],
"yyyy-mm") would work to sum and group the date but 2010-17 is not going to
be familiar to those looking at the report.

I will try your public function. Just not sure, I am doing the function in
the report or the query?

KenSheridan via AccessMonster.com said:
The Format function is, so you could call it directly with:

Format([The Date], "yyyy-mm")

Or you could wrap that in a custom function, which is what Tom is suggesting.
However, a more flexible solution is to return the week-starting date for a
date with a function like this:

Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
As Variant

' Returns 'week starting' date for any date

' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. vardate - optional date value for which week starting
' date to be returned. Defaults to current date

If IsMissing(varDate) Then varDate = VBA.Date

If Not IsNull(varDate) Then
WeekStart = varDate - Weekday(varDate, intStartDay) + 1
End If

End Function

This allows you to specify on which day the week starts (Monday by the look
of it in your case, so you'd pass 2 into the function as the first argument)
and defaults to the current date if no date is passed into the function. So,
calling it today, 2010-05-28:

WeekStart(2) returns 24/05/2010

WeekStart(2,#2010-06-01#) returns 31/05/2010

or for Sunday as the week starting day:
WeekStart(1,#2010-06-01#) returns 30/05/2010

The return values here are in the dd/mm/yyyy UK format, but would be in
mm/dd/yyyy format on your system of course.

You can call the function in the report's underlying query:

WeekStarting:WeekStart(2,[YourDateField])

and then group the report on the WeekStarting column.

Ken Sheridan
Stafford, England
Is that a built in function in access?
I use a function that turns a date into a weeknumber in the format
yyyy-ww, which I can then group by. The function uses the Format
[quoted text clipped - 8 lines]
5/24/2010 - 5/28/2010 5 time sheets returned
.

--
Message posted via AccessMonster.com


.
 
Add one more field for display purposes --
Week_Period: [entrydate]-Weekday([entrydate]) & " - " &
[entrydate]-Weekday([entrydate])+6

--
Build a little, test a little.


Alaska1 said:
Thank you it work and i can group on the week but in a report it will read
2010-21

KARL DEWEY said:
Is that a built in function in access?
Yes.
Format([YourDateTimeField], "yyyy-ww")

For the following dates you get --
entrydate Year-Wk
5/22/2010 2010-21
5/23/2010 2010-22
5/24/2010 2010-22
5/25/2010 2010-22
5/26/2010 2010-22
5/27/2010 2010-22
5/28/2010 2010-22
5/29/2010 2010-22
5/30/2010 2010-23
5/31/2010 2010-23

This is based on Sunday being the first day of the week but some payroll
systems have the week starting on other days.

To use week starting on Saturday use this --
Format([entrydate]+1,"yyyy-ww")

--
Build a little, test a little.


Alaska1 said:
Is that a built in function in access?

:

On Fri, 28 May 2010 06:10:01 -0700, Alaska1

I use a function that turns a date into a weeknumber in the format
yyyy-ww, which I can then group by. The function uses the Format
function to do this conversion.

-Tom.
Microsoft Access MVP


I have to display data by weeks in a report. What is the best way to do it?

weeks
5/24/2010 - 5/28/2010 5 time sheets returned
.
 
I have used the code you provided in the module calling it Function.

I am getting an error on the second line As Variant

Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
As Variant

' Returns 'week starting' date for any date

' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. vardate - optional date value for which week starting
' date to be returned. Defaults to current date

If IsMissing(varDate) Then varDate = VBA.Date

If Not IsNull(varDate) Then
WeekStart = varDate - Weekday(varDate, intStartDay) + 1
End If
Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
As Variant

' Returns 'week starting' date for any date

' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. vardate - optional date value for which week starting
' date to be returned. Defaults to current date

If IsMissing(varDate) Then varDate = VBA.Date

If Not IsNull(varDate) Then
WeekStart = varDate - Weekday(varDate, intStartDay) + 1
End If

I have added it into the query as a column

Week: WeekStart([CompletedandReturnedDate])
with CompletedandReturnedDate having the date in that field. WeekStart is
my public function name. I am getting an error in the query. It is not
pulling any data for that field.

KenSheridan via AccessMonster.com said:
Best to do it in the query as a computed column in the way I described in my
last post. Then you can group on the column and include a text box in the
group header with a control source such as:

="Week starting " & [WeekStarting]

You can of course format the [WeekStarting] value in any way you like, e.g.

="Week starting " & Format([WeekStarting], "dddd mmmm dd yyyy")

would give you a heading in the format:

Monday May 31 2010

Ken Sheridan
Stafford, England
Thank you for taking the time to answer my question. The Format([The Date],
"yyyy-mm") would work to sum and group the date but 2010-17 is not going to
be familiar to those looking at the report.

I will try your public function. Just not sure, I am doing the function in
the report or the query?
The Format function is, so you could call it directly with:
[quoted text clipped - 53 lines]
5/24/2010 - 5/28/2010 5 time sheets returned
.

--
Message posted via AccessMonster.com


.
 
Thank you for all your help. It worked fine. I moved the as variant up to
the first line and added the 2 in the query.

Appreciate all your help.

KenSheridan via AccessMonster.com said:
That's because your newsreader has split the line over two. It should all be
a single line. This is something you need to watch out form when code is
posted here.

However, you must include the starting day of the working week, which in your
case is a Monday, so the value to be passed into the function is 2:

Week: WeekStart(2,[CompletedandReturnedDate])

The second argument is optional if you want to return the start date for the
week of the current date, but in your case it's the starting date of the week
for the CompletedandReturnedDate so you need both arguments.

I notice that some of the code is repeated in your last post?? The function
should be pasted into a module exactly as I posted it, and the spurious line
break in the first line removed:

Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
As Variant

' Returns 'week starting' date for any date

' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. vardate - optional date value for which week starting
' date to be returned. Defaults to current date

If IsMissing(varDate) Then varDate = VBA.Date

If Not IsNull(varDate) Then
WeekStart = varDate - Weekday(varDate, intStartDay) + 1
End If

End Function

Ken Sheridan
Stafford, England
I have used the code you provided in the module calling it Function.

I am getting an error on the second line As Variant

Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
As Variant

' Returns 'week starting' date for any date

' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. vardate - optional date value for which week starting
' date to be returned. Defaults to current date

If IsMissing(varDate) Then varDate = VBA.Date

If Not IsNull(varDate) Then
WeekStart = varDate - Weekday(varDate, intStartDay) + 1
End If
Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
As Variant

' Returns 'week starting' date for any date

' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. vardate - optional date value for which week starting
' date to be returned. Defaults to current date

If IsMissing(varDate) Then varDate = VBA.Date

If Not IsNull(varDate) Then
WeekStart = varDate - Weekday(varDate, intStartDay) + 1
End If

I have added it into the query as a column

Week: WeekStart([CompletedandReturnedDate])
with CompletedandReturnedDate having the date in that field. WeekStart is
my public function name. I am getting an error in the query. It is not
pulling any data for that field.
Best to do it in the query as a computed column in the way I described in my
last post. Then you can group on the column and include a text box in the
[quoted text clipped - 25 lines]
5/24/2010 - 5/28/2010 5 time sheets returned
.

--
Message posted via AccessMonster.com


.
 
Just one more question. I have to group the date by the week and by person.
What is the best way to do it?

4/19/2010
Tom Smith 15 returns

KenSheridan via AccessMonster.com said:
That's because your newsreader has split the line over two. It should all be
a single line. This is something you need to watch out form when code is
posted here.

However, you must include the starting day of the working week, which in your
case is a Monday, so the value to be passed into the function is 2:

Week: WeekStart(2,[CompletedandReturnedDate])

The second argument is optional if you want to return the start date for the
week of the current date, but in your case it's the starting date of the week
for the CompletedandReturnedDate so you need both arguments.

I notice that some of the code is repeated in your last post?? The function
should be pasted into a module exactly as I posted it, and the spurious line
break in the first line removed:

Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
As Variant

' Returns 'week starting' date for any date

' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. vardate - optional date value for which week starting
' date to be returned. Defaults to current date

If IsMissing(varDate) Then varDate = VBA.Date

If Not IsNull(varDate) Then
WeekStart = varDate - Weekday(varDate, intStartDay) + 1
End If

End Function

Ken Sheridan
Stafford, England
I have used the code you provided in the module calling it Function.

I am getting an error on the second line As Variant

Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
As Variant

' Returns 'week starting' date for any date

' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. vardate - optional date value for which week starting
' date to be returned. Defaults to current date

If IsMissing(varDate) Then varDate = VBA.Date

If Not IsNull(varDate) Then
WeekStart = varDate - Weekday(varDate, intStartDay) + 1
End If
Public Function WeekStart(intStartDay As Integer, Optional varDate As Variant)
As Variant

' Returns 'week starting' date for any date

' Arguments:
' 1. intStartDay - weekday on which week starts, 1-7 (Sun - Sat)
' 2. vardate - optional date value for which week starting
' date to be returned. Defaults to current date

If IsMissing(varDate) Then varDate = VBA.Date

If Not IsNull(varDate) Then
WeekStart = varDate - Weekday(varDate, intStartDay) + 1
End If

I have added it into the query as a column

Week: WeekStart([CompletedandReturnedDate])
with CompletedandReturnedDate having the date in that field. WeekStart is
my public function name. I am getting an error in the query. It is not
pulling any data for that field.
Best to do it in the query as a computed column in the way I described in my
last post. Then you can group on the column and include a text box in the
[quoted text clipped - 25 lines]
5/24/2010 - 5/28/2010 5 time sheets returned
.

--
Message posted via AccessMonster.com


.
 
Back
Top