Hi All,
I am using the below code to calculate number of business days excluding Holidays. The code works great unless the date lands on a holiday. For some reason the current code I have does not register the "Do Until" statement correctly when I = holiday date (works fine if the holiday is not the final day. Do you know what might be wrong with my "Do Until" Statement?
This is what I am using for my do until. I put in variables to test if the conditions were being met and it did seem that they were. I'm thinking my error lies with the "Dcount" statement?
Do Until i = NoOfDays And Weekday(tmpDate) <> 1 And Weekday(tmpDate) <> 7 And DCount("*", "tblExceptionDates", "[Holiday] = #" & tmpDate & "#") > 0
Below is the full code:
Public Function CountDays1(startDate As Date, NoOfDays As Integer) As Date
' Function to count no of working days
Dim tmpDate As Date
Dim tmpStartDate As Date
Dim i As Integer
Dim dbs As Object
Dim rst_holiday_table As Recordset
tmpNo = NoOfDays
tmpStartDate = startDate
tmpDate = startDate
Set dbs = CurrentDb
Set rst_holiday_table = dbs.OpenRecordset("SELECT tblExceptionDates.Holiday FROM tblExceptionDates;")
i = 0
Do Until i = NoOfDays And Weekday(tmpDate) <> 1 And Weekday(tmpDate) <> 7 And DCount("*", "tblExceptionDates", "[Holiday] = #" & tmpDate & "#") > 0
If Weekday(tmpDate) = 1 Or Weekday(tmpDate) = 7 Then
tmpNo = tmpNo + 1
ElseIf DCount("*", "tblExceptionDates", "[Holiday] = #" & tmpDate & "#") > 0 Then
tmpNo = tmpNo + 1
Else
i = i + 1
End If
tmpDate = tmpDate + 1
Loop
End If
CountDays1 = DateAdd("d", tmpNo, tmpStartDate)
End Function
Thanks and Best Regards,
Mike
I am using the below code to calculate number of business days excluding Holidays. The code works great unless the date lands on a holiday. For some reason the current code I have does not register the "Do Until" statement correctly when I = holiday date (works fine if the holiday is not the final day. Do you know what might be wrong with my "Do Until" Statement?
This is what I am using for my do until. I put in variables to test if the conditions were being met and it did seem that they were. I'm thinking my error lies with the "Dcount" statement?
Do Until i = NoOfDays And Weekday(tmpDate) <> 1 And Weekday(tmpDate) <> 7 And DCount("*", "tblExceptionDates", "[Holiday] = #" & tmpDate & "#") > 0
Below is the full code:
Public Function CountDays1(startDate As Date, NoOfDays As Integer) As Date
' Function to count no of working days
Dim tmpDate As Date
Dim tmpStartDate As Date
Dim i As Integer
Dim dbs As Object
Dim rst_holiday_table As Recordset
tmpNo = NoOfDays
tmpStartDate = startDate
tmpDate = startDate
Set dbs = CurrentDb
Set rst_holiday_table = dbs.OpenRecordset("SELECT tblExceptionDates.Holiday FROM tblExceptionDates;")
i = 0
Do Until i = NoOfDays And Weekday(tmpDate) <> 1 And Weekday(tmpDate) <> 7 And DCount("*", "tblExceptionDates", "[Holiday] = #" & tmpDate & "#") > 0
If Weekday(tmpDate) = 1 Or Weekday(tmpDate) = 7 Then
tmpNo = tmpNo + 1
ElseIf DCount("*", "tblExceptionDates", "[Holiday] = #" & tmpDate & "#") > 0 Then
tmpNo = tmpNo + 1
Else
i = i + 1
End If
tmpDate = tmpDate + 1
Loop
End If
CountDays1 = DateAdd("d", tmpNo, tmpStartDate)
End Function
Thanks and Best Regards,
Mike