Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Middlemarch is offline Competent Performer
    Windows 10 Access 2019
    Join Date
    Mar 2015
    Posts
    479
    That does Sat only, but do many places have Day1 as other than Sunday.
    Can Access be aware of that ?
    Added: Put Vba where Day1 as other than Sunday. in chatGPT and got a answer!

  2. #17
    Join Date
    Apr 2017
    Posts
    1,792
    Quote Originally Posted by Middlemarch View Post
    That does Sat only, but do many places have Day1 as other than Sunday.
    Can Access be aware of that ?
    I assume this was about my post?

    E.g. with European week definition, and wanting the nearest last day of week (Sunday) returned
    Code:
    datSunday = dd -(Weekday(dd;2)<4)*Weekday(dd;2) + (Weekday(dd;2)>3)*(7-Weekday(dd;2))
    
    or wanting the nearest Saturday returned
    Code:
    datSaturday = dd -(Weekday(dd;2)<4)*Weekday(dd;2) + (Weekday(dd;2)>3)*(7-Weekday(dd;2)) -1
    

  3. #18
    Gustav's Avatar
    Gustav is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Jan 2025
    Posts
    32
    You can add a final twist to the ChatGPT solution:

    Code:
    Public Function DateNearestWeekday( _
        ByVal BaseDate As Date, _
        ByVal DayOfWeek As VbDayOfWeek, _
        Optional ByVal Bias As Integer) _
        As Date
        
        Dim BaseWeekday As VbDayOfWeek
        Dim OffsetMajor As Integer
        Dim OffsetMinor As Integer
        Dim Offset      As Integer
        Dim WeekdayDate As Date
    
    
        ' The weekday of the base date.
        BaseWeekday = Weekday(BaseDate)
    
    
        ' Calculate the day count from the base date to the previous and the following weekday.
        OffsetMinor = (BaseWeekday - DayOfWeek + 7) Mod 7
        OffsetMajor = (DayOfWeek - BaseWeekday + 7) Mod 7
        
        If OffsetMajor = OffsetMinor Then
            Offset = Sgn(Bias) * 7
        ElseIf OffsetMajor < OffsetMinor Then
            Offset = OffsetMajor
        ElseIf OffsetMajor > OffsetMinor Then
            Offset = -OffsetMinor
        End If
        WeekdayDate = DateAdd("d", Offset, BaseDate)
        
        DateNearestWeekday = WeekdayDate
        
    End Function
    This will allow you to specify what to do, if the searched weekday is that of the date to search from:

    Code:
    ' If the weekday is that of the base date, argument Bias may be specified:
    '
    '   Bias = 0: The base date (default)
    '   Bias > 0: The next date of weekday
    '   Bias < 0: The previous date of weekday

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

Similar Threads

  1. Replies: 6
    Last Post: 04-04-2017, 02:50 AM
  2. Replies: 9
    Last Post: 08-19-2014, 12:41 PM
  3. Replace function adding in unwanted carriage return
    By timmygrover in forum Queries
    Replies: 10
    Last Post: 07-02-2012, 11:58 AM
  4. Function to return a set of records?
    By vicrauch in forum Access
    Replies: 2
    Last Post: 07-12-2011, 08:27 AM
  5. Replies: 1
    Last Post: 06-23-2010, 09:45 AM

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