G
Guest
I have some code that I use to determine the number of working days between
date1 and date2:
Public Function WorkDays(dtmStart As Date, dtmEnd As Date) As Integer
Dim wkDays As Integer 'this is the counter for the number of days
Dim wkToday As Date ' this is used to increment the date to compare
wkDays = DateDiff("d", dtmStart, dtmEnd) + 1
wkToday = dtmStart
Do Until wkToday > dtmEnd
If WeekDay(wkToday, vbMonday) > 5 Then ' It is a saturday or sunday
wkDays = wkDays - 1 ' for weekend day
ElseIf Not IsNull(DLookup("[Holdate]", "tblHolidays", "[Holdate] =
#" & wkToday & "#")) Then 'It is a holiday
wkDays = wkDays - 1
End If
wkToday = DateAdd("d", 1, wkToday)
Loop
WorkDays = wkDays
End Function
This works great when I have it attached to a form and I pass the variables
to it programatically like this:
Private Sub Command0_Click()
Dim begdate As Date
Dim endDate As Date
begdate = txtBeg.Value
endDate = txtEnd.Value
Text1.Value = basLogic.WorkDays(begdate, endDate)
End Sub
but when I use the function in a query like this:
WorkDays ( [tblTASKsimplified]![act_start_date] ,
[tblTASKsimplified]![act_end_date] )
I get different (wrong) results and it runs extremely slow.
Anyone out there that can help?
date1 and date2:
Public Function WorkDays(dtmStart As Date, dtmEnd As Date) As Integer
Dim wkDays As Integer 'this is the counter for the number of days
Dim wkToday As Date ' this is used to increment the date to compare
wkDays = DateDiff("d", dtmStart, dtmEnd) + 1
wkToday = dtmStart
Do Until wkToday > dtmEnd
If WeekDay(wkToday, vbMonday) > 5 Then ' It is a saturday or sunday
wkDays = wkDays - 1 ' for weekend day
ElseIf Not IsNull(DLookup("[Holdate]", "tblHolidays", "[Holdate] =
#" & wkToday & "#")) Then 'It is a holiday
wkDays = wkDays - 1
End If
wkToday = DateAdd("d", 1, wkToday)
Loop
WorkDays = wkDays
End Function
This works great when I have it attached to a form and I pass the variables
to it programatically like this:
Private Sub Command0_Click()
Dim begdate As Date
Dim endDate As Date
begdate = txtBeg.Value
endDate = txtEnd.Value
Text1.Value = basLogic.WorkDays(begdate, endDate)
End Sub
but when I use the function in a query like this:
WorkDays ( [tblTASKsimplified]![act_start_date] ,
[tblTASKsimplified]![act_end_date] )
I get different (wrong) results and it runs extremely slow.
Anyone out there that can help?