Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32
  1. #16
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Brasilo,

    How often do you have to do this?



    Your example:
    Consider: Date1 and date2
    10/26/2017 01:31PM - 11/01/2017 10:00AM
    ?datediff("s",#10/26/2017 01:31PM#,#11/01/2017 10:00AM#)
    505740

    1 holiday(10/31) and a weekend (10/28 - 10/29)


    Each day(24h) has 86,400 seconds
    Total Seconds between Date1 and Date2..................505740
    Seconds for Holidays within the interval............... 86400
    Seconds for weekends within the interval.............. 172800

    So required interval in seconds is 505740 - 86400 -172800 = 246540
    246540 seconds = 2.85347222 days
    or 2 days and 51.20833333333333 minutes
    or 2 days, 51 minutes and 12 seconds

    Since you know there is 1 holiday and 2 weekend days within the interval, you can remove 3 days (72 hours) from the Datediff function and use 10/292017 10:00AM as end date for calculation
    eg.
    ?datediff("s",#10/26/2017 01:31PM#,#10/29/2017 10:00AM#)
    246540


    For a function, you calculate Total seconds in the interval - count( Holidays and weekend days in the interval) *86400

  2. #17
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Brasilo,

    Here is a function and test procedure to calculate the number of nonworking days in the interval between StartDate and Enddate. Weekend days are Saturday and Sunday. Holidays are recorded in tblHolidays.

    In this set up calculate the Total number of seconds between date1 and date2. [DateDiff("s",Date1,Date2)]
    Calculate the number of workdays and nonworkdays using function [NonWorkingdays2(Date1,Date2)]
    Calculate the number of seconds in the non workdays in the interval.

    Total seconds you seek = TotalSecondsInInterval -NumberOfSecondsInNonWorkDaysInInterval


    NOTE: NonWorkingDays2
    '-------------------------------------------------------------------------------------------------------------------------------
    ' RETURNS: Decimal number where whole number is Working Days and decimal is NonWorkdays
    '----------------------------------------------------------------------------------------------------------------------------------

    This is my sample tblHolidays
    Code:
    ID Description HolidayDate
    9 BrasiloDay 31-Oct-2017
    7 aprilfools 01-Apr-2016
    6 march day 17-Mar-2016
    5 Tuesday special 01-Feb-2016
    3 holidayOnWeekend 30-Jan-2016
    2 AnotherHoliday 29-Jan-2016
    1 MadeUpHoliday 26-Jan-2016
    4 xxxMonday 18-Jan-2016
    8 NewYears 01-Jan-2016
    Here is the function NonWorkingDays2

    NOTE: The function has been removed since it did not provide a general solution.
    Last edited by orange; 11-03-2017 at 09:47 AM. Reason: additional comment re function scope was incorrect

  3. #18
    BRASILO is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    77
    Quote Originally Posted by orange View Post
    Brasilo,

    Here is a function and test procedure to calculate the number of nonworking days in the interval between StartDate and Enddate. Weekend days are Saturday and Sunday. Holidays are recorded in tblHolidays.

    In this set up calculate the Total number of seconds between date1 and date2. [DateDiff("s",Date1,Date2)]
    Calculate the number of workdays and nonworkdays using function [NonWorkingdays2(Date1,Date2)]
    Calculate the number of seconds in the non workdays in the interval.

    Total seconds you seek = TotalSecondsInInterval -NumberOfSecondsInNonWorkDaysInInterval


    NOTE: NonWorkingDays2
    '-------------------------------------------------------------------------------------------------------------------------------
    ' RETURNS: Decimal number where whole number is Working Days and decimal is NonWorkdays
    '----------------------------------------------------------------------------------------------------------------------------------

    This is my sample tblHolidays
    Code:
    ID Description HolidayDate
    9 BrasiloDay 31-Oct-2017
    7 aprilfools 01-Apr-2016
    6 march day 17-Mar-2016
    5 Tuesday special 01-Feb-2016
    3 holidayOnWeekend 30-Jan-2016
    2 AnotherHoliday 29-Jan-2016
    1 MadeUpHoliday 26-Jan-2016
    4 xxxMonday 18-Jan-2016
    8 NewYears 01-Jan-2016
    Here is the function NonWorkingDays2

    Code:
    Public Function NonWorkingDays2(ByVal StartDate As Date, ByVal enddate As Date) As Single
    
    '....................................................................
    ' Name:     NonWorkingDays2
    ' Purpose: To find the number of Holidays and weekend days in an interval
    ' Inputs:   StartDate As Date
    '           EndDate As Date
    '--------------------------------------------------------------------------------------
    ' RETURNS: Decimal number where  whole number is Working Days and decimal is NonWorkdays
    '--------------------------------------------------------------------------------------
    ' where non work days are Holidays or weekend days.
    '  This uses Saturday and Sunday as weekend days
    ' Author: JED/mellon
    ' Date:     Oct 31 2017
    ' Comment: Accepts two dates and returns the number of nonworkingdays between them
    ' Note that this function accounts for holidays. It requires a table
    ' named tblHolidays with a field named HolidayDate.
    'This uses default start of week vbSunday 1__________________JED
    '
    ' I have used a boolean ShowDebug to identify weekendday or workday or holiday. You can turn it off by setting ShowDebug to false.
    '
    ' I also used a  ByVal so  as not to destroy te original startdate since the code keeps moving
    ' the startdate to the enddate during calculations.
    '
    'I also added a sort to the Holidays recordset to get oldest to newest dates.
    '
    '....................................................................
    
        Dim Showdebug As Boolean   '--------jed
    10  On Error GoTo NonWorkingDays2_Error
    
    20  Showdebug = True    '------------------jed used to show workdayholiday for each date in range
    
       
        Dim WendOrHoliday As Integer
        Dim intCount As Integer
        Dim rst As DAO.Recordset
        Dim db As DAO.Database
        
    30  Set db = CurrentDb
    40  Set rst = db.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays Order By HolidayDate desc;", dbOpenSnapshot)
    
        'sorting a date oldest to newest is descending??
    
    50  intCount = 0
    60  WendOrHoliday = 0
    
    70  Do While StartDate <= enddate
    
    80      rst.FindFirst "[HolidayDate] = #" & StartDate & "#"
    90      If WeekDay(StartDate) <> vbSunday And WeekDay(StartDate) <> vbSaturday Then
    100         If rst.NoMatch Then  'is a workday
    110             intCount = intCount + 1
    120         Else
    130             WendOrHoliday = WendOrHoliday + 1    'its a holiday
    140         End If
    150     Else
    160         WendOrHoliday = WendOrHoliday + 1    'its a weekend day
    170     End If
    180
    
            'jed debuggng tool '********************************************************************************************
    190     If Showdebug Then _
               Debug.Print StartDate & "  " & _
               IIf(WeekDay(StartDate) = vbSaturday Or WeekDay(StartDate) = vbSunday, "weekend day", _
                   IIf(DCount("*", "tblHolidays", "holidaydate = #" & StartDate & "#") = 1, "*HOLIDAY*", "workday"))
    
    200     StartDate = StartDate + 1
    
    210 Loop
    
    
        'NOTE: This returns a decimal number where
        '      the whole number is Working Days in the interval and
        '      the decimal number represents the weekend + holiday days
    
    220 NonWorkingDays2 = intCount + (WendOrHoliday / 10)
    
    
    NonWorkingDays2_Exit:
    230 Exit Function
    
    NonWorkingDays2_Error:
    240 MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure NonWorkingDays2"
    250 Resume NonWorkingDays2_Exit
    
    End Function

    Here is the test routine using your sample data from the post.

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : TestBrasilo
    ' Author    : mellon
    ' Date      : 31-Oct-2017
    ' Purpose   : To demonstrate the use of function NonWorkingDays2,
    'providing a StartDate and an Enddate to get the number of workdays and
    'the number of hlidays+ weekend days in the interval between the 2 dates.
    'And to use this info to get the number of seconds in the interval that
    'does not include weekend days or holidays.
    '
    ' The calculation to convert seconds to days/hrs/min/sec etc is not provided
    '---------------------------------------------------------------------------------------
    '
    Sub TestBrasilo()
          'This is a test procedure to show the logic to get
          'test for Brasilo Oct 31 2017 based on thread
          'https://www.accessforums.net/showthread.php?t=68807&p=376165#post376165
          ' As per OP
          '10/26/2017 01:31PM -  11/01/2017 10:00AM
          '1 holiday(10/31) and a weekend (10/28 - 10/29)
    
              Dim result As Single  'Result is the Raw return from the function NonWorkingDays2(StartDate, enddate)
    
              Dim StartDate As Date
              Dim enddate As Date
    10        Dim StartDateWithTime As Date: StartDateWithTime = #10/26/2017 1:31:00 PM#
    20        Dim EndDateWithTime As Date: EndDateWithTime = #11/1/2017 10:00:00 AM#
    30        Dim SecondsPerDay As Long: SecondsPerDay = 86400
              Dim WeekEndOrHolidays As Long
              Dim TotalSecondsInInterval As Long
              Dim SecondsToRemove As Long
    40        On Error GoTo TestBrasilo_Error
    
    50        TotalSecondsInInterval = DateDiff("s", StartDateWithTime, EndDateWithTime)
    
    60        StartDate = #10/26/2017#    '#1/13/2016#
    70        enddate = #11/1/2017#  ' #1/31/2016#
    
              'Display some parameters
    80        Debug.Print "StartDate: " & StartDate & vbTab & "StartDateWithTime: " & StartDateWithTime
    90        Debug.Print "EndDate  : " & enddate & vbTab & "EndDateWithTime: " & EndDateWithTime
    
    100       result = NonWorkingDays2(StartDate, enddate)
    110       WeekEndOrHolidays = Int((result - Int(result)) * 10)
    
    120       Debug.Print StartDate & "   " & enddate & "  workdays.weekend or holiday days  " & result
    130       Debug.Print "Non workdays in the interval " & WeekEndOrHolidays
    
              'Calculate Seconds to be removed (weekend and holiday).
    140       SecondsToRemove = WeekEndOrHolidays * SecondsPerDay
    150       Debug.Print "Total seconds in interval " & TotalSecondsInInterval
    160       Debug.Print "seconds to remove " & SecondsToRemove
    170       Debug.Print "Number of seconds in working days in the interval is "  _ 
                & TotalSecondsInInterval - SecondsToRemove
    
    TestBrasilo_Exit:
    180       Exit Sub
    
    TestBrasilo_Error:
    190       MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure TestBrasilo"
    200       Resume TestBrasilo_Exit
    End Sub
    And here is the immediate window displaying the Debug.print statements
    Code:
    StartDate: 26-Oct-2017  StartDateWithTime: 26-Oct-2017 1:31:00 PM
    EndDate  : 01-Nov-2017  EndDateWithTime: 01-Nov-2017 10:00:00 AM
    26-Oct-2017  workday
    27-Oct-2017  workday
    28-Oct-2017  weekend day
    29-Oct-2017  weekend day
    30-Oct-2017  workday
    31-Oct-2017  *HOLIDAY*
    01-Nov-2017  workday
    26-Oct-2017   01-Nov-2017  workdays.weekend or holiday days  4.3
    Non workdays in the interval 3
    Total seconds in interval 505740
    seconds to remove 259200
    Number of seconds in working days in the interval is 246540
    Note the 4 workdays, 3 weekend or holidays

    Thank you for this! To answer your question earlier. This will run multiple times a day. So the ability to have each range calculated daily is important. I'll try it and get back to you ASAP. I may have to respond back with results tomorrow. I'll be away from my computer most of the day. Once again, many thanks for your assistance.
    Last edited by orange; 11-03-2017 at 09:43 AM.

  4. #19
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Good luck with your project.
    Happy to help.

  5. #20
    BRASILO is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    77
    Quote Originally Posted by orange View Post
    Good luck with your project.
    Happy to help.
    Good Morning,

    I made a quick attempt at integrating your code into my DB... It didn't work... I'm assume I may have done it wrong. Do you think you could provide a sample DB where I can see how all is arranged correctly. I'm sure the error is on my end.

    Many Thanks!!

  6. #21
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    What exactly are you having problems with?

    The logic involved is:
    Using Datediff, get the total time in seconds in the interval between Date1 and Date2
    Using NonWorkingDays2() get the number of Workdays and nonWorkDays(eg Holidays and weekend days in the interval). nonWorkingDays2 returns a number such as 4.3--where 4 is the number of workdays, and 3 is the number of nonWorkdays) --It's the 3 you are interested in.

    The time in seconds of workdays = TotalSeconds for interval - (number of nonWorkDays * 86400)


    The function code is complete in my previous post under NonWorkingDays2
    You need a table for holidays as per the sample in previous post.

    The test routine shows how I called the function.
    Is there something in the test routine you don't understand?

    NOTE: I have revised the test routine in my post #23 based on arvi's post #22
    Last edited by orange; 11-02-2017 at 08:07 AM. Reason: advising of revised test routine

  7. #22
    Join Date
    Apr 2017
    Posts
    1,776
    Quote Originally Posted by orange View Post

    The logic involved is:
    Using Datediff, get the total time in seconds in the interval between Date1 and Date2
    Using NonWorkingDays2() get the number of Workdays and nonWorkDays(eg Holidays and weekend days in the interval). nonWorkingDays2 returns a number such as 4.3--where 4 is the number of workdays, and 3 is the number of nonWorkdays) --It's the 3 you are interested in.

    The time in seconds of workdays = TotalSeconds for interval - (number of nonWorkDays * 86400)
    Let's assume the op has Date1 and Date2 on same date, and this is a holyday! What will be the result?

  8. #23
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Good point arvi.
    Original code removed. It did not deal with general case.
    Last edited by orange; 11-02-2017 at 09:59 PM.

  9. #24
    Join Date
    Apr 2017
    Posts
    1,776
    I'm afraid this isn't enough!

    When Day1 and Day 2 are same date and weekend/holiday, the result will be 0;
    When Day1 is weekend/holiday, you have to replace Date1 with first next workday at 00:00;
    When Day2 is weekend/holiday, you have to replace Date2 with last previous workday at 24:00;
    After that you calculate worktime based on Date1 and Date2.

    Edit. It's enough to elliminate weekends/holidays shorter than 24 hrs from period. So
    When Date1 is weekend/holiday, you have to replace Date1 with next day at 00:00;
    When Date2 is weekend/holiday, you have to replace Date2 with previous day at 24:00 (or same day at 00:00);
    and use Date# instead of Day# for rest too.

  10. #25
    BRASILO is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    77
    Quote Originally Posted by ArviLaanemets View Post
    I'm afraid this isn't enough!

    When Day1 and Day 2 are same date and weekend/holiday, the result will be 0;
    When Day1 is weekend/holiday, you have to replace Date1 with first next workday at 00:00;
    When Day2 is weekend/holiday, you have to replace Date2 with last previous workday at 24:00;
    After that you calculate worktime based on Date1 and Date2.

    Edit. It's enough to elliminate weekends/holidays shorter than 24 hrs from period. So
    When Date1 is weekend/holiday, you have to replace Date1 with next day at 00:00;
    When Date2 is weekend/holiday, you have to replace Date2 with previous day at 24:00 (or same day at 00:00);
    and use Date# instead of Day# for rest too.
    It seems your correct. I was getting some correct outputs and some incorrect. Thanks for helping me explain.

  11. #26
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Yes arvi is correct. I addressed Brasilo's example, and then arvi's comment re Date1 and Dat2 being same day and Holiday. I've been out all day but will get back with a revised function.
    Sorry for any inconvenience.

  12. #27
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Here is a new function that checks startdate and enddate based on arvi's comments.
    I have run some tests and you can change/add some dates in the test routine to test other conditions.
    The ShowDebug flag in the function is set to True and, when True, a number of messages are printed to the immediate window to help with debugging and review of the data values during processing.


    Code:
    Public Function IntervalDuration(ByVal StartDate As Date, ByVal Enddate As Date) As Single
    
    '....................................................................
    ' Name:    IntervalDuration
    ' Purpose: To calculate the duration (in seconds)  between Date1 and Date2 ignoring all time related to holiday and
    '          weekend days in the interval.
    '          This routine respects Holidays identified in tblHolidays and considers Saturday and Sunday as weekEnd days.
    '          This routine uses 24 hour days.  This function requires a table named tblHolidays with a field named HolidayDate.
    '
    ' Inputs:   StartDate As Date
    '           EndDate As Date
    '
    ' Outputs:  The number of seconds in the interval removing any/all holiday and weekend days in taht interval.
    '
    'Comments: from ArviLaanemets
    ' When Day1 and Day 2 are same date and weekend/holiday, the result will be 0;
    ' When Day1 is weekend/holiday, you have to replace Date1 with first next workday at 00:00;
    ' When Day2 is weekend/holiday, you have to replace Date2 with last previous workday at 24:00;
    '--------------------------------------------------------------------------------------
    ' RETURNS: Number of seconds in the interval ignoring all all Holiday and week end time.
    '--------------------------------------------------------------------------------------
    
    'This is my sample tblHolidays
    '
    'ID  Description   HolidayDate
    '9   BrasiloDay       31 - Oct - 2017
    '7   aprilfools       1 - Apr - 2016
    '6   march day        17-Mar-2016
    '5   Tuesday special  01-Feb-2016
    '3   holidayOnWeekend 30 - Jan - 2016
    '2   AnotherHoliday   29 - Jan - 2016
    '1   MadeUpHoliday    26 - Jan - 2016
    '4   xxxMonday        18 - Jan - 2016
    '8   NewYears         1 - Jan - 2016
    
    '
    ' Author: JED/mellon
    ' Date:     Nov 2 2017
    '
    
    '  This uses default start of week vbSunday 1__________________JED
    '
    ' I have used a boolean ShowDebug to identify weekendday or workday or holiday. You can turn it off by setting ShowDebug to false.
    '
    ' I also used a  ByVal so  as not to destroy te original startdate since the code keeps moving
    ' the startdate to the enddate during calculations.
    '
    ' I also added a sort to the Holidays recordset to get oldest to newest dates.
    '
    '....................................................................
    '
        Dim WendOrHoliday As Integer
        Dim intCount As Integer
        Dim rst As DAO.Recordset
        Dim db As DAO.Database
        '
        'The following commented lines were used for testing as sub
    
        'Dim StartDate As Date: StartDate = #10/31/2017 4:00:00 AM#
        'Dim Enddate As Date: Enddate = #11/1/2017 10:00:00 PM#
    
    10    Dim ZeroTime As Date: ZeroTime = #12:00:00 AM#
    20    Dim SecondsIn24HrDay As Long: SecondsIn24HrDay = 86400
        Dim TotalSecondsInInterval As Long
    
        Dim Showdebug As Boolean   '--------jed this could be part of the function call
    
    30    On Error GoTo IntervalDuration_Error
    
    40    Showdebug = True    '------------------jed used to show workday/holiday for each date in range
    
    50    If Showdebug Then
    60      Debug.Print "Original StartDate: " & WeekdayName(WeekDay(StartDate)) & "  " & StartDate & "  "
    70      Debug.Print "Original EndDate  : " & WeekdayName(WeekDay(Enddate)) & "  " & Enddate
    80    End If
    90    Set db = CurrentDb
    100   Set rst = db.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays Order By HolidayDate desc;", dbOpenSnapshot)
    
        'sorting a date oldest to newest is descending??
    
    110   intCount = 0
    120   WendOrHoliday = 0
    
        'check if Date1 = Date2 and is a Holiday
    130   rst.FindFirst "[HolidayDate] = #" & DateValue(StartDate) & "#"
    
    140   If DateValue(StartDate) = DateValue(Enddate) And Not rst.NoMatch Then   'dates are same and is a holiday
    150     If Showdebug Then
    160         Debug.Print "StartDate and Enddate are same and is a Holiday  IntervalDuration is 0"
    170     End If
    180     IntervalDuration = 0
    190     Exit Function
    200   End If
    
        ' Check if Date1 is weekend/holiday, if so replace Date1 with first next workday at 00:00;
    
    210   rst.FindFirst "[HolidayDate] = #" & DateValue(StartDate) & "#"
    220   If (Not rst.NoMatch) Or (WeekDay(StartDate) = vbSunday Or WeekDay(StartDate) = vbSaturday) Then  'startDate
    230     Debug.Print "StartDate is weekend day or holiday so change startDate from " & StartDate; " to " & DateValue(StartDate) + 1
    240     StartDate = DateValue(StartDate + 1) + ZeroTime
    250     If Showdebug Then Debug.Print DateValue(StartDate) & " " & TimeValue(StartDate)
    260   End If
    
    
    
    
        ' Check if Date2 is weekend/holiday if so replace Date2 with last previous workday at 24:00;
    270   rst.FindFirst "[HolidayDate] = #" & DateValue(Enddate) & "#"
    280   If (Not rst.NoMatch) Or (WeekDay(Enddate) = vbSunday Or WeekDay(StartDate) = vbSaturday) Then
    290     Debug.Print "EndDate is weekend day or holiday so change EndDate from " & Enddate & " to " & DateValue(Enddate)
    300     Enddate = DateValue(Enddate) + ZeroTime
    310     If Showdebug Then Debug.Print Enddate
    320   End If
    
    330   If Showdebug Then
    340     Debug.Print "using StartDate :" & StartDate
    350     Debug.Print "using EndDate   :" & Enddate
    360   End If
    
        'StartDate and EndDate will have been adjusted if tests above is true.
        'Do a DateDiff to get total seconds in adjusted interval
    370   TotalSecondsInInterval = DateDiff("s", StartDate, Enddate)
    380
    390   Do While StartDate <= Enddate
    
    400     rst.FindFirst "[HolidayDate] = #" & DateValue(StartDate) & "#"
    410     If WeekDay(DateValue(StartDate)) <> vbSunday And WeekDay(DateValue(StartDate)) <> vbSaturday Then
    420         If rst.NoMatch Then  'is a workday
    430             intCount = intCount + 1
    440         Else
    450             WendOrHoliday = WendOrHoliday + 1    'its a holiday
    460         End If
    470     Else
    480         WendOrHoliday = WendOrHoliday + 1    'its a weekend day
    490     End If
    
    
            'jed debuggng  '********************************************************************************************
    500     If Showdebug Then _
               Debug.Print DateValue(StartDate) & "  " & _
               IIf(WeekDay(StartDate) = vbSaturday Or WeekDay(StartDate) = vbSunday, "weekend day", _
                   IIf(DCount("*", "tblHolidays", "holidaydate = #" & DateValue(StartDate) & "#") = 1, "*HOLIDAY*", "workday"))
    
    510     StartDate = StartDate + 1
    
    520   Loop
    
        'Do some calculations
    530   If Showdebug Then
    540     Debug.Print "TotalSecondsInInterval: " & TotalSecondsInInterval
    550     Debug.Print "Number of Weekend or Holiday days :" & WendOrHoliday
    560     Debug.Print "Number of Seconds in Weekend days and Holidays : " & WendOrHoliday * SecondsIn24HrDay
    570     Debug.Print "IntervalDuration in seconds :" & TotalSecondsInInterval - (WendOrHoliday * SecondsIn24HrDay)
    580   End If
    
    590   IntervalDuration = TotalSecondsInInterval - (WendOrHoliday * SecondsIn24HrDay)
    
    IntervalDuration_Exit:
    600   Exit Function
    
    IntervalDuration_Error:
    610   MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure IntervalDuration"
    620   Resume IntervalDuration_Exit
    
    End Function
    Here is a new test routine

    Code:
    Sub testIntervalDuration()
    'Test routine to exercise IntervalDuration
    
        Dim SDate(5) As Date, EDate(5) As Date, k As Integer
    10    SDate(0) = #10/20/2017 4:00:00 PM#  'Start on a Friday not holiday
    20    SDate(1) = #10/31/2017 6:00:00 AM#  'StartDate is Holiday
    30    SDate(2) = #10/21/2017 5:00:00 AM#  'StartDate is a weekend day not a holiday
    40    SDate(3) = #10/10/2017 9:00:00 AM#  'StartDate is a week day not a holiday
    
    50    EDate(0) = #10/24/2017 8:00:00 PM#   'Enddate on Tuesday not a holiday
    60    EDate(1) = #10/31/2017 10:00:00 AM#  'EndDate = StartDate and  is Holiday
    70    EDate(2) = #10/31/2017 6:00:00 AM#   'EndDate is Holiday
    80    EDate(3) = #10/12/2017 5:00:00 AM#  'endDate is a week day not a holiday no holidays or week end days in interval
    
    90    For k = 0 To 3
    
    100     Debug.Print "*****In test routine for k= " & k & " IntervalDuration in seconds is " & IntervalDuration(SDate(k), EDate(k))
    110     Debug.Print
    120     Debug.Print "--------------------- " & k & " --------------------------------"
    130     Debug.Print
    140   Next k
    End Sub
    Here is the output from the test routine (messages in the immediate window)

    Code:
    Original StartDate: Friday  20-Oct-2017 4:00:00 PM  
    Original EndDate  : Tuesday  24-Oct-2017 8:00:00 PM
    using StartDate :20-Oct-2017 4:00:00 PM
    using EndDate   :24-Oct-2017 8:00:00 PM
    20-Oct-2017  workday
    21-Oct-2017  weekend day
    22-Oct-2017  weekend day
    23-Oct-2017  workday
    24-Oct-2017  workday
    TotalSecondsInInterval: 360000
    Number of Weekend or Holiday days :2
    Number of Seconds in Weekend days and Holidays : 172800
    IntervalDuration in seconds :187200
    *****In test routine for k= 0 IntervalDuration in seconds is 187200
    
    --------------------- 0 --------------------------------
    
    
    Original StartDate: Tuesday  31-Oct-2017 6:00:00 AM  
    Original EndDate  : Tuesday  31-Oct-2017 10:00:00 AM
    StartDate and Enddate are same and is a Holiday  IntervalDuration is 0
    *****In test routine for k= 1 IntervalDuration in seconds is 0
    
    --------------------- 1 --------------------------------
    
    
    Original StartDate: Saturday  21-Oct-2017 5:00:00 AM  
    Original EndDate  : Tuesday  31-Oct-2017 6:00:00 AM
    StartDate is weekend day or holiday so change startDate from 21-Oct-2017 5:00:00 AM to 22-Oct-2017
    22-Oct-2017 12:00:00 AM
    EndDate is weekend day or holiday so change EndDate from 31-Oct-2017 6:00:00 AM to 31-Oct-2017
    31-Oct-2017 
    using StartDate :22-Oct-2017
    using EndDate   :31-Oct-2017
    22-Oct-2017  weekend day
    23-Oct-2017  workday
    24-Oct-2017  workday
    25-Oct-2017  workday
    26-Oct-2017  workday
    27-Oct-2017  workday
    28-Oct-2017  weekend day
    29-Oct-2017  weekend day
    30-Oct-2017  workday
    31-Oct-2017  *HOLIDAY*
    TotalSecondsInInterval: 777600
    Number of Weekend or Holiday days :4
    Number of Seconds in Weekend days and Holidays : 345600
    IntervalDuration in seconds :432000
    *****In test routine for k= 2 IntervalDuration in seconds is 432000
    
    --------------------- 2 --------------------------------
    
    
    Original StartDate: Tuesday  10-Oct-2017 9:00:00 AM  
    Original EndDate  : Thursday  12-Oct-2017 5:00:00 AM
    using StartDate :10-Oct-2017 9:00:00 AM
    using EndDate   :12-Oct-2017 5:00:00 AM
    10-Oct-2017  workday
    11-Oct-2017  workday
    TotalSecondsInInterval: 158400
    Number of Weekend or Holiday days :0
    Number of Seconds in Weekend days and Holidays : 0
    IntervalDuration in seconds :158400
    *****In test routine for k= 3 IntervalDuration in seconds is 158400
    
    --------------------- 3 --------------------------------

  13. #28
    BRASILO is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    77
    Quote Originally Posted by orange View Post
    Here is a new function that checks startdate and enddate based on arvi's comments.
    I have run some tests and you can change/add some dates in the test routine to test other conditions.
    The ShowDebug flag in the function is set to True and, when True, a number of messages are printed to the immediate window to help with debugging and review of the data values during processing.


    Code:
    Public Function IntervalDuration(ByVal StartDate As Date, ByVal Enddate As Date) As Single
    
    '....................................................................
    ' Name:    IntervalDuration
    ' Purpose: To calculate the duration (in seconds)  between Date1 and Date2 ignoring all time related to holiday and
    '          weekend days in the interval.
    '          This routine respects Holidays identified in tblHolidays and considers Saturday and Sunday as weekEnd days.
    '          This routine uses 24 hour days.  This function requires a table named tblHolidays with a field named HolidayDate.
    '
    ' Inputs:   StartDate As Date
    '           EndDate As Date
    '
    ' Outputs:  The number of seconds in the interval removing any/all holiday and weekend days in taht interval.
    '
    'Comments: from ArviLaanemets
    ' When Day1 and Day 2 are same date and weekend/holiday, the result will be 0;
    ' When Day1 is weekend/holiday, you have to replace Date1 with first next workday at 00:00;
    ' When Day2 is weekend/holiday, you have to replace Date2 with last previous workday at 24:00;
    '--------------------------------------------------------------------------------------
    ' RETURNS: Number of seconds in the interval ignoring all all Holiday and week end time.
    '--------------------------------------------------------------------------------------
    
    'This is my sample tblHolidays
    '
    'ID  Description   HolidayDate
    '9   BrasiloDay       31 - Oct - 2017
    '7   aprilfools       1 - Apr - 2016
    '6   march day        17-Mar-2016
    '5   Tuesday special  01-Feb-2016
    '3   holidayOnWeekend 30 - Jan - 2016
    '2   AnotherHoliday   29 - Jan - 2016
    '1   MadeUpHoliday    26 - Jan - 2016
    '4   xxxMonday        18 - Jan - 2016
    '8   NewYears         1 - Jan - 2016
    
    '
    ' Author: JED/mellon
    ' Date:     Nov 2 2017
    '
    
    '  This uses default start of week vbSunday 1__________________JED
    '
    ' I have used a boolean ShowDebug to identify weekendday or workday or holiday. You can turn it off by setting ShowDebug to false.
    '
    ' I also used a  ByVal so  as not to destroy te original startdate since the code keeps moving
    ' the startdate to the enddate during calculations.
    '
    ' I also added a sort to the Holidays recordset to get oldest to newest dates.
    '
    '....................................................................
    '
        Dim WendOrHoliday As Integer
        Dim intCount As Integer
        Dim rst As DAO.Recordset
        Dim db As DAO.Database
        '
        'The following commented lines were used for testing as sub
    
        'Dim StartDate As Date: StartDate = #10/31/2017 4:00:00 AM#
        'Dim Enddate As Date: Enddate = #11/1/2017 10:00:00 PM#
    
    10    Dim ZeroTime As Date: ZeroTime = #12:00:00 AM#
    20    Dim SecondsIn24HrDay As Long: SecondsIn24HrDay = 86400
        Dim TotalSecondsInInterval As Long
    
        Dim Showdebug As Boolean   '--------jed this could be part of the function call
    
    30    On Error GoTo IntervalDuration_Error
    
    40    Showdebug = True    '------------------jed used to show workday/holiday for each date in range
    
    50    If Showdebug Then
    60      Debug.Print "Original StartDate: " & WeekdayName(WeekDay(StartDate)) & "  " & StartDate & "  "
    70      Debug.Print "Original EndDate  : " & WeekdayName(WeekDay(Enddate)) & "  " & Enddate
    80    End If
    90    Set db = CurrentDb
    100   Set rst = db.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays Order By HolidayDate desc;", dbOpenSnapshot)
    
        'sorting a date oldest to newest is descending??
    
    110   intCount = 0
    120   WendOrHoliday = 0
    
        'check if Date1 = Date2 and is a Holiday
    130   rst.FindFirst "[HolidayDate] = #" & DateValue(StartDate) & "#"
    
    140   If DateValue(StartDate) = DateValue(Enddate) And Not rst.NoMatch Then   'dates are same and is a holiday
    150     If Showdebug Then
    160         Debug.Print "StartDate and Enddate are same and is a Holiday  IntervalDuration is 0"
    170     End If
    180     IntervalDuration = 0
    190     Exit Function
    200   End If
    
        ' Check if Date1 is weekend/holiday, if so replace Date1 with first next workday at 00:00;
    
    210   rst.FindFirst "[HolidayDate] = #" & DateValue(StartDate) & "#"
    220   If (Not rst.NoMatch) Or (WeekDay(StartDate) = vbSunday Or WeekDay(StartDate) = vbSaturday) Then  'startDate
    230     Debug.Print "StartDate is weekend day or holiday so change startDate from " & StartDate; " to " & DateValue(StartDate) + 1
    240     StartDate = DateValue(StartDate + 1) + ZeroTime
    250     If Showdebug Then Debug.Print DateValue(StartDate) & " " & TimeValue(StartDate)
    260   End If
    
    
    
    
        ' Check if Date2 is weekend/holiday if so replace Date2 with last previous workday at 24:00;
    270   rst.FindFirst "[HolidayDate] = #" & DateValue(Enddate) & "#"
    280   If (Not rst.NoMatch) Or (WeekDay(Enddate) = vbSunday Or WeekDay(StartDate) = vbSaturday) Then
    290     Debug.Print "EndDate is weekend day or holiday so change EndDate from " & Enddate & " to " & DateValue(Enddate)
    300     Enddate = DateValue(Enddate) + ZeroTime
    310     If Showdebug Then Debug.Print Enddate
    320   End If
    
    330   If Showdebug Then
    340     Debug.Print "using StartDate :" & StartDate
    350     Debug.Print "using EndDate   :" & Enddate
    360   End If
    
        'StartDate and EndDate will have been adjusted if tests above is true.
        'Do a DateDiff to get total seconds in adjusted interval
    370   TotalSecondsInInterval = DateDiff("s", StartDate, Enddate)
    380
    390   Do While StartDate <= Enddate
    
    400     rst.FindFirst "[HolidayDate] = #" & DateValue(StartDate) & "#"
    410     If WeekDay(DateValue(StartDate)) <> vbSunday And WeekDay(DateValue(StartDate)) <> vbSaturday Then
    420         If rst.NoMatch Then  'is a workday
    430             intCount = intCount + 1
    440         Else
    450             WendOrHoliday = WendOrHoliday + 1    'its a holiday
    460         End If
    470     Else
    480         WendOrHoliday = WendOrHoliday + 1    'its a weekend day
    490     End If
    
    
            'jed debuggng  '********************************************************************************************
    500     If Showdebug Then _
               Debug.Print DateValue(StartDate) & "  " & _
               IIf(WeekDay(StartDate) = vbSaturday Or WeekDay(StartDate) = vbSunday, "weekend day", _
                   IIf(DCount("*", "tblHolidays", "holidaydate = #" & DateValue(StartDate) & "#") = 1, "*HOLIDAY*", "workday"))
    
    510     StartDate = StartDate + 1
    
    520   Loop
    
        'Do some calculations
    530   If Showdebug Then
    540     Debug.Print "TotalSecondsInInterval: " & TotalSecondsInInterval
    550     Debug.Print "Number of Weekend or Holiday days :" & WendOrHoliday
    560     Debug.Print "Number of Seconds in Weekend days and Holidays : " & WendOrHoliday * SecondsIn24HrDay
    570     Debug.Print "IntervalDuration in seconds :" & TotalSecondsInInterval - (WendOrHoliday * SecondsIn24HrDay)
    580   End If
    
    590   IntervalDuration = TotalSecondsInInterval - (WendOrHoliday * SecondsIn24HrDay)
    
    IntervalDuration_Exit:
    600   Exit Function
    
    IntervalDuration_Error:
    610   MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure IntervalDuration"
    620   Resume IntervalDuration_Exit
    
    End Function
    Here is a new test routine

    Code:
    Sub testIntervalDuration()
    'Test routine to exercise IntervalDuration
    
        Dim SDate(5) As Date, EDate(5) As Date, k As Integer
    10    SDate(0) = #10/20/2017 4:00:00 PM#  'Start on a Friday not holiday
    20    SDate(1) = #10/31/2017 6:00:00 AM#  'StartDate is Holiday
    30    SDate(2) = #10/21/2017 5:00:00 AM#  'StartDate is a weekend day not a holiday
    40    SDate(3) = #10/10/2017 9:00:00 AM#  'StartDate is a week day not a holiday
    
    50    EDate(0) = #10/24/2017 8:00:00 PM#   'Enddate on Tuesday not a holiday
    60    EDate(1) = #10/31/2017 10:00:00 AM#  'EndDate = StartDate and  is Holiday
    70    EDate(2) = #10/31/2017 6:00:00 AM#   'EndDate is Holiday
    80    EDate(3) = #10/12/2017 5:00:00 AM#  'endDate is a week day not a holiday no holidays or week end days in interval
    
    90    For k = 0 To 3
    
    100     Debug.Print "*****In test routine for k= " & k & " IntervalDuration in seconds is " & IntervalDuration(SDate(k), EDate(k))
    110     Debug.Print
    120     Debug.Print "--------------------- " & k & " --------------------------------"
    130     Debug.Print
    140   Next k
    End Sub
    Here is the output from the test routine (messages in the immediate window)

    Code:
    Original StartDate: Friday  20-Oct-2017 4:00:00 PM  
    Original EndDate  : Tuesday  24-Oct-2017 8:00:00 PM
    using StartDate :20-Oct-2017 4:00:00 PM
    using EndDate   :24-Oct-2017 8:00:00 PM
    20-Oct-2017  workday
    21-Oct-2017  weekend day
    22-Oct-2017  weekend day
    23-Oct-2017  workday
    24-Oct-2017  workday
    TotalSecondsInInterval: 360000
    Number of Weekend or Holiday days :2
    Number of Seconds in Weekend days and Holidays : 172800
    IntervalDuration in seconds :187200
    *****In test routine for k= 0 IntervalDuration in seconds is 187200
    
    --------------------- 0 --------------------------------
    
    
    Original StartDate: Tuesday  31-Oct-2017 6:00:00 AM  
    Original EndDate  : Tuesday  31-Oct-2017 10:00:00 AM
    StartDate and Enddate are same and is a Holiday  IntervalDuration is 0
    *****In test routine for k= 1 IntervalDuration in seconds is 0
    
    --------------------- 1 --------------------------------
    
    
    Original StartDate: Saturday  21-Oct-2017 5:00:00 AM  
    Original EndDate  : Tuesday  31-Oct-2017 6:00:00 AM
    StartDate is weekend day or holiday so change startDate from 21-Oct-2017 5:00:00 AM to 22-Oct-2017
    22-Oct-2017 12:00:00 AM
    EndDate is weekend day or holiday so change EndDate from 31-Oct-2017 6:00:00 AM to 31-Oct-2017
    31-Oct-2017 
    using StartDate :22-Oct-2017
    using EndDate   :31-Oct-2017
    22-Oct-2017  weekend day
    23-Oct-2017  workday
    24-Oct-2017  workday
    25-Oct-2017  workday
    26-Oct-2017  workday
    27-Oct-2017  workday
    28-Oct-2017  weekend day
    29-Oct-2017  weekend day
    30-Oct-2017  workday
    31-Oct-2017  *HOLIDAY*
    TotalSecondsInInterval: 777600
    Number of Weekend or Holiday days :4
    Number of Seconds in Weekend days and Holidays : 345600
    IntervalDuration in seconds :432000
    *****In test routine for k= 2 IntervalDuration in seconds is 432000
    
    --------------------- 2 --------------------------------
    
    
    Original StartDate: Tuesday  10-Oct-2017 9:00:00 AM  
    Original EndDate  : Thursday  12-Oct-2017 5:00:00 AM
    using StartDate :10-Oct-2017 9:00:00 AM
    using EndDate   :12-Oct-2017 5:00:00 AM
    10-Oct-2017  workday
    11-Oct-2017  workday
    TotalSecondsInInterval: 158400
    Number of Weekend or Holiday days :0
    Number of Seconds in Weekend days and Holidays : 0
    IntervalDuration in seconds :158400
    *****In test routine for k= 3 IntervalDuration in seconds is 158400
    
    --------------------- 3 --------------------------------

    Thank you!! I'll try this once I get back to my computer this afternoon. Have a great weekend!

  14. #29
    BRASILO is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Aug 2017
    Posts
    77
    Quote Originally Posted by orange View Post
    Here is a new function that checks startdate and enddate based on arvi's comments.
    I have run some tests and you can change/add some dates in the test routine to test other conditions.
    The ShowDebug flag in the function is set to True and, when True, a number of messages are printed to the immediate window to help with debugging and review of the data values during processing.


    Code:
    Public Function IntervalDuration(ByVal StartDate As Date, ByVal Enddate As Date) As Single
    
    '....................................................................
    ' Name:    IntervalDuration
    ' Purpose: To calculate the duration (in seconds)  between Date1 and Date2 ignoring all time related to holiday and
    '          weekend days in the interval.
    '          This routine respects Holidays identified in tblHolidays and considers Saturday and Sunday as weekEnd days.
    '          This routine uses 24 hour days.  This function requires a table named tblHolidays with a field named HolidayDate.
    '
    ' Inputs:   StartDate As Date
    '           EndDate As Date
    '
    ' Outputs:  The number of seconds in the interval removing any/all holiday and weekend days in taht interval.
    '
    'Comments: from ArviLaanemets
    ' When Day1 and Day 2 are same date and weekend/holiday, the result will be 0;
    ' When Day1 is weekend/holiday, you have to replace Date1 with first next workday at 00:00;
    ' When Day2 is weekend/holiday, you have to replace Date2 with last previous workday at 24:00;
    '--------------------------------------------------------------------------------------
    ' RETURNS: Number of seconds in the interval ignoring all all Holiday and week end time.
    '--------------------------------------------------------------------------------------
    
    'This is my sample tblHolidays
    '
    'ID  Description   HolidayDate
    '9   BrasiloDay       31 - Oct - 2017
    '7   aprilfools       1 - Apr - 2016
    '6   march day        17-Mar-2016
    '5   Tuesday special  01-Feb-2016
    '3   holidayOnWeekend 30 - Jan - 2016
    '2   AnotherHoliday   29 - Jan - 2016
    '1   MadeUpHoliday    26 - Jan - 2016
    '4   xxxMonday        18 - Jan - 2016
    '8   NewYears         1 - Jan - 2016
    
    '
    ' Author: JED/mellon
    ' Date:     Nov 2 2017
    '
    
    '  This uses default start of week vbSunday 1__________________JED
    '
    ' I have used a boolean ShowDebug to identify weekendday or workday or holiday. You can turn it off by setting ShowDebug to false.
    '
    ' I also used a  ByVal so  as not to destroy te original startdate since the code keeps moving
    ' the startdate to the enddate during calculations.
    '
    ' I also added a sort to the Holidays recordset to get oldest to newest dates.
    '
    '....................................................................
    '
        Dim WendOrHoliday As Integer
        Dim intCount As Integer
        Dim rst As DAO.Recordset
        Dim db As DAO.Database
        '
        'The following commented lines were used for testing as sub
    
        'Dim StartDate As Date: StartDate = #10/31/2017 4:00:00 AM#
        'Dim Enddate As Date: Enddate = #11/1/2017 10:00:00 PM#
    
    10    Dim ZeroTime As Date: ZeroTime = #12:00:00 AM#
    20    Dim SecondsIn24HrDay As Long: SecondsIn24HrDay = 86400
        Dim TotalSecondsInInterval As Long
    
        Dim Showdebug As Boolean   '--------jed this could be part of the function call
    
    30    On Error GoTo IntervalDuration_Error
    
    40    Showdebug = True    '------------------jed used to show workday/holiday for each date in range
    
    50    If Showdebug Then
    60      Debug.Print "Original StartDate: " & WeekdayName(WeekDay(StartDate)) & "  " & StartDate & "  "
    70      Debug.Print "Original EndDate  : " & WeekdayName(WeekDay(Enddate)) & "  " & Enddate
    80    End If
    90    Set db = CurrentDb
    100   Set rst = db.OpenRecordset("SELECT [HolidayDate] FROM tblHolidays Order By HolidayDate desc;", dbOpenSnapshot)
    
        'sorting a date oldest to newest is descending??
    
    110   intCount = 0
    120   WendOrHoliday = 0
    
        'check if Date1 = Date2 and is a Holiday
    130   rst.FindFirst "[HolidayDate] = #" & DateValue(StartDate) & "#"
    
    140   If DateValue(StartDate) = DateValue(Enddate) And Not rst.NoMatch Then   'dates are same and is a holiday
    150     If Showdebug Then
    160         Debug.Print "StartDate and Enddate are same and is a Holiday  IntervalDuration is 0"
    170     End If
    180     IntervalDuration = 0
    190     Exit Function
    200   End If
    
        ' Check if Date1 is weekend/holiday, if so replace Date1 with first next workday at 00:00;
    
    210   rst.FindFirst "[HolidayDate] = #" & DateValue(StartDate) & "#"
    220   If (Not rst.NoMatch) Or (WeekDay(StartDate) = vbSunday Or WeekDay(StartDate) = vbSaturday) Then  'startDate
    230     Debug.Print "StartDate is weekend day or holiday so change startDate from " & StartDate; " to " & DateValue(StartDate) + 1
    240     StartDate = DateValue(StartDate + 1) + ZeroTime
    250     If Showdebug Then Debug.Print DateValue(StartDate) & " " & TimeValue(StartDate)
    260   End If
    
    
    
    
        ' Check if Date2 is weekend/holiday if so replace Date2 with last previous workday at 24:00;
    270   rst.FindFirst "[HolidayDate] = #" & DateValue(Enddate) & "#"
    280   If (Not rst.NoMatch) Or (WeekDay(Enddate) = vbSunday Or WeekDay(StartDate) = vbSaturday) Then
    290     Debug.Print "EndDate is weekend day or holiday so change EndDate from " & Enddate & " to " & DateValue(Enddate)
    300     Enddate = DateValue(Enddate) + ZeroTime
    310     If Showdebug Then Debug.Print Enddate
    320   End If
    
    330   If Showdebug Then
    340     Debug.Print "using StartDate :" & StartDate
    350     Debug.Print "using EndDate   :" & Enddate
    360   End If
    
        'StartDate and EndDate will have been adjusted if tests above is true.
        'Do a DateDiff to get total seconds in adjusted interval
    370   TotalSecondsInInterval = DateDiff("s", StartDate, Enddate)
    380
    390   Do While StartDate <= Enddate
    
    400     rst.FindFirst "[HolidayDate] = #" & DateValue(StartDate) & "#"
    410     If WeekDay(DateValue(StartDate)) <> vbSunday And WeekDay(DateValue(StartDate)) <> vbSaturday Then
    420         If rst.NoMatch Then  'is a workday
    430             intCount = intCount + 1
    440         Else
    450             WendOrHoliday = WendOrHoliday + 1    'its a holiday
    460         End If
    470     Else
    480         WendOrHoliday = WendOrHoliday + 1    'its a weekend day
    490     End If
    
    
            'jed debuggng  '********************************************************************************************
    500     If Showdebug Then _
               Debug.Print DateValue(StartDate) & "  " & _
               IIf(WeekDay(StartDate) = vbSaturday Or WeekDay(StartDate) = vbSunday, "weekend day", _
                   IIf(DCount("*", "tblHolidays", "holidaydate = #" & DateValue(StartDate) & "#") = 1, "*HOLIDAY*", "workday"))
    
    510     StartDate = StartDate + 1
    
    520   Loop
    
        'Do some calculations
    530   If Showdebug Then
    540     Debug.Print "TotalSecondsInInterval: " & TotalSecondsInInterval
    550     Debug.Print "Number of Weekend or Holiday days :" & WendOrHoliday
    560     Debug.Print "Number of Seconds in Weekend days and Holidays : " & WendOrHoliday * SecondsIn24HrDay
    570     Debug.Print "IntervalDuration in seconds :" & TotalSecondsInInterval - (WendOrHoliday * SecondsIn24HrDay)
    580   End If
    
    590   IntervalDuration = TotalSecondsInInterval - (WendOrHoliday * SecondsIn24HrDay)
    
    IntervalDuration_Exit:
    600   Exit Function
    
    IntervalDuration_Error:
    610   MsgBox "Error " & err.number & " in line " & Erl & " (" & err.Description & ") in procedure IntervalDuration"
    620   Resume IntervalDuration_Exit
    
    End Function
    Here is a new test routine

    Code:
    Sub testIntervalDuration()
    'Test routine to exercise IntervalDuration
    
        Dim SDate(5) As Date, EDate(5) As Date, k As Integer
    10    SDate(0) = #10/20/2017 4:00:00 PM#  'Start on a Friday not holiday
    20    SDate(1) = #10/31/2017 6:00:00 AM#  'StartDate is Holiday
    30    SDate(2) = #10/21/2017 5:00:00 AM#  'StartDate is a weekend day not a holiday
    40    SDate(3) = #10/10/2017 9:00:00 AM#  'StartDate is a week day not a holiday
    
    50    EDate(0) = #10/24/2017 8:00:00 PM#   'Enddate on Tuesday not a holiday
    60    EDate(1) = #10/31/2017 10:00:00 AM#  'EndDate = StartDate and  is Holiday
    70    EDate(2) = #10/31/2017 6:00:00 AM#   'EndDate is Holiday
    80    EDate(3) = #10/12/2017 5:00:00 AM#  'endDate is a week day not a holiday no holidays or week end days in interval
    
    90    For k = 0 To 3
    
    100     Debug.Print "*****In test routine for k= " & k & " IntervalDuration in seconds is " & IntervalDuration(SDate(k), EDate(k))
    110     Debug.Print
    120     Debug.Print "--------------------- " & k & " --------------------------------"
    130     Debug.Print
    140   Next k
    End Sub
    Here is the output from the test routine (messages in the immediate window)

    Code:
    Original StartDate: Friday  20-Oct-2017 4:00:00 PM  
    Original EndDate  : Tuesday  24-Oct-2017 8:00:00 PM
    using StartDate :20-Oct-2017 4:00:00 PM
    using EndDate   :24-Oct-2017 8:00:00 PM
    20-Oct-2017  workday
    21-Oct-2017  weekend day
    22-Oct-2017  weekend day
    23-Oct-2017  workday
    24-Oct-2017  workday
    TotalSecondsInInterval: 360000
    Number of Weekend or Holiday days :2
    Number of Seconds in Weekend days and Holidays : 172800
    IntervalDuration in seconds :187200
    *****In test routine for k= 0 IntervalDuration in seconds is 187200
    
    --------------------- 0 --------------------------------
    
    
    Original StartDate: Tuesday  31-Oct-2017 6:00:00 AM  
    Original EndDate  : Tuesday  31-Oct-2017 10:00:00 AM
    StartDate and Enddate are same and is a Holiday  IntervalDuration is 0
    *****In test routine for k= 1 IntervalDuration in seconds is 0
    
    --------------------- 1 --------------------------------
    
    
    Original StartDate: Saturday  21-Oct-2017 5:00:00 AM  
    Original EndDate  : Tuesday  31-Oct-2017 6:00:00 AM
    StartDate is weekend day or holiday so change startDate from 21-Oct-2017 5:00:00 AM to 22-Oct-2017
    22-Oct-2017 12:00:00 AM
    EndDate is weekend day or holiday so change EndDate from 31-Oct-2017 6:00:00 AM to 31-Oct-2017
    31-Oct-2017 
    using StartDate :22-Oct-2017
    using EndDate   :31-Oct-2017
    22-Oct-2017  weekend day
    23-Oct-2017  workday
    24-Oct-2017  workday
    25-Oct-2017  workday
    26-Oct-2017  workday
    27-Oct-2017  workday
    28-Oct-2017  weekend day
    29-Oct-2017  weekend day
    30-Oct-2017  workday
    31-Oct-2017  *HOLIDAY*
    TotalSecondsInInterval: 777600
    Number of Weekend or Holiday days :4
    Number of Seconds in Weekend days and Holidays : 345600
    IntervalDuration in seconds :432000
    *****In test routine for k= 2 IntervalDuration in seconds is 432000
    
    --------------------- 2 --------------------------------
    
    
    Original StartDate: Tuesday  10-Oct-2017 9:00:00 AM  
    Original EndDate  : Thursday  12-Oct-2017 5:00:00 AM
    using StartDate :10-Oct-2017 9:00:00 AM
    using EndDate   :12-Oct-2017 5:00:00 AM
    10-Oct-2017  workday
    11-Oct-2017  workday
    TotalSecondsInInterval: 158400
    Number of Weekend or Holiday days :0
    Number of Seconds in Weekend days and Holidays : 0
    IntervalDuration in seconds :158400
    *****In test routine for k= 3 IntervalDuration in seconds is 158400
    
    --------------------- 3 --------------------------------
    I tried the code this morning, it works good on the immediate window. However, when put into an Exp I have to an issue with type mismatch. Not sure why that is coming up????

  15. #30
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850

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

Similar Threads

  1. Remove Holiday's and Weekend Involving Time Stamps
    By Dave_01 in forum Programming
    Replies: 8
    Last Post: 01-26-2016, 02:13 PM
  2. Replies: 3
    Last Post: 01-26-2016, 01:56 PM
  3. Date calculation excluding the weekends
    By Aosmond in forum Forms
    Replies: 1
    Last Post: 08-04-2014, 08:27 PM
  4. how do I not record date of the weekend or holiday?
    By fabiobarreto10 in forum Forms
    Replies: 4
    Last Post: 06-12-2012, 09:33 AM
  5. Code to combine report filter and date range
    By rhubarb in forum Reports
    Replies: 17
    Last Post: 10-28-2011, 03:08 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