Apologies if this has been asked before, I've searched for ages and couldn't find anything so here goes!
I'm using the function below in a number of queries to calculate date differences in working days.
Function Work_Days(BegDate As Variant, EndDate As Variant) As Integer
' Note that this function does not account for holidays.
Dim WholeWeeks As Variant
Dim DateCnt As Integer
Dim EndDays As Integer
BegDate = DateValue(BegDate)
EndDate = DateValue(EndDate)
WholeWeeks = DateDiff("w", BegDate, EndDate)
DateCnt = DateAdd("ww", WholeWeeks, BegDate)
EndDays = 0
Do While DateCnt < EndDate
If Format(DateCnt, "ddd"
<> "Sun" And Format(DateCnt, "ddd"
<> "Sat" Then
EndDays = EndDays + 1
End If
DateCnt = DateAdd("d", 1, DateCnt)
Loop
Work_Days = WholeWeeks * 5 + EndDays
End Function
This works great to calculate the difference but the problem is that it is returning the result as text rather than a number. This means that when I try to perform calculations on it in a calculated control I get an #Error value! I have tried changing the format in the field properties to general number but it has no effect.
Does anyone know of a way to solve this please? I don't really want to scrap this method and have to build a holidays table.
Any help would be greatly appreciated! Thanks!
I'm using the function below in a number of queries to calculate date differences in working days.
Function Work_Days(BegDate As Variant, EndDate As Variant) As Integer
' Note that this function does not account for holidays.
Dim WholeWeeks As Variant
Dim DateCnt As Integer
Dim EndDays As Integer
BegDate = DateValue(BegDate)
EndDate = DateValue(EndDate)
WholeWeeks = DateDiff("w", BegDate, EndDate)
DateCnt = DateAdd("ww", WholeWeeks, BegDate)
EndDays = 0
Do While DateCnt < EndDate
If Format(DateCnt, "ddd"
EndDays = EndDays + 1
End If
DateCnt = DateAdd("d", 1, DateCnt)
Loop
Work_Days = WholeWeeks * 5 + EndDays
End Function
This works great to calculate the difference but the problem is that it is returning the result as text rather than a number. This means that when I try to perform calculations on it in a calculated control I get an #Error value! I have tried changing the format in the field properties to general number but it has no effect.
Does anyone know of a way to solve this please? I don't really want to scrap this method and have to build a holidays table.
Any help would be greatly appreciated! Thanks!