G
Guest
How can I calculate the number of work days (no Sat, Sun or Holidays) between
2 dates and use this for present dates and future dates?
2 dates and use this for present dates and future dates?
How can I calculate the number of work days (no Sat, Sun or Holidays)
between 2 dates and use this for present dates and future dates?
Thank you, Dirk,
I have added a public function using the Holiday Table, however the
DAO. stmts are stopping this function from working. Do you have any
suggestions?
I apologize in advance because I haven't used functions in much
capacity. I am pasting the code and also the error I receive after
applying this function to my query. I appreciate all your help.
Ginger
Test Query:
NumDays: WorkDays([Est Close],[Approval Date])
Public Function WorkDays(StartDate As Date, EndDate As Date) As
Integer
'Name: WorkDays
'Inputs: StartDate As Date
' EndDate As Date
'Returns: Integer
'Author: Ginger Frye
'Date: August 16, 2005
'Comment: Accepts two dates and returns the number of weekdays
between them 'Note that this function will account for holidays. It
requires a table 'named Holidays tbl with a field named HolidayDate.
'-----------------------------------------------------------------------
----
On Error GoTo Err_WorkDays
Dim intCount As Integer
Dim rst As DAO.Recordset
Dim DB As DAO.Database
Set DB = CurrentDb
Set rst = DB.OpenRecordset("SELECT [HolidayDate]FROM Holidays tbl",
dbOpenSnapshot)
'StartDate = StartDate+1
'To count StartDate as the 1st day comment out the line above
intCount = 0
Do While StartDate <= EndDate
rst.FindFirst "[HolidayDate]= #" & StartDate & "#"
If Weekday(StartDate) <> vbSunday And Weekday(StartDate) <>
vbSaturday Then If rst.NoMatch Then intCount = intCount + 1
End If
StartDate = StartDate + 1
Loop
WorkDays = intCount
Exit_WorkDays:
Exit Function
Err_WorkDays:
Select Case Err
Case Else
MsgBox Err.Description
Resume Exit_WorkDays
End Select
End Function
The error I received is Undefined Funtion 'WorkDays' in expression.
It does nothing when I try to Debug / Compile. The module name is
WorkDays and the box at the left (when in design view) shows
(general). This is a drop down box with no dropdown items available.
I have made this change but still get no Debug / Compile, nor does
the "Step Into" work.
I thought this function would do a "compile" and possibly note
anything missing, but like I said, I am not an advanced user of
Modules and using Functions.
I did go back to the query and ran it.
It will now give me information and I have verified the data it
returns. It looks like I have a working function now.
Thank you so
much for your help. I will not claim myself as the author....that
was not my intention...I labeled it that way for internal company
purposes. Again thank you for your help.
Sam said:Dirk
I have read this thread and found it to be most useful! I too had a
problem with eliminating weekends and after reading this thread have
worked out what I was going wrong (Thanks Heaps)
I do however have a problem with this code calculating the number of
days when a date field is left blank. I will expand on this:-
I have seven date fields in my data base. When a user places the file
into a differnet status, it auto records a date in one of these
fields based on a calculation between the data data entered and the
date the file was placed in the status. I have created a query based
on these tables.
There are occasions where some or all of these fields will not need
to have a date inserted (actually these would be at least one that
would be populated) Where there is no date populated I get "#Error"
where the date field is blank.
How can I overcome this problem?? Should I set up the table to
default a zero (0) in the field so that in the event that there is no
Data a 0 is listed or is there a way to adapt the code below to
compensate for this?
I hope my explanantion has not confused you, however if I have not
explained myself correctly, I will gladly expand on this if need be.
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.