Solved

Numbering rows sequentialy in a query

Posted on 2015-01-31
9
119 Views
Last Modified: 2015-01-31
I want the row number to shown in a column of a query. Starting at 1 and continuing until the end of the records
0
Comment
Question by:DatabaseDek
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 40581732
Unfortunately, this features is not built into Access.

However, if you want to create a custom function, you can get that in a query.

The function would be added to a new VBA module with this:

Function RowNumber(TableName As String, ID As Long) As Long
    Dim cnt As Long
    cnt = DCount("*", TableName, "ID <= " & ID)
    RowNumber = cnt
End Function

Open in new window


then your query would be include something like this:

SeqNum: RowNumber("tblTest",[ID])

Where tblTest is the name of the table and [ID] is the name of the primary key in that table.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40581758
Funny that you are asking this question now.  I just this morning answered a similar question, although in that case the user wanted to update a column with a sequential number.

Take a look at my response.  Keep in mind, you will have to reset the value each time you run the query.  Also, I only use this in a MakeTable, or Update query because as you scroll through the query, forwards and backwards, the sequential numbers will get recomputed.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40581850
Is there an autonumber field?

Is there some relationship between the rows' IDs returned by the query?
0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Comment

by:DatabaseDek
ID: 40581864
Thank you.

Would it be simpler to use a make table query and base the report on that?
Can a make table query generate an increment field.
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 40581867
yes, but you don't need a MakeTable...you can simply add a new field into your current table and set it as AutoNumber (then probably set it as primary key).
when you save, the field will be populated with values from 1 to the number of records, then increment as new records are added.

The only caveat is that the numbers are not guaranteed to be in exact sequence since it will skip numbers if someone started to create a record then cancels (escapes) leaving that ID unused.
0
 

Author Comment

by:DatabaseDek
ID: 40581893
As I say the query does not use all of the numbers from the table.

What I am trying to do is print labels. The labels have a product code. But if I print 50 pages of labels with 10 per page how would you find an individual label. If I could number the labels then I can also print a numbered reference to the labels that would be printed in order and would subsequently be easy to find by the incremental label number.
0
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Access MVP) earned 500 total points
ID: 40581947
In a Report, you can create a 'Running Sum" in a Text Box using a Control Source of
=1
Then (with this text box visible) ... 'records'  in the Detail Section will start at 1 and increment forward.

Simple
0
 

Author Closing Comment

by:DatabaseDek
ID: 40581985
That was so simple had to give the points to MX.

But my thanks to you all. I will try be more exact in future
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40582004
If you had told us in your original question that this was for a report, it would have saved a lot of time.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question