I know this is an old question, but it was the best search, and I have not seen any other solutions on the Internet, so I hope this helps others.
My solution works for any dataset, whether it has a unique identifier or not.
Add the following VBA code to the module:
Public row as Variant Function RowNum(dummy) As Integer row = row + 1 RowNum = row End Function Function GetRowNum(dummy) As Integer GetRowNum = row End Function Function ResetRowNum() row = 0 End Function
Now here is a sample request:
SELECT Table1.Field1, Table1.Field2, RowNum([Field1]) AS RowId, "Row: "&GetRowNum([Field1]) AS RowText FROM Table1
You can add any "ORDER BY" or even "GROUP BY" if you wish. You can use any field that will be in the query output as input for RowNum and GetRowNum . It is important to note that use RowNum only the first time you want a line number, and use GetRowNum every time after. This is done so that one row does not increase the counter.
The last thing you need to do is create a macro that runs ResetRowNum and run it after each request that you use with this method, or if you are running a series of requests through a macro or VBA, be sure to run ResetRowNum after every request that uses these functions.
Also, avoid presenting a data table, since it seems to constantly recalculate formulas when scrolling, which leads to a constant increase in the number.
Aehetag
source share