tangledball
Registered User.
- Local time
- Today, 21:46
- Joined
- Mar 22, 2009
- Messages
- 14
Is there a way for me to omit public holidays using the CalcWorkDays Funtion?
I have the following code that works great for removing the weekends from my calculation, but is there a way for me to remove the public holidays from the equation?
I am currently using the following code:
Function CalcWorkDays(StartDate, EndDate) As Integer
Dim LTotalDays As Integer
Dim LSaturdays As Integer
Dim LSundays As Integer
On Error GoTo Err_Execute
CalcWorkDays = 0
If IsDate(StartDate) And IsDate(EndDate) Then
If EndDate <= StartDate Then
CalcWorkDays = 1
Else
LTotalDays = DateDiff("d", StartDate - 1, EndDate)
LSaturdays = DateDiff("ww", StartDate - 1, EndDate, 7)
LSundays = DateDiff("ww", StartDate - 1, EndDate, 1)
'Workdays is the elapsed days excluding Saturdays and Sundays
CalcWorkDays = LTotalDays - LSaturdays - LSundays
End If
End If
Exit Function
Err_Execute:
'If error occurs, return 0
CalcWorkDays = 0
End Function
=========================================================
But have also found this code:
Function CalcWorkDays(dtmStart As Date, DtmEnd As Date) As Integer
On Error GoTo CalcWorkDays_Error
'Calculates the number of days between the dates
'Add one so all days are included
CalcWorkDays = DateDiff("d", dtmStart, DtmEnd) - _
(DateDiff("ww", dtmStart, DtmEnd, vbSaturday) + _
DateDiff("ww", dtmStart, DtmEnd, vbSunday)) + 1
'Subtract the Holidays
CalcWorkDays = CalcWorkDays - DCount("*", "tblHolidays", _
"[HOLI_DATE] between #" & dtmStart & "# And #" & DtmEnd & "#")
CalcWorkDays_Exit:
On Error Resume Next
Exit Function
CalcWorkDays_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure CalcWorkDays of Module modDateFunctions"
GoTo CalcWorkDays_Exit
End Function
but i can't seem to get this to work for some reason, i have removed the dtm's quoted in the code above and changed the descriptions to that of the table names from which the function will use to calc the number of days. I have also created a Holiday table and entered some public holiday dates and created a leave table with some sample dates that will cross both weekends and public holidays, however when i call this up through my query the records seem to triple (ie i have 3 sample dates in the leave table) but these now show as 9 records when run through the query, also the calculations are all out and the dates where public holidays cross are showing as minus figures.
In the query table i have just entered the StartDate and EndDate in the query fields seen as i would be calling up the public holiday dates from the Holiday table, but it's not working. I have tried various methods, linking the holiday table with the Leave table within the query and including the Holiday [PubDate] within the query field, but still nothing.
I have called up the function using an expresssion which is as follows:
Expr1: Calcworkdays(Date(),[Holidays]![PubDate])
If someone has a working sample of this in operation, i should be able to find out what i'm doing wrong, alternatively any help would be appreciated.
Appologies to the mods for my previous duplicate posting.

I have the following code that works great for removing the weekends from my calculation, but is there a way for me to remove the public holidays from the equation?
I am currently using the following code:
Function CalcWorkDays(StartDate, EndDate) As Integer
Dim LTotalDays As Integer
Dim LSaturdays As Integer
Dim LSundays As Integer
On Error GoTo Err_Execute
CalcWorkDays = 0
If IsDate(StartDate) And IsDate(EndDate) Then
If EndDate <= StartDate Then
CalcWorkDays = 1
Else
LTotalDays = DateDiff("d", StartDate - 1, EndDate)
LSaturdays = DateDiff("ww", StartDate - 1, EndDate, 7)
LSundays = DateDiff("ww", StartDate - 1, EndDate, 1)
'Workdays is the elapsed days excluding Saturdays and Sundays
CalcWorkDays = LTotalDays - LSaturdays - LSundays
End If
End If
Exit Function
Err_Execute:
'If error occurs, return 0
CalcWorkDays = 0
End Function
=========================================================
But have also found this code:
Function CalcWorkDays(dtmStart As Date, DtmEnd As Date) As Integer
On Error GoTo CalcWorkDays_Error
'Calculates the number of days between the dates
'Add one so all days are included
CalcWorkDays = DateDiff("d", dtmStart, DtmEnd) - _
(DateDiff("ww", dtmStart, DtmEnd, vbSaturday) + _
DateDiff("ww", dtmStart, DtmEnd, vbSunday)) + 1
'Subtract the Holidays
CalcWorkDays = CalcWorkDays - DCount("*", "tblHolidays", _
"[HOLI_DATE] between #" & dtmStart & "# And #" & DtmEnd & "#")
CalcWorkDays_Exit:
On Error Resume Next
Exit Function
CalcWorkDays_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure CalcWorkDays of Module modDateFunctions"
GoTo CalcWorkDays_Exit
End Function
but i can't seem to get this to work for some reason, i have removed the dtm's quoted in the code above and changed the descriptions to that of the table names from which the function will use to calc the number of days. I have also created a Holiday table and entered some public holiday dates and created a leave table with some sample dates that will cross both weekends and public holidays, however when i call this up through my query the records seem to triple (ie i have 3 sample dates in the leave table) but these now show as 9 records when run through the query, also the calculations are all out and the dates where public holidays cross are showing as minus figures.
In the query table i have just entered the StartDate and EndDate in the query fields seen as i would be calling up the public holiday dates from the Holiday table, but it's not working. I have tried various methods, linking the holiday table with the Leave table within the query and including the Holiday [PubDate] within the query field, but still nothing.
I have called up the function using an expresssion which is as follows:
Expr1: Calcworkdays(Date(),[Holidays]![PubDate])
If someone has a working sample of this in operation, i should be able to find out what i'm doing wrong, alternatively any help would be appreciated.
Appologies to the mods for my previous duplicate posting.

Last edited: