The following functions programmed in VB are made to give the first or the last day of a given month (passed in as a date). After looking and trying some examples in the forums, I could not get any to work correctly since they would error either when on the first or last day of the month. I hope this helps, and though it may be a bit heavy on overhead, it seems to work well for the dates I am working with. The first function finds the first day of the month in the given date. The second function finds the last day of the month in the given date by calling the first function. The third function finds the last day of the month in the given date without a call to the first function. They are all included for anyone who wants the options. Note: The functions are long so lines may be cut from site formatting.
[tt]
Public Function FirstDayOfMonth(ByVal sdate As Date) As Date
FirstDayOfMonth = (DatePart("m", sdate) & "/01/" & DatePart("yyyy", sdate))
End Function
Public Function LastDayOfMonth(ByVal sdate As Date) As Date
LastDayOfMonth = DateAdd("m", 1, FirstDayOfMonth(sdate)) - 1
End Function
Public Function LastDayOfMonthInd(ByVal sdate As Date) As Date
LastDayOfMonth = DateAdd("m", 1, FirstDayOfMonth(sdate)) - 1
End Function
[/tt]
[tt]
Public Function FirstDayOfMonth(ByVal sdate As Date) As Date
FirstDayOfMonth = (DatePart("m", sdate) & "/01/" & DatePart("yyyy", sdate))
End Function
Public Function LastDayOfMonth(ByVal sdate As Date) As Date
LastDayOfMonth = DateAdd("m", 1, FirstDayOfMonth(sdate)) - 1
End Function
Public Function LastDayOfMonthInd(ByVal sdate As Date) As Date
LastDayOfMonth = DateAdd("m", 1, FirstDayOfMonth(sdate)) - 1
End Function
[/tt]