Is there any function already made that finds the # of a certain weekday in a month? For example, if I wanted to find the number of sundays for August 2010 it would return five. I have searched the web but have not found anything yet.
Is there any function already made that finds the # of a certain weekday in a month? For example, if I wanted to find the number of sundays for August 2010 it would return five. I have searched the web but have not found anything yet.
There's probably a more efficient way of doing this, but this works *shrug*
Public Function NumSundays(MonthName As String, intYear As Integer) As Integer
Dim FirstofMonth, LastDayofMonth As Date
Dim dDay As Date
Dim DayCount As Integer
'Converts Month/Year to date (will return first of month)
FirstofMonth = (CDate(MonthName & " " & intYear))
LastDayofMonth = DateSerial(Year(FirstofMonth), Month(FirstofMonth) + 1, 0)
DayCount = 0
'Loops through days of month
For dDay = FirstofMonth To LastDayofMonth
If WeekdayName(Weekday(dDay)) = "Sunday" Then
DayCount = DayCount + 1
End If
Next
NumSundays = DayCount
End Function
Last edited by trb5016; 08-18-2010 at 12:16 PM. Reason: or not
With the weekday() function, you can determine on which day of the week a date falls. I would guess that you could create a custom function that loops through the days of a month (you provide the month and the year) and counts the number of Sundays. To make it a little more versatile, you can also supply the day of the week for which you want the count (not just Sunday).
I went ahead and created a simple form that utilizes code in the on click event of the button that does the calculation. The database is attached.
Lots of help on this one. Here's another: http://www.mvps.org/access/datetime/date0011.htm