Here is a function that returns the number of working days between two dates.
It will require that you have a holiday table. Mine has 2 fields, Holdate
and Holdate_Description. You can take out the code that refers to the
holiday table if you don't want to count holidays. Also, it counts every
day, for example, if you put #5/26/2005# and #5/31/2005# it will return 3.
Opps! I just reread your post. You want a date based on a date + a number
of days.
So now your have it Both ways!
Public Function AddWorkDays(OriginalDate As Date, DaysToAdd As Integer) As
Date
Dim intDayCount As Integer
Dim intNotADay As Integer
Dim dtmReturnDate As Date
intDayCount = 0
dtmReturnDate = DateAdd("d", -1, OriginalDate)
Do While True
If Weekday(OriginalDate, vbMonday) <= 5 Then
If IsNull(DLookup("[HolDate]", "Holidays", _
"[HolDate] = #" & OriginalDate & "#")) Then
intDayCount = intDayCount + 1
dtmReturnDate = OriginalDate
End If
End If
If intDayCount = DaysToAdd Then
Exit Do
End If
OriginalDate = DateAdd("d", 1, OriginalDate)
Loop
AddWorkDays = dtmReturnDate
End Function
As to your other question, the answer is yes, but. The one thing I am
unsure of is where to put the code. The reason being that in the example I
gave previously, entering value in text box 1 updates the value in text box 2
using the After Update event. In this case, to update text box 3 from text
box 2 After Update will not work. The After Update event does not fire if
the value is changed via VBA.
Function CalcWorkDays(dtmStart As Date, dtmEnd As Date) As Integer
Dim intTotalDays As Integer ' Counter for number of days
Dim dtmToday As Date ' To increment the date to compare
'Compliments of Dave Hargis
intTotalDays = DateDiff("d", dtmStart, dtmEnd) + 1 'Start with total days
'Add one to include
First Day
dtmToday = dtmStart 'Initiate compare date
Do Until dtmToday > dtmEnd
If Weekday(dtmToday, vbMonday) > 5 Then 'It is Saturday or
Sunday
intTotalDays = intTotalDays - 1 'Take one day away
for Weekend day
ElseIf Not IsNull(DLookup("[Holdate]", "Holidays", _
"[Holdate] = #" & dtmToday & "#")) Then 'It is a holiday
intTotalDays = intTotalDays - 1 'Take one day away
for the Holiday
End If
dtmToday = DateAdd("d", 1, dtmToday) 'Add a day for next
compare
Loop 'Until dtmToday > dtmEnd All days have been
compared
CalcWorkDays = intTotalDays 'Return the value
End Function
Paul said:
Hello Klatuu,
Another question i am afraid:
If I had a text box called txtThirdBox, could that lookup the text that was
created in txtSecondBox that was brought in from txtFirstBox?
I ask this because I have 8 text boxes that I would like to update, each one
copying from the previous one.
Good question, yes?
Regards,
Paul