JohnG said:
The tables i have is as follows
empl Started Employment Ended Employement
joe Jan 1, 2005
Sam Jan 20, 2005
Betty Feb 20, 2005 June 1, 2005
The report I would like looks like
Report 1: Simple count on 1st of each month
Jan Feb March April May June July Aug Sept Oct Nov Dev
1 2 3 3 3 3 2 2 2 2
2 2
Report 2: Avg Heads in Month
Jan Feb March April May June July Aug Sept Oct Nov Dev
1.2 2.2 3 3 3 3 2 2 2 2
2 2
I used the following functions:
Function Round(varIn As Variant, intPlaces As Integer) As Variant
Round = Int(10 ^ intPlaces * varIn + 0.5) / 10 ^ intPlaces
End Function
Public Function ProRateMonth(dtMStart As Date, dtEStart As Date, varEEnd
As Variant) As Double
Dim dblDaysInMonth As Double
Dim dtMEnd As Date
Dim dtEEnd As Date
'Return the number of days in a month overlapping a date range divided
by the number of days in the month
'Assumes dtEStart <= varEEnd and dtMStart is the date of the first day
of the month
ProRateMonth = 0
dtMEnd = DateSerial(Year(dtMStart), Month(dtMStart) + 1, 0)
dblDaysInMonth = DateDiff("d", dtMStart, dtMEnd) + 1
If IsNull(varEEnd) Then
If dtEStart > dtMEnd Then Exit Function
If dtEStart < dtMStart Then
ProRateMonth = 1
Else
ProRateMonth = (DateDiff("d", dtEStart, dtMEnd) + 1) / dblDaysInMonth
End If
Exit Function
End If
If dtMEnd <= dtEStart Then
If dtMEnd = dtEStart Then ProRateMonth = 1# / dblDaysInMonth
Exit Function
End If
If varEEnd <= dtMStart Then
If varEEnd = dtMStart Then ProRateMonth = 1# / dblDaysInMonth
Exit Function
End If
If dtMStart <= dtEStart And dtEStart <= dtMEnd And dtMEnd <= varEEnd Then
ProRateMonth = (DateDiff("d", dtEStart, dtMEnd) + 1) / dblDaysInMonth
Exit Function
End If
If dtMStart <= dtEStart And dtEStart <= varEEnd And dtEEnd <= dtMEnd Then
ProRateMonth = (DateDiff("d", dtEStart, varEEnd) + 1) / dblDaysInMonth
Exit Function
End If
If dtEStart <= dtMStart And dtMStart <= dtMEnd And dtMEnd <= varEEnd Then
ProRateMonth = (DateDiff("d", dtMStart, dtMEnd) + 1) / dblDaysInMonth
Exit Function
End If
If dtEStart <= dtMStart And dtMStart <= varEEnd And varEEnd <= dtMEnd Then
ProRateMonth = (DateDiff("d", dtMStart, varEEnd) + 1) / dblDaysInMonth
End If
End Function
SELECT Sum(Abs(ProRateMonth(#Feb 1, 2005#,[Started Employment],[Ended
Employment])>0.99)) AS Report1Days FROM tblEmpl;
produced:
Report1Days
2
It counts the employee if the employee worked all the days of the month.
I'm not sure that's what you want since you have [Report 1].June = 3.
To have an employee count if they work any days in the month, replace
0.99 with 0.
SELECT Round(Sum(ProRateMonth(#Feb 1, 2005#, [Started Employment],
[Ended Employment])), 1) AS Report2Days FROM tblEmpl;
produced:
Report2Days
2.3
A crosstab query is probably required to get the months going across.
I'll see what I can come up with. Are the reports always for Jan to
Dec? My field format for [Started Employment] was mmm d", "yyyy. Also,
be sure to test the function ProRateMonth before use.
James A. Fortune