Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    djclinton15 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    123

    Date Criteria clarification

    I understand how in Access when one wants to create a “weekly” query whether it’s the previous week, the current week, or the next week query will return starting on Sunday and ending on Saturday. However I would like my weekday’s query to start from Monday and end on Sunday. Might anyone here know/tell me how I can create a ‘current weekday’ query & a ‘next week’ query?



    I tried these 2-criteria’ which seemed to work, but don’t know if they are correct & will work in the long run:
    To have a current weekday query staring from Sun to Sat criteria be: Between Date()-3 and Date()+4
    To have a next week query staring from Sun to Sat criteria be: Between Date()+7 and Date()+13

    Please, any advice/suggestion would be greatly appreciated! Thanks in advance!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Those will only work if run on a particular day of the week, as both are relative to the current date. See if this helps:

    https://msdn.microsoft.com/en-us/lib...ffice.14).aspx
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    djclinton15 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    123
    Thanks pbaldy, couldn't find what I needed.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    weekday’s query to start from Monday and end on Sunday
    I came up with these two functions to calculate dates for start date on Monday and end date on Sunday:
    Code:
    Public Function fWeekStartMonday(TheDate As Date) As Date
        Dim NumOfDays As Integer
    
        NumOfDays = Weekday(TheDate) - vbMonday
    
        'return start of week date
        fWeekStartMonday = DateAdd("d", -NumOfDays, TheDate)
    
    End Function
    
    '---------------------------------------------
    
    Public Function fWeekEndSunday(TheDate As Date) As Date
        Dim NumOfDays As Integer
    
        NumOfDays = Weekday(TheDate) - vbMonday
    
        'return end of week date
        fWeekEndSunday = DateAdd("d", 6, DateAdd("d", -(NumOfDays), TheDate))
    
    End Function
    Examples:
    -------------
    (specific date)
    Between fWeekStartMonday(#12/5/2017#) and fWeekEndSunday(#12/5/2017#)
    or
    (last week)
    Between fWeekStartMonday(Date()-7) and fWeekEndSunday(Date()-7)
    or
    (this week)
    Between fWeekStartMonday(Date()) and fWeekEndSunday(Date())
    or
    (next week)
    Between fWeekStartMonday(Date()+7) and fWeekEndSunday(Date()+7)

  6. #6
    Join Date
    Apr 2017
    Posts
    1,679
    WEEKDAY(TheDate,1) returns 1 when date is Monday, and 7 when date is Sunday.

    I.e.
    Code:
    = TheDate - (Weekday(TheDate,1) -1)
    returns Monday of ISO week, the TheDate belongs to.

    More difficult will be the calculating ISO week number. ISO week starts at Monday, and the week belongs to year, it's Thursday belongs into. I.e when 1st January is on Friday, Saturday or Sunday, then these are days of last week of previous year. And p.e. when 1st January is Thursday, then December 29., 30., and 31. belongs to 1st week of new year.

    Edit: when driving home after work, it occurred me, that I erred on formula parameter! There must be everywhere
    WEEKDAY(TheDate,2)
    Last edited by ArviLaanemets; 01-02-2018 at 09:46 AM.

  7. #7
    djclinton15 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    123
    Frist, thanks for replying to my thread. The examples you gave, (this week) & (next week) I'm not sure where to placed them into the VBA code you provided above?/ I'm not well-versed with VBA Coding at all!

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Put the 2 functions (Function fWeekStartMonday and Function fWeekEndSunday) in a standard module.

  9. #9
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    What about Between Date() and Date()+6.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by Thompyt View Post
    What about Between Date() and Date()+6.
    That doesn't account for Sun-Sat.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    djclinton15 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    123
    Steve, I tried your suggestion:
    (Function fWeekStartMonday and Function fWeekEndSunday) in a standard module. I received this error, Compile error: in query expression 'Format([EmplDate<"yyyy". which doesn't make sense. For my "Years" field in my query I have it written as, Yrs: Format([EmplDate],"yyyy"

    All of the other Codes that were suggested from other members were VBA Codes, right?

    Again, I thank everyone here for your suggestions, but I might be over my head & am very frustrated at nothing working! I don't know if I need more in-depth instructions or chalk this one up as being out of my range.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    A no VBA solution if you want to always base on the current date:

    Between Date() - Weekday(Date()) + 1 And Date() + (7 - Weekday(Date()))

    ?date() - Weekday(date()) + 1
    12/31/2017
    ?date() + (7 - Weekday(date()))
    1/6/2018
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    djclinton15 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    123
    [QUOTE=pbaldy;382712]A no VBA solution if you want to always base on the current date:

    Between Date() - Weekday(Date()) + 1 And Date() + (7 - Weekday(Date()))

    pbaldy, I'm assuming this criteria applies to the current week? Would this criteria apply for next week:

    Between Date() - Weekday(Date()) + 8 And Date() + (14- Weekday(Date()))

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you test?

    ?date() - Weekday(date()) + 8
    1/7/2018
    ?date() + (14 - Weekday(date()))
    1/13/2018
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  15. #15
    djclinton15 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Nov 2015
    Posts
    123
    Not too sure what you mean by 'test,' however I do have a Table (actually it's query based on this year '2018') I'm using the formula with dates stating from 1/1/2018 to 3/31/2018. When I ran my 'NxtWk' query with the formula, Between Date() - Weekday(Date()) +
    8
    And Date() + (
    1
    4
    - Weekday(Date()))
    it came back with the dates 1/7/2017 (Mon) to 1/13/2013 (Sat) next week's dates. So, I'm hopefully assuming it's working?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 7
    Last Post: 01-23-2016, 07:27 PM
  2. IsNull clarification
    By Jen0dorf in forum Access
    Replies: 6
    Last Post: 01-11-2016, 10:16 AM
  3. Clarification on distrubution
    By Jen0dorf in forum Access
    Replies: 3
    Last Post: 11-05-2015, 12:57 PM
  4. Just Looking for Clarification...
    By faythe1215 in forum Programming
    Replies: 5
    Last Post: 02-06-2015, 09:03 AM
  5. Replies: 3
    Last Post: 08-21-2012, 03:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums