You need to test for the last day of Month.
Function :
Public Function DaysInMonth( _
intMonth As Integer, _
intYear As Integer) _
As Integer
’ Comments : Returns the number of days in a month
’ Parameters: intMonth - number of the month (1-12)
’ intYear - Year for the month for considering leap
’ years.
’ Note: Always specify the full four digits of the year
’ or the code cannot be considered Year 2000 compliant.
’ Returns : Number of days in the month
’ Source : Greg
’
Dim datDate As Date
On Error GoTo PROC_ERR
’ Last day of month
datDate = DateSerial(intYear, intMonth + 1, 0)
DaysInMonth = Day(datDate)
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
“DaysInMonth”
Resume PROC_EXIT
End Function
OR
ublic Function MonthFirstDay( _
intMonth As Integer, _
intYear As Integer) _
As Date
’ Comments : Returns the date of the first day of the month
’ Parameters: intMonth - Number of the month (1-12)
’ intYear - Year to check
’ Note: Always specify the full four digits of the year
’ or the code cannot be considered Year 2000 compliant.
’ Returns : Date of the first day of the month
’ Source : GREG
’
On Error GoTo PROC_ERR
MonthFirstDay = DateSerial(intYear, intMonth, 1)
PROC_EXIT:
Exit Function
PROC_ERR:
MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
“MonthFirstDay”
Resume PROC_EXIT
End Function
Example:
’ To use this example:
’ 1. Create a new form.
’ 2. Create a command button called cmdTest
’ 3. Paste the entire contents of this module into the
’ new form’s module.
’
Private Sub cmdTest_Click()
Dim datTest As Date
Dim datTest2 As Date
Dim intWeeks As Integer
datTest = CVDate(“12/13/1990”)
datTest2 = CVDate(“05/12/1998”)
Debug.Print datTest & " plus 30 weekdays is " & _
AddWeekdays(datTest, 30)
Debug.Print “If you were born on " & datTest & _
“, you would be " & AgeCalc(datTest, intWeeks) & _
" years (” & intWeeks & " weeks) old”
Debug.Print “There are " & DaysInMonth(2, 1999) & _
" days in February, 1999.”
Debug.Print "There are " & DiffDays(datTest, datTest2) & _
" days between " & datTest & " and " & datTest2
Debug.Print "There are " & DiffDaysFractional(datTest, datTest2) & _
" fractional days between " & datTest & " and " & datTest2
Debug.Print "There are " & DiffMinutes(datTest, datTest2) & _
" minutes between " & datTest & " and " & datTest2
Debug.Print "There are " & DiffMinutesFractional(datTest, datTest2) & _
" fractional minutes between " & datTest & " and " & datTest2
Debug.Print "There are " & DiffSeconds(datTest, datTest2) & _
" seconds between " & datTest & " and " & datTest2
Debug.Print "There are " & DiffWeekdays(datTest, datTest2) & _
" weekdays between " & datTest & " and " & datTest2
Debug.Print "There are " & DiffWeeks(datTest, datTest2) & _
" weeks between " & datTest & " and " & datTest2
Debug.Print "The first day of the week for date " & datTest & _
" is " & FirstDayOfWeek(datTest)
Debug.Print "The last day of the week for date " & datTest & _
" is " & LastDayOfWeek(datTest)
Debug.Print "The last Sunday in the month for date " & _
datTest & " is " & LastDayOfWeekInMonth(12, 1990, 1)
Debug.Print "The first day of the Feburary 1999 is " & _
MonthFirstDay(2, 1999)
Debug.Print "The first weekday in Feburary 1999 is " & _
MonthFirstWeekday(2, 1999)
Debug.Print "The last day of the month in February 2000 is " & _
MonthLastDay(2, 2000)
Debug.Print "The last weekday of the month in February 2000 is " & _
MonthLastWeekday(2, 2000)
Debug.Print "The day after 02/28/2000 is " & _
NextDate(#2/28/2000#)
Debug.Print "The next Sunday after " & datTest & " is " & _
NextDOW(datTest, 1)
Debug.Print "The next weekday after " & datTest & " is " & _
NextWeekday(datTest)
Debug.Print "The 2nd Sunday of February 1998 is " & _
NthDayOfMonth(2, 1998, 2, 1)
Debug.Print "One day before " & datTest & " is " & _
PriorDate(datTest)
Debug.Print "The last Sunday before " & datTest & " is " & _
PriorDOW(datTest, 1)
Debug.Print "The previous weekday before " & datTest & " is " & _
PriorWeekday(datTest)
Debug.Print "First day of the quarter for date " & datTest _
& " is " & QuarterFirstDay(datTest)
Debug.Print "The last day of the quarter for date " & datTest _
& " is " & QuarterLastDay(datTest)
Debug.Print “I’m waiting for 5 seconds…”
WaitSeconds 5
Debug.Print “… done.”
Debug.Print "There are " & YearsBetweenDates(datTest, datTest2) _
& " years between " & datTest & " and " & datTest2
Debug.Print "1990 " & _
IIf(IsLeapYear(1990), "is “, “isn’t”) & _
" a leap year.”
Debug.Print "1992 " & _
IIf(IsLeapYear(1992), "is “, “isn’t”) & _
" a leap year.”
Debug.Print “2000 " & _
IIf(IsLeapYear2(2000), “is”, “isn’t”) & _
" a leap year.”
End Sub