Advertisement

02.01.2003 at 08:43AM PST, ID: 20492559
[x]
Attachment Details

Display record/row number in a query

Asked by shanesuebsahakarn in Microsoft Access Database

Tags: , , , ,

This has been asked a few times and I've seen no generic solutions so here is a function to retrieve and display the row number in a query:

Function GetRowNum(strQueryName As String, strIDField As String, varID As Variant) As Long
Dim rst As DAO.Recordset

Set rst = CurrentDb.OpenRecordset("SELECT [" & strIDField & "]" & " FROM " & strQueryName, dbOpenSnapshot)
Select Case rst(strIDField).Type
    Case dbBigInt, dbBinary, dbBoolean, dbByte, dbCurrency, dbDecimal, dbDouble, dbFloat, dbInteger, dbLong, dbSingle, dbVarBinary:
        rst.FindFirst "[" & strIDField & "]=" & varID
    Case dbChar, dbText, dbMemo:
        rst.FindFirst "[" & strIDField & "]='" & varID & "'"
    Case dbDate, dbTime:
        rst.FindFirst "[" & strIDField & "]=#" & Format(varID, "mm/dd/yyyy hh:nn:ss") & "#"
End Select

GetRowNum = rst.AbsolutePosition + 1

rst.Close
Set rst = Nothing
End Function

If anyone wishes to use it, paste the above code into a module (you will also require a reference to the DAO library if you are using A2K or later - open a code module and click Tools->References and make sure DAO is ticked), and put a field like this in your query:
GetRowNum("MyQuery","MyIDField",[MyIDField])

Where MyQuery is the name of the query you are calling the function from and MyIDField is the name of the primary key field.

It will work correctly regardless of any sorting performed in the query, but will only work in saved querydefs (ie you can't use it for example, in the SQL of a combo box). If the query is manually sorted or filtered by clicking on the Sort/Filter icons or menu options, the function will return the wrong row numbers.Start Free Trial
 
 
[+][-]02.08.2003 at 09:00PM PST, ID: 7911373

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Access Database
Tags: number, row, query, display, record
Sign Up Now!
Solution Provided By: Computer101
Participating Experts: 2
Solution Grade: A
 
 
[+][-]03.03.2004 at 03:53PM PST, ID: 10509849

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32