Discussion Locked

MS ACCESS -counting records

0 helpful
9 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am creating an application in MS ACCESS97. I have a table with employee start date and termination date.

The user will input two dates and I need to have the program calculate the average number of employees that existed for that period. So it has to parse all the dates and count how many employees existed on each date and then divide by the number of days.

Any Ideas??

9 replies

by peyison

This isn't the route you suggested, but it might work. This query will count the actual number of employess between the two dates.

StartDate and TermDate are the fields in the table.

Date1 and Date2 are the dates in the range.

This also assumes that if an employee is active, TermDate is null.

SELECT Sum(IIf([StartDate]<[Date2],IIf([TermDate]>[Date1] Or IsNull([TermDate]),1,0),0)) AS EmpCount
FROM tblEmps;

To do the average number as you described it, you'll probably have to do it in code. If you are interested in seeing how that might look, let me know.

Hope this helps.

by peyison

I don't think the answer below is going to return an accurate average, either. It will count the number of employees active within the date range, and divide that number by the number of days.

What you're looking for is to get the number of active employees for each day in the range, add them up, and divide that total by the number of days. I still think you'll have to use VBA code to do this. Again, let me know if you want more info on that.

by peyison

Here's a function that should work. You pass it the two dates in the range, and it will return the average employees in the range.

The call would look like:

Msgbox GetAvgEmp("1/1/01", "1/7/01")

Private Function GetAvgEmp(atDate1 As Date, atDate2 As Date) As Double
Dim db As Database
Dim rst As Recordset
Dim sSql As String
Dim tDateIndex As Date
Dim lEmpCount As Long

If atDate1 > atDate2 Then
MsgBox "The first date must be earlier than the second date.", vbExclamation
Exit Function
End If

Set db = CurrentDb

'--loop through the dates - inclusively
For tDateIndex = atDate1 To atDate2
sSql = "Select Count(EmpId) as NumEmps From tblEmps " & _
"Where StartDate <= #" & tDateIndex & "# And " & _
"(TermDate >= #" & tDateIndex & "# or TermDate Is null)"
Set rst = db.OpenRecordset(sSql)

If Not rst.EOF Then
If Not IsNull(rst!NumEmps) Then
'--accumulate the number of employees lEmpCount = lEmpCount + rst!NumEmps
End If
End If

rst.Close
Set rst = Nothing
Next

Set db = Nothing

'--divide the number of employees by the number of days in the date range
'--need to add 1 to thedate difference since we are including both days in the range
GetAvgEmp = lEmpCount / (atDate2 - atDate1 + 1)

End Function

by steved

It can be done in one query. I've created this one as a parameter query, so calling it would automatically cause Access to ask the user for start date and end date, but you could just as easily get these values from another source.

-------------------------------------
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
SELECT DateDiff("d",[Start Date],[End Date]) AS DaysInRange, Count(Employees.EmpID) AS EmployeesInRange, ([EmployeesInRange]/[DaysInRange]) AS Average
FROM Employees
WHERE (((Employees.StartDate)>=[Start Date] And (Employees.StartDate)<=[End Date]));
---------------------------------------

I hope this helps.

Steve Diamond

by steved

Sorry, I misread your criteria, so my WHERE clause is inadequate. Try this instead:
------------------------------------
PARAMETERS [Start Date] DateTime, [End Date] DateTime;
SELECT DateDiff("d",[Start Date],[End Date]) AS DaysInRange, Count([Employees].[EmpID]) AS EmployeesInRange, ([EmployeesInRange]/[DaysInRange]) AS Average
FROM Employees
WHERE [Employees].[StartDate]<=[End Date] And ([Employees].[TerminationDate]<=[Start Date] Or [Employees].[TerminationDate] Is Null);
---------------------------------------

by nicholasp

If you have VBA code that will do this or something close .Please let me know. A query will not do this accuratly. Thanks

This discussion is closed to new replies.