Solved

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

Posted on 2014-09-16
4
23 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
4 Comments
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero 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 49

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now