Solved

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

Posted on 2014-09-16
4
16 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
Comment Utility
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
Comment Utility
Thank you, I'll try it!
0
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 250 total points
Comment Utility
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 26

Expert Comment

by:MacroShadow
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
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.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

771 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

16 Experts available now in Live!

Get 1:1 Help Now