Weekdays

D

Duane

I am very new to VBA and would like to know if there is a way to have a
function return the number of week days in a given month for then entire
year?

I have supervisors who are required to make rounds of their assignments on a
daily basis, excluding weekends. I could setup a table and enter the
information by month for the next ten years, but it sure would be nice to
not have to do it that way.

Thanks in advance.
 
D

Dirk Goldgar

Duane said:
I am very new to VBA and would like to know if there is a way to have a
function return the number of week days in a given month for then entire
year?

I have supervisors who are required to make rounds of their assignments on
a daily basis, excluding weekends. I could setup a table and enter the
information by month for the next ten years, but it sure would be nice to
not have to do it that way.


There's not a built-in function for that, but see these links:

http://www.mvps.org/access/datetime/date0006.htm

http://www.mvps.org/access/datetime/date0012.htm
 
D

Duane

Thanks for your response Dirk,

The first link you provided appears to be just what I need, but I can't seem
to get the function to work. I truly feel dumb (newbie dumb), but can you
give me a little explaination how to call this function, or provide me with
a place to read up on it.

Thanks
 
J

John Spencer

Copy the function.
Open a VBA module
Paste the function into the module. Save the module with a name other than
the name of the function.

Then call the function by giving it two dates
WorkingDays(#1/1/2008#,#1/31/2008#)
That will return then number of working days in the month

Personally, I have become a fan of including a calendar table in my
databases that allows me to answer this question and many others with a
query using the calendar table. The simplest version of the table would
have three fields :
TheDate (Date field with one entry for all dates in a period of time)
IsWeekDay (Yes/No field - True = M to Friday)
IsHoliday (Yes/No field - True = this is a holiday)

A 150 years of data takes up less than 3 megabytes of storage. And the
table can be constructed fairly easily with a VBA routine and an update
query.

With the table, your problem is solved with

SELECT Count(TheDate), Format(Thedate,"mmm")
FROM CalendarTable
WHERE Year(TheDate) = 2008
AND IsWeekDay = True
AND isHoliday = False

The following routine makes it easy to create the table. You will need to
figure out how to populate the holidays

Public Sub sBuildCalendarTable(dStartDate As Date, _
dEndDate As Date, _
Optional tfOptionalHolidays As Boolean =
True)

Dim strSQL As String
Dim dbAny As DAO.Database
Set dbAny = DBEngine(0)(0)

'================================================
' Build the Calendar Table
'================================================
strSQL = "CREATE TABLE CalendarTable (" & _
" TheDate DateTime Constraint PKTheDate Primary Key" & _
", IsWeekDay YesNo " & _
", IsHoliday YesNo " & _
", HolidayName Text(50)" & _
")"
dbAny.Execute strSQL, dbFailOnError

sFillCalendarTable dStartDate, dEndDate

'================================================
' Populate Weekday field
'================================================
strSQL = "UPDATE CalendarTable " & _
"SET CalendarTable.IsWeekday = " & _
"Weekday([theDate]) In (2,3,4,5,6)"
dbAny.Execute strSQL, dbFailOnError

End Sub

Private Sub sFillCalendarTable(dStart, dEnd)
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim iCount As Long

On Error GoTo sFillCalendarTable_Error
Set db = CurrentDb()
Set rst = db.OpenRecordset("SELECT TheDate " & _
" FROM CalendarTable" & _
" WHERE TheDate is Null")

With rst
For iCount = 0 To DateDiff("d", dStart, dEnd)
.AddNew
rst!TheDate = DateAdd("d", iCount, dStart)
.Update
Next iCount
End With

Exit Sub

sFillCalendarTable_Error:
Resume Next 'Assumption that error is caused
'by trying to add duplicate record

End Sub



--
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
D

Dirk Goldgar

Duane said:
Thanks for your response Dirk,

The first link you provided appears to be just what I need, but I can't
seem to get the function to work. I truly feel dumb (newbie dumb), but
can you give me a little explaination how to call this function, or
provide me with a place to read up on it.


Did John Spencer's post give you the answers you need? Personally, I don't
see the need to create an actual calendar table, which he recommends, though
a table of holidays (days off that aren't on weekends) can come in handy.
But John also showed you how to call the function shown in the link I
posted.
 

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