Hey guys and gals, I am trying to run a function that will calulate the number of workdays between 2 dates and take into account holidays as listed in a seperate table. I found the info below in the Tek-Tips archives, but can't seem to get it to work. I have saved the info into a module and tried to insert it into a query. I get the error: "Undefined function "DeltaDays" in expression".
I'm probably overlooking something very simple, but would appreciate any ideas you might have. The code is below:
*******************************************************
Public Function DeltaDays(StartDate As Date, EndDate As Date) As Integer
'Get the number of workdays between the given dates
Dim dbs As Database
Dim rstHolidays As Recordset
Dim Idx As Long
Dim MyDate As Date
Dim NumDays As Long
Dim strCriteria As String
Dim NumSgn As String * 1
Set dbs = CurrentDb
Set rstHolidays = dbs.OpenRecordset("tblHolidays", dbOpenDynaset)
NumSgn = Chr(35)
MyDate = Format(StartDate, "Short Date"
For Idx = CLng(StartDate) To CLng(EndDate)
Select Case (WeekDay(MyDate))
Case Is = 1 'Sunday
'Do Nothing, it is NOT a Workday
Case Is = 7 'Saturday
'Do Nothing, it is NOT a Workday
Case Else 'Normal Workday
strCriteria = "[HoliDate] = " & NumSgn & MyDate & NumSgn
rstHolidays.FindFirst strCriteria
If (rstHolidays.NoMatch) Then
NumDays = NumDays + 1
Else
'Do Nothing, it is NOT a Workday
End If
End Select
MyDate = DateAdd("d", 1, MyDate)
Next Idx
DeltaDays = NumDays
End Function
*********************************************
Thanks!
I'm probably overlooking something very simple, but would appreciate any ideas you might have. The code is below:
*******************************************************
Public Function DeltaDays(StartDate As Date, EndDate As Date) As Integer
'Get the number of workdays between the given dates
Dim dbs As Database
Dim rstHolidays As Recordset
Dim Idx As Long
Dim MyDate As Date
Dim NumDays As Long
Dim strCriteria As String
Dim NumSgn As String * 1
Set dbs = CurrentDb
Set rstHolidays = dbs.OpenRecordset("tblHolidays", dbOpenDynaset)
NumSgn = Chr(35)
MyDate = Format(StartDate, "Short Date"
For Idx = CLng(StartDate) To CLng(EndDate)
Select Case (WeekDay(MyDate))
Case Is = 1 'Sunday
'Do Nothing, it is NOT a Workday
Case Is = 7 'Saturday
'Do Nothing, it is NOT a Workday
Case Else 'Normal Workday
strCriteria = "[HoliDate] = " & NumSgn & MyDate & NumSgn
rstHolidays.FindFirst strCriteria
If (rstHolidays.NoMatch) Then
NumDays = NumDays + 1
Else
'Do Nothing, it is NOT a Workday
End If
End Select
MyDate = DateAdd("d", 1, MyDate)
Next Idx
DeltaDays = NumDays
End Function
*********************************************
Thanks!