KevinNewtoAccess
Technical User
I am trying to calculate the number of working days (excluding weekend & holiday) between 2 dates. I have found this topic covered in the FAQ section but am having trouble with the code listed there.
I have copied & pasted the following code into a new module
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
When I try to use the Deltadays function I get the following visual basic error
"User defined type not defined" with a highlight on the line
Dim dbs as Database
I am sure this is simply down to my lack of understanding of visual basic!
Any suggestions would be much appreciated!
Thanks
Kevin
I have copied & pasted the following code into a new module
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
When I try to use the Deltadays function I get the following visual basic error
"User defined type not defined" with a highlight on the line
Dim dbs as Database
I am sure this is simply down to my lack of understanding of visual basic!
Any suggestions would be much appreciated!
Thanks
Kevin