isladogs
MVP / VIP
- Local time
- Today, 02:35
- Joined
- Jan 14, 2017
- Messages
- 18,538
Its easy to create a rank order in Access reports but not so easy in queries
If you Google you will find several methods of doing so, but this is the easiest method I'm aware of
Attached is a simple example database I created in answer to a question on another forum.
It shows how to set a rank order in Access queries using the Serialize function below:
For example, in this query, the Serialize function is used to create a TableID field which serves as the row number:
The example db uses student assessment marks for a fictitious school
It has 2 tables - tblAssessmentMarks/tblAssessmentTypes
Various queries including three with rank order fields
a) qryY7ScAss1MarkRANK - puts Y7 Science students in rank order by mark (descending order) - as several students have the same mark, the rank values can be repeated
b) qryY7ScAss1PupilRANK - same data but this time ranked in descending mark order by PupilID - so no repeated values for rank
c) qryY7HiAvgAssMarksRANK - Y7 History average marks by assessment in rank order
Hope this is enough to show how to use the function
If you Google you will find several methods of doing so, but this is the easiest method I'm aware of
Attached is a simple example database I created in answer to a question on another forum.
It shows how to set a rank order in Access queries using the Serialize function below:
Code:
Public Function Serialize(qryname As String, KeyName As String, keyValue) As Long
On Error GoTo Err_Handler
'used to create rank order for records in a query
'add as query field
'Example Serialize("qry1","field1",[field1])
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset(qryname, dbOpenDynaset, dbReadOnly)
rst.FindFirst Application.BuildCriteria(KeyName, rst.Fields(KeyName).type, keyValue)
Serialize = Nz(rst.AbsolutePosition, -1) + 1
rst.Close
Set rst = Nothing
Exit_Handler:
Exit Function
Err_Handler:
MsgBox "Error " & Err.Number & " in Serialize procedure: " & Err.Description
GoTo Exit_Handler
End Function
For example, in this query, the Serialize function is used to create a TableID field which serves as the row number:
Code:
SELECT Serialize("qryJSONFileTables","TableName",[TableName]) AS [TableID], MSysObjects.Name AS TableName, qryJSONFileTableNames.FileID, qryJSONFileTableNames.FileNameFROM qryJSONFileTableNames INNER JOIN MSysObjects ON qryJSONFileTableNames.TableName = MSysObjects.Name
WHERE (((MSysObjects.Type)=1) AND ((MSysObjects.Flags)=0))
ORDER BY MSysObjects.Name;
The example db uses student assessment marks for a fictitious school
It has 2 tables - tblAssessmentMarks/tblAssessmentTypes
Various queries including three with rank order fields
a) qryY7ScAss1MarkRANK - puts Y7 Science students in rank order by mark (descending order) - as several students have the same mark, the rank values can be repeated
b) qryY7ScAss1PupilRANK - same data but this time ranked in descending mark order by PupilID - so no repeated values for rank
c) qryY7HiAvgAssMarksRANK - Y7 History average marks by assessment in rank order
Hope this is enough to show how to use the function
Attachments
Last edited: