Solved

MS Access - change start of week to Monday from Sunday (US)

Posted on 2014-09-16
4
31 Views
Last Modified: 2016-06-23
Info you might need: in the US; Access 2010; program someone else wrote; I've only been doing this 6 months.

I'm trying to run queries (and build reports on them) for dates that fall into a "last week" time frame where the week runs Monday to Sunday.

90% of the time, this will be run on a Monday, but I don't want to use Between Date()-7 and Date()-1 because there are times when it might be pulled later in the week. I want the reports to be able to be pulled by clerical people by just pushing a button, not doing anything complicated.

I found this:
   Year([FieldName])* 53 + DatePart("ww", [FieldName]) = Year(Date())* 53 + DatePart("ww", Date()) - 1

And it works like I want, except it returns Sunday to Saturday.

I thought changing my computer settings through control panel to have the week start Monday would help this (changing globally on my computer), but it's still returning Sunday to Saturday.

I've found a couple of things referencing vbMonday for first day of week in specific bits of code (primarily to return a day of the week for a date, but I'm not finding the correct syntax for what I'm looking for. I'd like to set it as a global constant so it covers the whole database.

Can I do this?
0
Comment
Question by:Jennifer Wagner
[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
4 Comments
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 40326083
you  will need a user define function to do this,
place this codes in a regular module

Function GetLastWeekMonDate(CurrentDate) As Date
   If VarType(CurrentDate) <> 7 Then
      GetLastWeekMonDate = Null
   Else
      Select Case Weekday(CurrentDate)
         Case 1       ' Sunday
            GetLastWeekMonDate = CurrentDate - 6
         Case 2       ' Monday
            GetLastWeekMonDate = CurrentDate - 7
         Case 3 To 7  ' Tuesday..Saturday
            GetLastWeekMonDate = CurrentDate - Weekday(CurrentDate) - 5
       End Select
   End If
End Function

Open in new window


in your query, use the criteria

Between  GetLastWeekMonDate(Date) And GetLastWeekMonDate(Date) + 6
0
 

Author Comment

by:Jennifer Wagner
ID: 40326107
Thank you, I'll try it!
0
 
LVL 50

Accepted Solution

by:
Gustav Brock earned 250 total points
ID: 40327433
I don't know where you found that code but it is far out.

What you need/miss is the weekday parameter used in many VB(A) date functions as in these functions which will fit your purpose:
Public Function DateWeekFirst( _
  ByVal datDate As Date, _
  Optional ByVal lngFirstDayOfWeek As VbDayOfWeek = vbUseSystemDayOfWeek) _
  As Date

' Returns the first date of the week of datDate.
' lngFirstDayOfWeek defines the first weekday of the week.
' 2000-09-07. Cactus Data ApS.
' 2003-05-01. System settings used as default.
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek.
    
  DateWeekFirst = DateAdd("d", vbSunday - Weekday(datDate, lngFirstDayOfWeek), datDate)
    
End Function

Public Function DateWeekLast( _
  ByVal datDate As Date, _
  Optional ByVal lngFirstDayOfWeek As Long = vbUseSystemDayOfWeek) _
  As Date

' Returns the last date of the week of datDate.
' lngFirstDayOfWeek defines the first weekday of the week.
' 2000-09-07. Cactus Data ApS.
' 2003-05-01. System settings used as default.
' 2012-10-44. Data type of lngFirstDayOfWeek changed to VbDayOfWeek.
    
  DateWeekLast = DateAdd("d", vbSaturday - Weekday(datDate, lngFirstDayOfWeek), datDate)
    
End Function

Open in new window

Note the use of the VbDayOfWeek enumeration for the second parameter of Weekday. That's the magic that will run weeks from Monday to Monday.

/gustav
0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 41668923
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Gustav Brock (https:#a40327433)
-- Rey Obrero (https:#a40326083)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

MacroShadow
Experts-Exchange Cleanup Volunteer
0

Featured Post

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!

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

740 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