Working Days v Calendar Days

sTrongFuse

Registered User.
Local time
Today, 10:16
Joined
Dec 3, 2012
Messages
26
Not sure if what I'm trying to do is possible, but it's worth a try.

I currently have the following code, which works fine:

Private Sub Closed_AfterUpdate()
Me.Status = "Closed"
If Me.[Closed] <= (Me.[Received] + 1) Then
Me.[24Hour] = "Yes"
Else
If Me.[Closed] > (Me.[Received] + 1) Then
Me.[24Hour] = "No"
End If
End If
End Sub

This compares the date a customer complaint was closed with the date it was received, and if a complaint is closed by the following day the [24Hour] flag is set to Yes otherwise, it is set to No.

Is there a way of changing the Me.[Received] + 1 bit, which counts calender days, so that it compares working days instead?

eg Complaint received Friday, closed Monday so [24Hour] = Yes instead of No

At the moment, [24Hour] can be manually overridden to account for this, but I'd like to automate it if possible.

Cheers,

T
 
Is there a way of changing the Me.[Received] + 1 bit, which counts calender days, so that it compares working days instead?
Yes.

see http://www.techonthenet.com/access/functions/date/weekday.php for the Weekday function and examples. You should be able to determine which Weekday [Received] and [Closed] refer to, and take appropriate action. You may, depending on your details, need to have a tblHolidays that identifies statutory Holidays which could override your Weekday calculation in special circumstances.

Good luck with your project.
 
Last edited:
I actually have a working days function in one of my systems at work I can copy and paste for you tomorrow, if someone doesn't give you a better answer before then.

It'll actually be a pair of functions, but it works.
 
Here you go.

Code:
Option Compare Database
Option Explicit

Public Function funNetWorkDays(ByRef dteStartDate As Date, _
                               ByRef dteEndDate As Date, _
                               blnCountFirstDay As Boolean, _
                               Optional ByRef strHolidays As String = "tblHolidays") As Integer
' ************************************************************
' Created by       : Microsoft
' Parameters       : Start Date, End Date
' Returns          : Integer
' Remarks          : This routine returns the number of working days between two selected dates.
'                    Returns -1 in case of error.
'                    This routine requires a 'Holidays' table be maintained with the following fields:
'                       *ID - Autonumber
'                       *Description - String
'                       *Holiday - Date(Short)
' Changes          :
' ************************************************************

On Error GoTo funNetWorkDays_Err

    Dim strProcName As String
    Dim nWeekdays As Integer
    Dim nHolidays As Integer
    Dim strWhere As String
    
    strProcName = "funNetWorkDays"

    ' DateValue returns the date part only.
    dteStartDate = DateValue(dteStartDate)
    dteEndDate = DateValue(dteEndDate)
    
    nWeekdays = funWeekdays(dteStartDate, dteEndDate)
    If nWeekdays = -1 Then
        funNetWorkDays = -1
        GoTo funNetWorkDays_Exit
    End If
    
    'If blnCountFirstDay is FALSE, then decrement nWeekdays by 1 - we want total days elapsed, not number of days involved.
    If Not blnCountFirstDay Then nWeekdays = nWeekdays - 1
    
    strWhere = "[Holiday] >= #" & dteStartDate _
        & "# AND [Holiday] <= #" & dteEndDate & "#"
    
    ' Count the number of holidays.
    nHolidays = DCount(Expr:="[Holiday]", _
        Domain:=strHolidays, _
        Criteria:=strWhere)
    
    funNetWorkDays = nWeekdays - nHolidays

funNetWorkDays_Exit:
    Exit Function

funNetWorkDays_Err:
        
    MsgBox "Error occurred" & vbCrLf & vbCrLf & _
    "In Function:" & vbTab & strProcName & vbCrLf & _
    "Err Number: " & vbTab & Err.Number & vbCrLf & _
    "Description: " & vbTab & Err.Description, vbCritical, _
    "Error in " & Chr$(34) & strProcName & Chr$(34)
    Resume funNetWorkDays_Exit

End Function

Public Function funWeekdays(ByRef dteStartDate As Date, _
                            ByRef dteEndDate As Date) As Integer
' ************************************************************
' Created by       : Microsoft
' Parameters       : Start Date, End Date
' Returns          : Integer
' Remarks          : This routine returns the number of working days between two selected dates.
'                    Returns -1 in case of error.
' Changes          : Added switch to see if first day should be counted.
' ************************************************************
On Error GoTo funWeekdays_Err

    Dim strProcName As String
    
    ' The number of weekend days per week.
    Const ncNumberOfWeekendDays As Integer = 2
    
    ' The number of days inclusive.
    Dim varDays As Variant
    
    ' The number of weekend days.
    Dim varWeekendDays As Variant
    
    ' Temporary storage for datetime.
    Dim dtmX As Date
    
    strProcName = "funWeekdays"
    
    ' If the end date is earlier, swap the dates.
    If dteEndDate < dteStartDate Then
        dtmX = dteStartDate
        dteStartDate = dteEndDate
        dteEndDate = dtmX
    End If
    
    ' Calculate the number of days inclusive (+ 1 is to add back startDate).
    varDays = DateDiff(Interval:="d", _
        date1:=dteStartDate, _
        date2:=dteEndDate) + 1
    
    ' Calculate the number of weekend days.
    varWeekendDays = (DateDiff(Interval:="ww", _
        date1:=dteStartDate, _
        date2:=dteEndDate) _
        * ncNumberOfWeekendDays) _
        + IIf(DatePart(Interval:="w", _
        Date:=dteStartDate) = vbSunday, 1, 0) _
        + IIf(DatePart(Interval:="w", _
        Date:=dteEndDate) = vbSaturday, 1, 0)
    
    ' Calculate the number of weekdays.
    funWeekdays = (varDays - varWeekendDays)
    
funWeekdays_Exit:
    Exit Function

funWeekdays_Err:
        
    MsgBox "Error occurred" & vbCrLf & vbCrLf & _
    "In Function:" & vbTab & strProcName & vbCrLf & _
    "Err Number: " & vbTab & Err.Number & vbCrLf & _
    "Description: " & vbTab & Err.Description, vbCritical, _
    "Error in " & Chr$(34) & strProcName & Chr$(34)
    Resume funWeekdays_Exit

End Function

Yeah, I just ripped them off the Microsoft website and slightly modified them. Here's the page they came from: http://msdn.microsoft.com/en-us/library/office/dd327646(v=office.12).aspx
 

Users who are viewing this thread

Back
Top Bottom