by RGFuaWVsIF » Wed, 11 Nov 2009 02:52:03
n this specific case you'd need to create a custom function. Below is one I
threw together rapidly to try to help you out. It is not the most elegant
way to do this but it does work. Feel free to improve upon it.
Function NoDaysByMonth(dtStart As Date, dtEnd As Date) As String
On Error GoTo Error_Handler
Dim iNoMonths As Integer
Dim dtFirstofMonth As Date
Dim dtLastOfMonth As Date
iNoMonths = DateDiff("m", dtStart, dtEnd) 'No months between the date
'Dates are in the same month
If iNoMonths = 0 Then
NoDaysByMonth = DateDiff("d", dtStart, dtEnd) + 1 & " days for " &
Format(dtStart, "mmmm, yyyy")
Exit Function
End If
'Dates are in different months
NoDaysByMonth = DateDiff("d", dtStart, DateAdd("m", 1,
DateSerial(Year(dtStart), _
Month(dtStart), 1)) - 1) + 1 & " days for " &
Format(dtStart, "mmmm, yyyy")
For i = 1 To iNoMonths - 1
dtFirstofMonth = DateAdd("m", i, dtStart)
dtFirstofMonth = DateSerial(Year(dtFirstofMonth),
Month(dtFirstofMonth), 1)
dtLastOfMonth = DateAdd("m", i, dtStart)
dtLastOfMonth = DateAdd("m", 1, DateSerial(Year(dtLastOfMonth),
Month(dtLastOfMonth), 1)) - 1
NoDaysByMonth = NoDaysByMonth & vbCrLf & _
DateDiff("d", dtFirstofMonth, dtLastOfMonth) + 1 & "
days for " & _
Format(dtLastOfMonth, "mmmm, yyyy")
Next i
NoDaysByMonth = NoDaysByMonth & vbCrLf & DateDiff("d",
DateSerial(Year(dtEnd), _
Month(dtEnd), 1), dtEnd) + 1 & " days for " &
Format(dtEnd, "mmmm, yyyy")
Exit Function
Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: NoDaysByMonth" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Function
End Function
--
Hope this helps,
Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
"PF" wrote: