Thursday, December 10, 2009

Find First day and Lastday of the month

Below is the code to find the first day and last day of the current month in vb code.

'------------------------------------------------------------------
Dim StartDate As Date
Dim EndDate As Date

StartDate = DateSerial(CInt(Format(Date, "yyyy")), CInt(Format(Date, "mm")) - 1, 1)
EndDate = MonthLastDay(DateSerial(CInt(Format(Date, "yyyy")), CInt(Format(Date, "mm")) - 1, 1))
'--------------------------------------------------------------------------

Public Function MonthLastDay(ByVal dCurrDate As Date)
Dim MonthFirstDay As Date

On Error GoTo lbl_Error

MonthLastDay = Empty
MonthFirstDay = DateSerial(CInt(Format(Date, "yyyy")), CInt(Format(Date, "mm")) + 1, 1)
MonthLastDay = DateAdd("d", -1, MonthFirstDay)

Exit Function
lbl_Error:
MsgBox Err.Description, vbOKOnly + vbExclamation
End Function