Date Function

C

Chris

Hi,

I have a field: DateLoggedIn, which stores the date when data is entered,

What I am looking for is a function, and how to apply this function, i.e. in
a query in order to calculate an expression a DueDate which will display a
new date, 3 working days from the DateLoggedIn excluding weekends and
holidays.

I have already a TblHolidays that contains all holidays for the next 12
months.

Example:
DateLoggedIn: 13-Sep-07
DueDate: Should show 17-Sep-07, not 16-Sep-07 as it is a Sunday.

Much help greatly appreciated.
Chris1
 
M

Michael Gramelspacher

This is almost identical to your other thread.

Maybe you can try a calendar table solution.
http://www.psci.net/gramelsp/temp/calendars.zip

Open the table Calendar to get an idea what a calendar table is. This
sample table has data for 1996-1998, same as Northwind.

Create a new database and import the module.
Open the module and in the Immediate Window type:
call CreateCalendar and press return. You have just created an empty
calendar table.

In the Immediate Window type:
call FillCalendar(#1/1/2007#,#12/31/2012#) and press return. You
have just filled the calendar table with data.

The tedious part is entering the holidays. It will take some time,
but after you are finished, you are set for the next five years.

If you prefer, you can edit this and run for each year's holidays. It
may or may not be useful.
call UpdateCalendarHolidays

After all the holidays are entered, you can run this to number all the
workdays. IT is not necessary to do this as the alternate function
which use it are commented out. Any holiday changes require that
workdays be renumbered.
call NumberWorkdays
 
K

Klatuu

Here is a simple function that will do this for you.

'---------------------------------------------------------------------------------------
' Procedure : CalcWorkDays
' DateTime : 5/8/2006 16:34
' Author : Dave Hargis
' Purpose : Counts the number of days between two dates excluding Saturdays,
' : Sundays, and any days in the Holidays table
'---------------------------------------------------------------------------------------
'
Function CalcWorkDays(dtmStart As Date, dtmEnd As Date) As Integer

On Error GoTo CalcWorkDays_Error

'Calculates the number of days between the dates
'Add one so all days are included
CalcWorkDays = DateDiff("d", dtmStart, dtmEnd) - _
(DateDiff("ww", dtmStart, dtmEnd, 7) + _
DateDiff("ww", dtmStart, dtmEnd, 1)) + 1
'Subtract the Holidays
CalcWorkDays = CalcWorkDays - DCount("*", "holidays", "[holdate] between
#" _
& dtmStart & "# And #" & dtmEnd & "#")

CalcWorkDays_Exit:

On Error Resume Next
Exit Function

CalcWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure CalcWorkDays of Module modDateFunctions"
GoTo CalcWorkDays_Exit

End Function
 
C

Chris

Thank you for your help Klatuu,

I have put in the module. But I am still stuck, my knowledge of vba is
limited.
Do I need to change "dtmStart" for my field name "DateLoggedIn", and do I
have to create a new field called "dtmEnd"? it might sound stupid, but I am
lost and yet I feel I am not far.
When applied your module, it came out with numbers of days, which is fine,
but I need to see this as a date, not a digit:
Ie DateloggedIn: 14/09/07
Result of function should give me a new date which would be 3 days max from
the DateLoggedIn, excluding Sat and Sundays and holidays.
The result should be:
Duedate: 19/09/07.
Hope this is not too confusing, but the whole is driving me mad!
DueDate:

Klatuu said:
Here is a simple function that will do this for you.

'---------------------------------------------------------------------------------------
' Procedure : CalcWorkDays
' DateTime : 5/8/2006 16:34
' Author : Dave Hargis
' Purpose : Counts the number of days between two dates excluding Saturdays,
' : Sundays, and any days in the Holidays table
'---------------------------------------------------------------------------------------
'
Function CalcWorkDays(dtmStart As Date, dtmEnd As Date) As Integer

On Error GoTo CalcWorkDays_Error

'Calculates the number of days between the dates
'Add one so all days are included
CalcWorkDays = DateDiff("d", dtmStart, dtmEnd) - _
(DateDiff("ww", dtmStart, dtmEnd, 7) + _
DateDiff("ww", dtmStart, dtmEnd, 1)) + 1
'Subtract the Holidays
CalcWorkDays = CalcWorkDays - DCount("*", "holidays", "[holdate] between
#" _
& dtmStart & "# And #" & dtmEnd & "#")

CalcWorkDays_Exit:

On Error Resume Next
Exit Function

CalcWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure CalcWorkDays of Module modDateFunctions"
GoTo CalcWorkDays_Exit

End Function

--
Dave Hargis, Microsoft Access MVP


Chris said:
Hi,

I have a field: DateLoggedIn, which stores the date when data is entered,

What I am looking for is a function, and how to apply this function, i.e. in
a query in order to calculate an expression a DueDate which will display a
new date, 3 working days from the DateLoggedIn excluding weekends and
holidays.

I have already a TblHolidays that contains all holidays for the next 12
months.

Example:
DateLoggedIn: 13-Sep-07
DueDate: Should show 17-Sep-07, not 16-Sep-07 as it is a Sunday.

Much help greatly appreciated.
Chris1
 
K

Klatuu

Sorry, Chris, I didn't see the part about wanting a date. Here is what you
really need. There are two function, so you need both. Put them both in a
standard module.

You don't need to change anything.

All you need to do is call it with DateLoggedIn
=AddWorkDays(DateLoggedIn, 3)
'---------------------------------------------------------------------------------------
' Procedure : AddWorkDays
' DateTime : 5/9/2006 11:50
' Author : Dave Hargis
' Purpose : Returns a date that is a number of work days from the original
date
' : OriginalDAte is the date from which to calculate
' : DaysToAdd is the number of days to add or subtract from
OriginalDate
'---------------------------------------------------------------------------------------
'
Public Function AddWorkDays(OriginalDate As Date, DaysToAdd As Integer) As
Date
Dim dtmCheckDay As Date 'Keeps track of the date to be tested
Dim lngDayCount As Long 'Counts workdays
Dim lngAddDays As Long 'Counts all days to calculate the return date
Dim lngAdder As Long 'Either +1 or -1 / used to add or subtract a day.

On Error GoTo AddWorkDays_Error

'Set up for negative or postive adding
If DaysToAdd < 0 Then
lngAdder = -1
ElseIf DaysToAdd > 0 Then
lngAdder = 1
Else
lngAdder = 0
End If

dtmCheckDay = OriginalDate

'Icrement through the calender
Do Until lngDayCount = DaysToAdd
If IsWorkDay(dtmCheckDay) Then
'Add +/- 1 to the number of workdays so far
lngDayCount = lngDayCount + lngAdder
End If
'Increment the calendar day
dtmCheckDay = DateAdd("d", lngAdder, dtmCheckDay)
'Add +/- 1 to the actual days
lngAddDays = lngAddDays + lngAdder
Loop

AddWorkDays = DateAdd("d", lngAddDays - lngAdder, OriginalDate)

AddWorkDays_Exit:
On Error GoTo 0

Exit Function

AddWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure AddWorkDays of Module modDateFunctions"
GoTo AddWorkDays_Exit

End Function

'---------------------------------------------------------------------------------------
' Procedure : IsWorkDay
' DateTime : 4/14/2004 10:55
' Author : Dave Hargis
' Purpose :
'---------------------------------------------------------------------------------------
'
Public Function IsWorkDay(dtmSomeDay As Date) As Boolean
Dim blnWorkingDay
On Error GoTo IsWorkDay_Error

blnWorkingDay = Weekday(dtmSomeDay, vbMonday) < 6
If blnWorkingDay Then
blnWorkingDay = IsNull(DLookup("[Holdate]", "Holidays", _
"[Holdate] = #" & dtmSomeDay & "#"))
End If
IsWorkDay = blnWorkingDay

IsWorkDay_Exit:
On Error GoTo 0

Exit Function

IsWorkDay_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure IsWorkDay of Module modDateFunctions"
GoTo IsWorkDay_Exit
End Function



--
Dave Hargis, Microsoft Access MVP


Chris said:
Thank you for your help Klatuu,

I have put in the module. But I am still stuck, my knowledge of vba is
limited.
Do I need to change "dtmStart" for my field name "DateLoggedIn", and do I
have to create a new field called "dtmEnd"? it might sound stupid, but I am
lost and yet I feel I am not far.
When applied your module, it came out with numbers of days, which is fine,
but I need to see this as a date, not a digit:
Ie DateloggedIn: 14/09/07
Result of function should give me a new date which would be 3 days max from
the DateLoggedIn, excluding Sat and Sundays and holidays.
The result should be:
Duedate: 19/09/07.
Hope this is not too confusing, but the whole is driving me mad!
DueDate:

Klatuu said:
Here is a simple function that will do this for you.

'---------------------------------------------------------------------------------------
' Procedure : CalcWorkDays
' DateTime : 5/8/2006 16:34
' Author : Dave Hargis
' Purpose : Counts the number of days between two dates excluding Saturdays,
' : Sundays, and any days in the Holidays table
'---------------------------------------------------------------------------------------
'
Function CalcWorkDays(dtmStart As Date, dtmEnd As Date) As Integer

On Error GoTo CalcWorkDays_Error

'Calculates the number of days between the dates
'Add one so all days are included
CalcWorkDays = DateDiff("d", dtmStart, dtmEnd) - _
(DateDiff("ww", dtmStart, dtmEnd, 7) + _
DateDiff("ww", dtmStart, dtmEnd, 1)) + 1
'Subtract the Holidays
CalcWorkDays = CalcWorkDays - DCount("*", "holidays", "[holdate] between
#" _
& dtmStart & "# And #" & dtmEnd & "#")

CalcWorkDays_Exit:

On Error Resume Next
Exit Function

CalcWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure CalcWorkDays of Module modDateFunctions"
GoTo CalcWorkDays_Exit

End Function

--
Dave Hargis, Microsoft Access MVP


Chris said:
Hi,

I have a field: DateLoggedIn, which stores the date when data is entered,

What I am looking for is a function, and how to apply this function, i.e. in
a query in order to calculate an expression a DueDate which will display a
new date, 3 working days from the DateLoggedIn excluding weekends and
holidays.

I have already a TblHolidays that contains all holidays for the next 12
months.

Example:
DateLoggedIn: 13-Sep-07
DueDate: Should show 17-Sep-07, not 16-Sep-07 as it is a Sunday.

Much help greatly appreciated.
Chris1
 
C

Chris

Thank you so much, it is working!!!! Fantastic, you are a genius!
Chris.

Klatuu said:
Sorry, Chris, I didn't see the part about wanting a date. Here is what you
really need. There are two function, so you need both. Put them both in a
standard module.

You don't need to change anything.

All you need to do is call it with DateLoggedIn
=AddWorkDays(DateLoggedIn, 3)
'---------------------------------------------------------------------------------------
' Procedure : AddWorkDays
' DateTime : 5/9/2006 11:50
' Author : Dave Hargis
' Purpose : Returns a date that is a number of work days from the original
date
' : OriginalDAte is the date from which to calculate
' : DaysToAdd is the number of days to add or subtract from
OriginalDate
'---------------------------------------------------------------------------------------
'
Public Function AddWorkDays(OriginalDate As Date, DaysToAdd As Integer) As
Date
Dim dtmCheckDay As Date 'Keeps track of the date to be tested
Dim lngDayCount As Long 'Counts workdays
Dim lngAddDays As Long 'Counts all days to calculate the return date
Dim lngAdder As Long 'Either +1 or -1 / used to add or subtract a day.

On Error GoTo AddWorkDays_Error

'Set up for negative or postive adding
If DaysToAdd < 0 Then
lngAdder = -1
ElseIf DaysToAdd > 0 Then
lngAdder = 1
Else
lngAdder = 0
End If

dtmCheckDay = OriginalDate

'Icrement through the calender
Do Until lngDayCount = DaysToAdd
If IsWorkDay(dtmCheckDay) Then
'Add +/- 1 to the number of workdays so far
lngDayCount = lngDayCount + lngAdder
End If
'Increment the calendar day
dtmCheckDay = DateAdd("d", lngAdder, dtmCheckDay)
'Add +/- 1 to the actual days
lngAddDays = lngAddDays + lngAdder
Loop

AddWorkDays = DateAdd("d", lngAddDays - lngAdder, OriginalDate)

AddWorkDays_Exit:
On Error GoTo 0

Exit Function

AddWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure AddWorkDays of Module modDateFunctions"
GoTo AddWorkDays_Exit

End Function

'---------------------------------------------------------------------------------------
' Procedure : IsWorkDay
' DateTime : 4/14/2004 10:55
' Author : Dave Hargis
' Purpose :
'---------------------------------------------------------------------------------------
'
Public Function IsWorkDay(dtmSomeDay As Date) As Boolean
Dim blnWorkingDay
On Error GoTo IsWorkDay_Error

blnWorkingDay = Weekday(dtmSomeDay, vbMonday) < 6
If blnWorkingDay Then
blnWorkingDay = IsNull(DLookup("[Holdate]", "Holidays", _
"[Holdate] = #" & dtmSomeDay & "#"))
End If
IsWorkDay = blnWorkingDay

IsWorkDay_Exit:
On Error GoTo 0

Exit Function

IsWorkDay_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure IsWorkDay of Module modDateFunctions"
GoTo IsWorkDay_Exit
End Function



--
Dave Hargis, Microsoft Access MVP


Chris said:
Thank you for your help Klatuu,

I have put in the module. But I am still stuck, my knowledge of vba is
limited.
Do I need to change "dtmStart" for my field name "DateLoggedIn", and do I
have to create a new field called "dtmEnd"? it might sound stupid, but I am
lost and yet I feel I am not far.
When applied your module, it came out with numbers of days, which is fine,
but I need to see this as a date, not a digit:
Ie DateloggedIn: 14/09/07
Result of function should give me a new date which would be 3 days max from
the DateLoggedIn, excluding Sat and Sundays and holidays.
The result should be:
Duedate: 19/09/07.
Hope this is not too confusing, but the whole is driving me mad!
DueDate:

Klatuu said:
Here is a simple function that will do this for you.

'---------------------------------------------------------------------------------------
' Procedure : CalcWorkDays
' DateTime : 5/8/2006 16:34
' Author : Dave Hargis
' Purpose : Counts the number of days between two dates excluding Saturdays,
' : Sundays, and any days in the Holidays table
'---------------------------------------------------------------------------------------
'
Function CalcWorkDays(dtmStart As Date, dtmEnd As Date) As Integer

On Error GoTo CalcWorkDays_Error

'Calculates the number of days between the dates
'Add one so all days are included
CalcWorkDays = DateDiff("d", dtmStart, dtmEnd) - _
(DateDiff("ww", dtmStart, dtmEnd, 7) + _
DateDiff("ww", dtmStart, dtmEnd, 1)) + 1
'Subtract the Holidays
CalcWorkDays = CalcWorkDays - DCount("*", "holidays", "[holdate] between
#" _
& dtmStart & "# And #" & dtmEnd & "#")

CalcWorkDays_Exit:

On Error Resume Next
Exit Function

CalcWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure CalcWorkDays of Module modDateFunctions"
GoTo CalcWorkDays_Exit

End Function

--
Dave Hargis, Microsoft Access MVP


:

Hi,

I have a field: DateLoggedIn, which stores the date when data is entered,

What I am looking for is a function, and how to apply this function, i.e. in
a query in order to calculate an expression a DueDate which will display a
new date, 3 working days from the DateLoggedIn excluding weekends and
holidays.

I have already a TblHolidays that contains all holidays for the next 12
months.

Example:
DateLoggedIn: 13-Sep-07
DueDate: Should show 17-Sep-07, not 16-Sep-07 as it is a Sunday.

Much help greatly appreciated.
Chris1
 
B

babs

I am trying this but keep getting an error Compile error in query expression???

DueDate: AddWorkDays([DateLoggedIn],3)

This is my calc in my query.

My holiday table is just called holidays and not sure what to name the field
for the holidays??? What am I missing.

Does it just interally know the weekend days based on the computers
calendar???

Need your help - in the middle of a class and just got asked this question!!
Goood on e- no easier way to do it in 2007??

Thanks,
Barb
Klatuu said:
Sorry, Chris, I didn't see the part about wanting a date. Here is what you
really need. There are two function, so you need both. Put them both in a
standard module.

You don't need to change anything.

All you need to do is call it with DateLoggedIn
=AddWorkDays(DateLoggedIn, 3)
'---------------------------------------------------------------------------------------
' Procedure : AddWorkDays
' DateTime : 5/9/2006 11:50
' Author : Dave Hargis
' Purpose : Returns a date that is a number of work days from the original
date
' : OriginalDAte is the date from which to calculate
' : DaysToAdd is the number of days to add or subtract from
OriginalDate
'---------------------------------------------------------------------------------------
'
Public Function AddWorkDays(OriginalDate As Date, DaysToAdd As Integer) As
Date
Dim dtmCheckDay As Date 'Keeps track of the date to be tested
Dim lngDayCount As Long 'Counts workdays
Dim lngAddDays As Long 'Counts all days to calculate the return date
Dim lngAdder As Long 'Either +1 or -1 / used to add or subtract a day.

On Error GoTo AddWorkDays_Error

'Set up for negative or postive adding
If DaysToAdd < 0 Then
lngAdder = -1
ElseIf DaysToAdd > 0 Then
lngAdder = 1
Else
lngAdder = 0
End If

dtmCheckDay = OriginalDate

'Icrement through the calender
Do Until lngDayCount = DaysToAdd
If IsWorkDay(dtmCheckDay) Then
'Add +/- 1 to the number of workdays so far
lngDayCount = lngDayCount + lngAdder
End If
'Increment the calendar day
dtmCheckDay = DateAdd("d", lngAdder, dtmCheckDay)
'Add +/- 1 to the actual days
lngAddDays = lngAddDays + lngAdder
Loop

AddWorkDays = DateAdd("d", lngAddDays - lngAdder, OriginalDate)

AddWorkDays_Exit:
On Error GoTo 0

Exit Function

AddWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure AddWorkDays of Module modDateFunctions"
GoTo AddWorkDays_Exit

End Function

'---------------------------------------------------------------------------------------
' Procedure : IsWorkDay
' DateTime : 4/14/2004 10:55
' Author : Dave Hargis
' Purpose :
'---------------------------------------------------------------------------------------
'
Public Function IsWorkDay(dtmSomeDay As Date) As Boolean
Dim blnWorkingDay
On Error GoTo IsWorkDay_Error

blnWorkingDay = Weekday(dtmSomeDay, vbMonday) < 6
If blnWorkingDay Then
blnWorkingDay = IsNull(DLookup("[Holdate]", "Holidays", _
"[Holdate] = #" & dtmSomeDay & "#"))
End If
IsWorkDay = blnWorkingDay

IsWorkDay_Exit:
On Error GoTo 0

Exit Function

IsWorkDay_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure IsWorkDay of Module modDateFunctions"
GoTo IsWorkDay_Exit
End Function



--
Dave Hargis, Microsoft Access MVP


Chris said:
Thank you for your help Klatuu,

I have put in the module. But I am still stuck, my knowledge of vba is
limited.
Do I need to change "dtmStart" for my field name "DateLoggedIn", and do I
have to create a new field called "dtmEnd"? it might sound stupid, but I am
lost and yet I feel I am not far.
When applied your module, it came out with numbers of days, which is fine,
but I need to see this as a date, not a digit:
Ie DateloggedIn: 14/09/07
Result of function should give me a new date which would be 3 days max from
the DateLoggedIn, excluding Sat and Sundays and holidays.
The result should be:
Duedate: 19/09/07.
Hope this is not too confusing, but the whole is driving me mad!
DueDate:

Klatuu said:
Here is a simple function that will do this for you.

'---------------------------------------------------------------------------------------
' Procedure : CalcWorkDays
' DateTime : 5/8/2006 16:34
' Author : Dave Hargis
' Purpose : Counts the number of days between two dates excluding Saturdays,
' : Sundays, and any days in the Holidays table
'---------------------------------------------------------------------------------------
'
Function CalcWorkDays(dtmStart As Date, dtmEnd As Date) As Integer

On Error GoTo CalcWorkDays_Error

'Calculates the number of days between the dates
'Add one so all days are included
CalcWorkDays = DateDiff("d", dtmStart, dtmEnd) - _
(DateDiff("ww", dtmStart, dtmEnd, 7) + _
DateDiff("ww", dtmStart, dtmEnd, 1)) + 1
'Subtract the Holidays
CalcWorkDays = CalcWorkDays - DCount("*", "holidays", "[holdate] between
#" _
& dtmStart & "# And #" & dtmEnd & "#")

CalcWorkDays_Exit:

On Error Resume Next
Exit Function

CalcWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure CalcWorkDays of Module modDateFunctions"
GoTo CalcWorkDays_Exit

End Function

--
Dave Hargis, Microsoft Access MVP


:

Hi,

I have a field: DateLoggedIn, which stores the date when data is entered,

What I am looking for is a function, and how to apply this function, i.e. in
a query in order to calculate an expression a DueDate which will display a
new date, 3 working days from the DateLoggedIn excluding weekends and
holidays.

I have already a TblHolidays that contains all holidays for the next 12
months.

Example:
DateLoggedIn: 13-Sep-07
DueDate: Should show 17-Sep-07, not 16-Sep-07 as it is a Sunday.

Much help greatly appreciated.
Chris1
 
B

babs

figure it out - sorry!!!!

Thanks

Klatuu said:
Sorry, Chris, I didn't see the part about wanting a date. Here is what you
really need. There are two function, so you need both. Put them both in a
standard module.

You don't need to change anything.

All you need to do is call it with DateLoggedIn
=AddWorkDays(DateLoggedIn, 3)
'---------------------------------------------------------------------------------------
' Procedure : AddWorkDays
' DateTime : 5/9/2006 11:50
' Author : Dave Hargis
' Purpose : Returns a date that is a number of work days from the original
date
' : OriginalDAte is the date from which to calculate
' : DaysToAdd is the number of days to add or subtract from
OriginalDate
'---------------------------------------------------------------------------------------
'
Public Function AddWorkDays(OriginalDate As Date, DaysToAdd As Integer) As
Date
Dim dtmCheckDay As Date 'Keeps track of the date to be tested
Dim lngDayCount As Long 'Counts workdays
Dim lngAddDays As Long 'Counts all days to calculate the return date
Dim lngAdder As Long 'Either +1 or -1 / used to add or subtract a day.

On Error GoTo AddWorkDays_Error

'Set up for negative or postive adding
If DaysToAdd < 0 Then
lngAdder = -1
ElseIf DaysToAdd > 0 Then
lngAdder = 1
Else
lngAdder = 0
End If

dtmCheckDay = OriginalDate

'Icrement through the calender
Do Until lngDayCount = DaysToAdd
If IsWorkDay(dtmCheckDay) Then
'Add +/- 1 to the number of workdays so far
lngDayCount = lngDayCount + lngAdder
End If
'Increment the calendar day
dtmCheckDay = DateAdd("d", lngAdder, dtmCheckDay)
'Add +/- 1 to the actual days
lngAddDays = lngAddDays + lngAdder
Loop

AddWorkDays = DateAdd("d", lngAddDays - lngAdder, OriginalDate)

AddWorkDays_Exit:
On Error GoTo 0

Exit Function

AddWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure AddWorkDays of Module modDateFunctions"
GoTo AddWorkDays_Exit

End Function

'---------------------------------------------------------------------------------------
' Procedure : IsWorkDay
' DateTime : 4/14/2004 10:55
' Author : Dave Hargis
' Purpose :
'---------------------------------------------------------------------------------------
'
Public Function IsWorkDay(dtmSomeDay As Date) As Boolean
Dim blnWorkingDay
On Error GoTo IsWorkDay_Error

blnWorkingDay = Weekday(dtmSomeDay, vbMonday) < 6
If blnWorkingDay Then
blnWorkingDay = IsNull(DLookup("[Holdate]", "Holidays", _
"[Holdate] = #" & dtmSomeDay & "#"))
End If
IsWorkDay = blnWorkingDay

IsWorkDay_Exit:
On Error GoTo 0

Exit Function

IsWorkDay_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure IsWorkDay of Module modDateFunctions"
GoTo IsWorkDay_Exit
End Function



--
Dave Hargis, Microsoft Access MVP


Chris said:
Thank you for your help Klatuu,

I have put in the module. But I am still stuck, my knowledge of vba is
limited.
Do I need to change "dtmStart" for my field name "DateLoggedIn", and do I
have to create a new field called "dtmEnd"? it might sound stupid, but I am
lost and yet I feel I am not far.
When applied your module, it came out with numbers of days, which is fine,
but I need to see this as a date, not a digit:
Ie DateloggedIn: 14/09/07
Result of function should give me a new date which would be 3 days max from
the DateLoggedIn, excluding Sat and Sundays and holidays.
The result should be:
Duedate: 19/09/07.
Hope this is not too confusing, but the whole is driving me mad!
DueDate:

Klatuu said:
Here is a simple function that will do this for you.

'---------------------------------------------------------------------------------------
' Procedure : CalcWorkDays
' DateTime : 5/8/2006 16:34
' Author : Dave Hargis
' Purpose : Counts the number of days between two dates excluding Saturdays,
' : Sundays, and any days in the Holidays table
'---------------------------------------------------------------------------------------
'
Function CalcWorkDays(dtmStart As Date, dtmEnd As Date) As Integer

On Error GoTo CalcWorkDays_Error

'Calculates the number of days between the dates
'Add one so all days are included
CalcWorkDays = DateDiff("d", dtmStart, dtmEnd) - _
(DateDiff("ww", dtmStart, dtmEnd, 7) + _
DateDiff("ww", dtmStart, dtmEnd, 1)) + 1
'Subtract the Holidays
CalcWorkDays = CalcWorkDays - DCount("*", "holidays", "[holdate] between
#" _
& dtmStart & "# And #" & dtmEnd & "#")

CalcWorkDays_Exit:

On Error Resume Next
Exit Function

CalcWorkDays_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure CalcWorkDays of Module modDateFunctions"
GoTo CalcWorkDays_Exit

End Function

--
Dave Hargis, Microsoft Access MVP


:

Hi,

I have a field: DateLoggedIn, which stores the date when data is entered,

What I am looking for is a function, and how to apply this function, i.e. in
a query in order to calculate an expression a DueDate which will display a
new date, 3 working days from the DateLoggedIn excluding weekends and
holidays.

I have already a TblHolidays that contains all holidays for the next 12
months.

Example:
DateLoggedIn: 13-Sep-07
DueDate: Should show 17-Sep-07, not 16-Sep-07 as it is a Sunday.

Much help greatly appreciated.
Chris1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top