Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2024
    Posts
    1

    Deducting days from a date

    I am new to Access programming and I am trying to update an access date field QCDay in a form frmProductionControlInput so that on focus it brings in a date which is 5 working days excluding holidays and weekends less than another field DeliveryDay in the same form. I have created a tblHolidays table with HolidayDate as the field containing the holidays. Can anyone help. Thanks Richard

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Need VBA procedure. Calculating a date value excluding holidays and weekends is a common topic. Google it. Here is one discussion https://www.accessforums.net/showthread.php?t=60206
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    Arvin Meyer (former Access MVP) did this like 25 years ago. (even more now!) But he used VBA, because you have to loop over a table of dates and exclude holidays and weekends. It gets messy. Might be on mvps.org/access or datastrat.com

  5. #5
    Join Date
    Jun 2022
    Posts
    28
    I had created a method that used recursion but it was only designed to skip weekends. It steps forward (or back) one day then checks that date to see if it's a Sat or Sun. If not, it decrements the loop counter, if it is then it doesn't change the loop counter and then recalls itself. You could probably add a DLOOKUP to your holidays table to the check for a Sat or Sun. The only drawback to it was the number of days it could look into the future (or past). It depends how much RAM you have, but it was a high enough number that I didn't think I would exceed it in practical use. I can't remember exactly, but I think it was over a year, maybe two or three.

  6. #6
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    Quote Originally Posted by richardmorris0 View Post
    I am new to Access programming and I am trying to update an access date field QCDay in a form frmProductionControlInput so that on focus it brings in a date which is 5 working days excluding holidays and weekends less than another field DeliveryDay in the same form. I have created a tblHolidays table with HolidayDate as the field containing the holidays. Can anyone help. Thanks Richard
    Twenty-odd years ago, Arvin Meyer wrote some code that does that. you can find it here.

  7. #7
    Join Date
    Jun 2022
    Posts
    28
    Quote Originally Posted by seņor_sriracha View Post
    I had created a method that used recursion but it was only designed to skip weekends.
    Et voilā:

    Code:
    ' recursive function to add (or subtract) business days to a date
    ' business days exclude Saturdays and Sundays (holidays not accounted for)
    ' limit 2185 days before out of stack space error
    Public Function BusinessDaysFrom(ByVal dtStart As Date, days As Integer) As Date
        Dim dayNum As Integer
        Dim nextDay As Date
    
    
        On Error GoTo bdError
    
    
        ' base case
        If days = 0 Then
            BusinessDaysFrom = dtStart
            Exit Function
        End If
    
    
        ' add (or subtract) a day to the date argument
        nextDay = dtStart + IIf(days > 0, 1, -1)
    
    
        dayNum = Weekday(nextDay)
    
    
        ' check to see if day plus (or minus) one is a saturday(7) or sunday(1)
        ' only decrement (or increment) the days counter if it's a weekday
        If dayNum <> vbSunday And dayNum <> vbSaturday Then
            days = days - IIf(days > 0, 1, -1)
        End If
    
    
        ' call next frame
        BusinessDaysFrom = BusinessDaysFrom(nextDay, days)
    
    
    exitBD:
        Exit Function
    
    
    bdError:
        MsgBox "Error calculating next business day" & vbCrLf & _
                vbCrLf & Err.Description, vbCritical + vbOKOnly, "Recursion Error"
        Resume exitBD
    
    
    End Function

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 03-07-2016, 04:17 PM
  2. deducting holiday from a query in a form
    By shod90 in forum Forms
    Replies: 1
    Last Post: 01-10-2016, 12:17 PM
  3. Replies: 2
    Last Post: 04-10-2015, 03:51 AM
  4. Replies: 1
    Last Post: 03-27-2014, 06:43 PM
  5. Select Dates between days and two days ago?
    By rstonehouse in forum Queries
    Replies: 4
    Last Post: 08-18-2010, 02:13 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