I currently have vba code that ranks employees based on their average
job time ordered by their region, zone, and job code. I currently
have vba code that will cycle through a query and ranks each employee
based on their region, zone, job code and avg job time. (See code
below). My problem is that I do not know how to rank the ties. Right
now if two people have the same avg time one will be ranked 3rd and
the other ranked 4th. I would like to have them both be ranked 3rd.
If anyone has any ideas please let me know.
Thanks
Public Function CalculateMonthlyTotalsbyYard(strPeriod As String)
'************************************************* ******************************************
' strPeriod has form YYYYMM
'
' To USE this code, you must click 'Tools/References' in the menu bar
' - Then select the "Microsoft DAO 3.6 Object Library" reference.
'************************************************* ******************************************
Dim rstRead As DAO.Recordset
Dim rstWriteEmp As DAO.Recordset
Dim rstWriteTot As DAO.Recordset
Dim strSQL As String
Dim iRank As Integer
Dim iRegionZoneCount As Integer
Dim iPrevZone As Integer
Dim iPrevRegion As Integer
Dim iPrevJobCode As Integer
Dim sPrevYrMo As String
' Set String to hold select statement which returns Employees in
Ranking Order
strSQL = "SELECT * FROM [qryEmployeeSums] " & _
" WHERE [YrMo] = '" & strPeriod & "' " & _
"ORDER BY [Region], [Zone], [JobCode], [AvgTime] ASC;"
' Initialize the Recordset with the query data
Set rstRead = CurrentDb.openrecordset(strSQL)
' Make sure that some rows were returned
If rstRead.RecordCount > 0 Then
' Initialize the Recordset for writing Employee Data
Set rstWriteEmp = CurrentDb.openrecordset("tblEmployeeSums")
' Initialize the Recordset for writing Region,Zone,JobCode
Employee Totals
Set rstWriteTot =
CurrentDb.openrecordset("tblRegionZoneJobTotals")
' Initialize variables
iPrevRegion = 0
iPrevZone = 0
iPrevJobCode = 0
' Move to the first record and
rstRead.MoveFirst
' Spin through each row returned, accumulating rankings
While Not rstRead.EOF
If rstRead![Region] = iPrevRegion And _
rstRead![Zone] = iPrevZone And _
rstRead![JobCode] = iPrevJobCode Then
' Increment Ranking for Current Employee
iRank = iRank + 1
' Increment total counters for Current Region, Zone
and Job Code
iRegionZoneCount = iRegionZoneCount + 1
Else
' Region, Zone or JobCode has changed, write Totals
record
' Note: Do not write for very first record
If iPrevRegion <> 0 Then
With rstWriteTot
.AddNew
![Region] = iPrevRegion
![Zone] = iPrevZone
![JobCode] = iPrevJobCode
![YrMo] = sPrevYrMo
![EmpCount] = iRegionZoneCount
.Update
End With
End If
' Set Previous variables on current record
iPrevRegion = rstRead![Region]
iPrevZone = rstRead![Zone]
iPrevJobCode = rstRead![JobCode]
sPrevYrMo = rstRead![YrMo]
iRank = 1
iRegionZoneCount = 1
End If
' Write Current Employee Total Record to Table
With rstWriteEmp
.AddNew
![Region] = rstRead![Region]
![Zone] = rstRead![Zone]
![JobCode] = rstRead![JobCode]
![YrMo] = rstRead![YrMo]
![EmployeeID] = rstRead![EmployeeID]
![JobsComp] = rstRead![JobsComp]
![TotalTime] = rstRead![TotalTime]
![AvgTime] = rstRead![AvgTime]
![Ranking] = iRank
.Update
End With
' Get the next record
rstRead.MoveNext
Wend
' Write the Final totals record
With rstWriteTot
.AddNew
![Region] = iPrevRegion
![Zone] = iPrevZone
![JobCode] = iPrevJobCode
![YrMo] = sPrevYrMo
![EmpCount] = iRegionZoneCount
.Update
End With
End If
End Function