G
Guest
Here are two functions. The first is a variation of your number of working
days. I think this one is a little better. The second is the one you
requested to add 5 days.
In additions to other changes, note I changed <= 5 to < 5. When the counter
gets to 5, you have already added 5 working days.
Public Function WorkingDays(StartDate As Date, EndDate As Date) As Integer
Dim intCount As Integer
On Error GoTo Err_WorkingDays
intDayCount = Datediff("d",StartDate, EndDate)
Do While StartDate <= EndDate
If WeekDay(StartDate, vbMonday) > 5 Then
intDayCount = intDayCount - 1)
Else
If Not IsNull(dlookup("[HolDate]","Holidays","[HolDate] = " & "'"
& StartDate & "'")) Then
intDayCount = intDayCount -1)
End If
End If
StartDate = Dateadd("d", 1, StartDate)
Loop
WorkingDays = intDayCount
Exit_WorkingDays:
Exit Function
Public Function Add5WeekDays(OriginalDate As Date) As Date
Dim intDayCount as Integer
Dim dtmReturnDate as Date
intDayCount = 0
dtmReturnDate = OriginalDAte
Do Until intDayCount = 5
If WeekDay(dtmReturnDate, vbMonday) < 5 Then
intDayCount = intDayCount + 1)
dtmReturnDate = Dateadd("d",1,dtmReturnDate)
Else
If IsNull(dlookup("[HolDate]","Holidays","[HolDate] = " & "'" &
dtmReturnDate & "'")) Then
intDayCount = intDayCount +1)
dtmReturnDate = Dateadd("d",1,dtmReturnDate)
End If
End If
Loop
Add5WeekDays = OriginalDate
days. I think this one is a little better. The second is the one you
requested to add 5 days.
In additions to other changes, note I changed <= 5 to < 5. When the counter
gets to 5, you have already added 5 working days.
Public Function WorkingDays(StartDate As Date, EndDate As Date) As Integer
Dim intCount As Integer
On Error GoTo Err_WorkingDays
intDayCount = Datediff("d",StartDate, EndDate)
Do While StartDate <= EndDate
If WeekDay(StartDate, vbMonday) > 5 Then
intDayCount = intDayCount - 1)
Else
If Not IsNull(dlookup("[HolDate]","Holidays","[HolDate] = " & "'"
& StartDate & "'")) Then
intDayCount = intDayCount -1)
End If
End If
StartDate = Dateadd("d", 1, StartDate)
Loop
WorkingDays = intDayCount
Exit_WorkingDays:
Exit Function
Public Function Add5WeekDays(OriginalDate As Date) As Date
Dim intDayCount as Integer
Dim dtmReturnDate as Date
intDayCount = 0
dtmReturnDate = OriginalDAte
Do Until intDayCount = 5
If WeekDay(dtmReturnDate, vbMonday) < 5 Then
intDayCount = intDayCount + 1)
dtmReturnDate = Dateadd("d",1,dtmReturnDate)
Else
If IsNull(dlookup("[HolDate]","Holidays","[HolDate] = " & "'" &
dtmReturnDate & "'")) Then
intDayCount = intDayCount +1)
dtmReturnDate = Dateadd("d",1,dtmReturnDate)
End If
End If
Loop
Add5WeekDays = OriginalDate
NewbieSupreme said:I have an MVP-provided fnction I use to determine the number of workdays
taken for several phases of an order. The code is below (as you can see, I
use a Table called Holidays to exclude holiday dates as well):
Public Function WorkingDays(StartDate As Date, EndDate As Date) As Integer
On Error GoTo Err_WorkingDays
Dim intCount As Integer
Dim rst As DAO.Recordset
Dim DB As DAO.Database
Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT [HolDate] FROM Holidays", dbOpenSnapshot)
StartDate = StartDate + 1
intCount = 0
Do While StartDate <= EndDate
rst.FindFirst "[HolDate] = #" & StartDate & "#"
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday Then
If rst.NoMatch Then intCount = intCount + 1
End If
StartDate = StartDate + 1
Loop
WorkingDays = intCount
Exit_WorkingDays:
Exit Function
Err_WorkingDays:
Select Case Err
Case Else
MsgBox Err.Description
Resume Exit_WorkingDays
End Select
End Function
On a form, I would like to make the default value of the PromisedDate
control equal to 5 working days after the same form's ReceivedDate. This
value would be set in the AfterUpdate event of ReceivedDate. I have a
function (MVP-supplied again, of course) that works in terms of Weekdays,
but it does not use the Holidays table. I tried several permutations using
the Holidays table references from the other function, but my coding
knowledge is not sufficient to add the use of the Holidays table. The
default value setting function is below:
Public Function Add5WeekDays(OriginalDate As Date) As Date
Dim x As Integer
OriginalDate = forms(frmOrderEntry)!DateRec
x = 0
Do Until x = 5
DayOfWeek = Weekday(DateAdd("d", 1, OriginalDate))
Debug.Print DayOfWeek
Select Case DayOfWeek
Case 1
OriginalDate = DateAdd("d", 1, OriginalDate)
Case 2 To 6
OriginalDate = DateAdd("d", 1, OriginalDate)
x = x + 1
Debug.Print "x = " & x
Case 7
OriginalDate = DateAdd("d", 1, OriginalDate)
End Select
Loop
Add5WeekDays = OriginalDate
End Function
All I need to do is include the looking up and excluding of date values in
the Holidays table to this function. Can anyone tell me how to do this?
Any help is greatly appreciated, and thanks for reading.
NewbieSupreme said:I have an MVP-provided fnction I use to determine the number of workdays
taken for several phases of an order. The code is below (as you can see, I
use a Table called Holidays to exclude holiday dates as well):
Public Function WorkingDays(StartDate As Date, EndDate As Date) As Integer
On Error GoTo Err_WorkingDays
Dim intCount As Integer
Dim rst As DAO.Recordset
Dim DB As DAO.Database
Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT [HolDate] FROM Holidays", dbOpenSnapshot)
StartDate = StartDate + 1
intCount = 0
Do While StartDate <= EndDate
rst.FindFirst "[HolDate] = #" & StartDate & "#"
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <> vbSaturday Then
If rst.NoMatch Then intCount = intCount + 1
End If
StartDate = StartDate + 1
Loop
WorkingDays = intCount
Exit_WorkingDays:
Exit Function
Err_WorkingDays:
Select Case Err
Case Else
MsgBox Err.Description
Resume Exit_WorkingDays
End Select
End Function
On a form, I would like to make the default value of the PromisedDate
control equal to 5 working days after the same form's ReceivedDate. This
value would be set in the AfterUpdate event of ReceivedDate. I have a
function (MVP-supplied again, of course) that works in terms of Weekdays,
but it does not use the Holidays table. I tried several permutations using
the Holidays table references from the other function, but my coding
knowledge is not sufficient to add the use of the Holidays table. The
default value setting function is below:
Public Function Add5WeekDays(OriginalDate As Date) As Date
Dim x As Integer
OriginalDate = forms(frmOrderEntry)!DateRec
x = 0
Do Until x = 5
DayOfWeek = Weekday(DateAdd("d", 1, OriginalDate))
Debug.Print DayOfWeek
Select Case DayOfWeek
Case 1
OriginalDate = DateAdd("d", 1, OriginalDate)
Case 2 To 6
OriginalDate = DateAdd("d", 1, OriginalDate)
x = x + 1
Debug.Print "x = " & x
Case 7
OriginalDate = DateAdd("d", 1, OriginalDate)
End Select
Loop
Add5WeekDays = OriginalDate
End Function
All I need to do is include the looking up and excluding of date values in
the Holidays table to this function. Can anyone tell me how to do this?
Any help is greatly appreciated, and thanks for reading.