Solved

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

Posted on 2014-09-16
4
36 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 51

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

622 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