datems-accessdesign-view

MS Access 2010 (Design View): return Monday of the current week with Monday as 1st day of the week


I need to make my Access query always return the Monday of the current week. I have seen a few solutions on Google/StackOverflow but they are written in SQL and I am a beginner in creating Access queries (I am using the Design view to make them).

Goal: The week should be considered as M T W T F S S. Then, the query should always return the Monday of the current week. Therefore, if it is Sunday, it should still return the Monday before, NOT the next week's Monday. Can anyone explain how to do this using the Design View in Access 2010?


Solution

  • Keep in mind that in this context we are working with dates, so if we do Date() - 1, we will get 1 day prior to today.

    Date() ~ Today's date

    DatePart(
            "w" - Weekday
            Date() - Today's date
            2 - vBMonday (Access assumes Sunday is the first day of the week, which is why this is necessary.)
            1 - vbFirstJan1 - This gets into using the first week of the year. We could have omitted this, as 1 is the default.
    )
    
    -1 - Subtract 1 from the DatePart value.
    

    Values

    Date() = 4/27/2015 (at time of this writing)
    DatePart("w",Date(),2,1) = 1
    DatePart("w",Date(),2,1)-1 = 0
    

    So we have Date()-0... Okay, what's so great about that? Well, let's look at a more useful scenario where today's date is a day other than Monday.

    Let's act like today is 4/28/2015 (Tuesday)

    Date() = 4/28/2015
    DatePart("w",Date(),2,1) = 2
    DatePart("w",Date(),2,1)-1 = 1
    

    So, from the outside, in; give me the current weekday value. (1 = Monday, 2 = Tuesday, etc.), and subtract 1 from that -> that's how many days we need to subtract from the current date to get back to the weekday value of 1 (Monday).