Log in

View Full Version : Access Date Functions


smile4life
Jul 31, 2003, 06:44 AM
I need to count the number of workdays between a range of dates. I know how to simply count the number of days, but can't seem to figure out how I would exclude non-workdays. Any ideas?

Thanks.

tadds
Nov 26, 2003, 08:17 AM
Hi

I've just did a quick hack for you. Here's the code that
you can use. Did a quick test in Excel VBA. Should work
in any VBA (Access, Excel, etc.)


Function GetDayType(dd As Date) As Byte

'--- This function computes day type based on given
Date (dd)
' Return Codes:
' 1 - Weekday (Monday to Friday)
' 2 - Saturday
' 3 - Sunday

If Weekday(dd, 2) >= 1 And Weekday(dd, 2) <= 5 Then
GetDayType = 1
ElseIf Weekday(dd, 2) = 6 Then
GetDayType = 2
ElseIf Weekday(dd, 2) = 7 Then
GetDayType = 3
End If
End Function

Sub Simulate_Get_Weekdays()
Dim StartDate As Date
Dim EndDate As Date
Dim j As Integer '-- day difference
Dim i As Integer '-- for loop index
Dim k As Integer '-- counter for weekdays

StartDate = #11/1/2003#
EndDate = #11/15/2003#

j = DateDiff("d", StartDate, EndDate)
k = 0

For i = 1 To j

'--- Tip: If you use different return code, you
can count weekends too!
If (GetDayType(DateSerial(Year(StartDate), Month
(StartDate), Day(StartDate) + i)) = 1) Then
k = k + 1
End If
Next i

MsgBox k '-- number of weekdays
End Sub


Enjoy!