Results 1 to 10 of 10
  1. #1
    HansBades is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Dec 2021
    Posts
    85

    Query Row Number

    How can I get a query to return a row number for each record returned in numerical order? For instance if I query Select SKUID, SKUNAME from SKU WHERE SKU.DEPTIDFK =4 how can I get a third field with the row count? Thanks.



    SKUID SKUNAME Row
    5 Hammer 1
    7 Pliers 2
    12 Aluminum Shield 3

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  3. #3
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    It's an oft asked question and usually involves DCount function in the counting field. A google search will likely produce some 100 million plus results.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    Here's another approach not involving subqueries
    Rank Order In Queries (isladogs.co.uk)
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  7. #7
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    Just a thought: without order by, the rownumber makes no sense?

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    this method does not rely on an order by and as a function will be much faster than dcount or a subquery, particularly noticeable for large datasets

    Put this function in a module

    Code:
    Function RowNumber(Optional r As Variant = -1) As Variant
    Static X As Long
    
    
        If r = -1 Then
            X = 0
        Else
            X = X + 1
        End If
        
        RowNumber = X
        
    End Function
    and call it with your query - something like
    Code:
    SELECT *, RowNumber([PK]) AS Row
    FROM mytable
    WHERE (((RowNumber())=False))
    Note the Where clause - this is required to to reset the count. Better to use the PK, but any field that is unique will do. If the field is not unique, the count will not increment correctly

    Other points to note:
    this works fine when viewed in a form or report, but view the query or form datasheet and the rownumbers will change if a row is selected
    One of the potential benefits is if you reorder or filter the query, the rownumbers remain static - i.e. the first row is always 1, the second 2 etc
    and as stated above, an order by is not required

  9. #9
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,044
    If you want a row number viewed in a report, you don't need a function: just create an unbound control, set the control source to = 1 and set the running sum property to 'over all' or 'over group'

  10. #10
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Office 365
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,977
    @NoellaG
    Agreed but the OP clearly stated query row number
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 01-05-2018, 04:35 PM
  2. Replies: 2
    Last Post: 08-04-2017, 12:37 PM
  3. Replies: 2
    Last Post: 10-12-2016, 05:48 PM
  4. Sequence number in query use a starting number
    By fgwapo in forum Programming
    Replies: 1
    Last Post: 09-21-2014, 12:44 AM
  5. Replies: 6
    Last Post: 07-25-2011, 01:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums