Does anyone have a bit of code for a function that would create sequential numbers that could be inserted into a query. I have a query that I export to a file where I want to generate line numbers like 1, 2, 3, 4, 5, etc.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
line number in a query (A2000 SR-1)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » line number in a query (A2000 SR-1)
- This topic has 16 replies, 4 voices, and was last updated 20 years, 7 months ago.
Viewing 1 reply threadAuthorReplies-
WSHansV
AskWoody LoungerJuly 29, 2004 at 6:55 pm #857531Although there are solutions, they often work only in certain circumstances. In a report: you can put a text box in a section with Control Source set to =1 and Running Sum set to Over All. If exporting a report is a viable alternative, it is much easier than using cumbersome custom functions in a query.
-
Gwb
AskWoody Lounger -
WSHansV
AskWoody Lounger -
Gwb
AskWoody LoungerJuly 29, 2004 at 8:07 pm #857580 -
WSPatricia W
AskWoody LoungerJuly 29, 2004 at 8:34 pm #857585People here will go running and screaming from my non-classy way of solving this problem, but here’s what I would do in your case, given the difficulty:
After readying your table or query for export (sorting, etc.), I would export it to a new table that also happens to have an autonumber field — for example, name this the “SalesForExport” table, and then export from that.
thx
Pat -
WSPatricia W
AskWoody LoungerJuly 29, 2004 at 8:34 pm #857586People here will go running and screaming from my non-classy way of solving this problem, but here’s what I would do in your case, given the difficulty:
After readying your table or query for export (sorting, etc.), I would export it to a new table that also happens to have an autonumber field — for example, name this the “SalesForExport” table, and then export from that.
thx
Pat -
WSHansV
AskWoody LoungerJuly 29, 2004 at 8:35 pm #857591Try this:
Create a standard module by clicking New in the Modules section of the database window.
Copy the following code into the module:Public lngNum As Long
Public Function LineNum(AnyVal) As Long
lngNum = lngNum + 1
LineNum = lngNum
End FunctionOpen your query in design view.
Add a calculated column:LineNumber: LineNum([AnyField])
where AnyField is the name of an arbitrary field in the table.
As you will find out, the first time you run the query in a session, the line numbers will start at 1, but for each subsequent run, they will start at the previous highest line number + 1. If you want the line numbers to start at 1 again, you must reset the lngNum variable to 0 in code before opening the query.
-
WSjpgus
AskWoody LoungerAugust 13, 2004 at 5:22 pm #864236I have a similar need. I have a query that has InvoiceID and INVdetailnum I’d like to add a field that essential assings a line item number 1,2,3 etc to each Invdetail num so in the end you can see that invdetailnum 136 id item 3 of invoice 17. So. the querry should sort first on InvoiceID, then on INVdetailnum, and the Linenum fields starts at 1 and goes up by one until it sees that its hit a record with a new InvoiceID so it starts at 1 again and counts up by one…. Any hope for me.
-
WSHansV
AskWoody LoungerAugust 13, 2004 at 7:49 pm #864312You can do this in two steps:
1. Create a query that sorts on InvoiceID first and then on INVDetailNum. You can add other fields if you need them. Save this query as, say, qrySortedInvoices.
2. Create a new query based on qrySortedInvoices. Add the fields from the query (or *), plus a calculated column:
LineNum: Val(DCount("*","qrySortedInvoices","InvoiceID = " & [InvoiceID] & " AND INVDetailNum <= " & [INVDetailNum]))
Note: some browsers mess up the “less than or equal” in the above expression. The end should be
" AND INVDetailNum < = " & [INVDetailNum]))
but without a space between < and =
-
WSHansV
AskWoody LoungerAugust 13, 2004 at 7:49 pm #864313You can do this in two steps:
1. Create a query that sorts on InvoiceID first and then on INVDetailNum. You can add other fields if you need them. Save this query as, say, qrySortedInvoices.
2. Create a new query based on qrySortedInvoices. Add the fields from the query (or *), plus a calculated column:
LineNum: Val(DCount("*","qrySortedInvoices","InvoiceID = " & [InvoiceID] & " AND INVDetailNum <= " & [INVDetailNum]))
Note: some browsers mess up the “less than or equal” in the above expression. The end should be
" AND INVDetailNum < = " & [INVDetailNum]))
but without a space between < and =
-
WSjpgus
AskWoody LoungerAugust 13, 2004 at 5:22 pm #864237I have a similar need. I have a query that has InvoiceID and INVdetailnum I’d like to add a field that essential assings a line item number 1,2,3 etc to each Invdetail num so in the end you can see that invdetailnum 136 id item 3 of invoice 17. So. the querry should sort first on InvoiceID, then on INVdetailnum, and the Linenum fields starts at 1 and goes up by one until it sees that its hit a record with a new InvoiceID so it starts at 1 again and counts up by one…. Any hope for me.
-
WSHansV
AskWoody LoungerJuly 29, 2004 at 8:35 pm #857592Try this:
Create a standard module by clicking New in the Modules section of the database window.
Copy the following code into the module:Public lngNum As Long
Public Function LineNum(AnyVal) As Long
lngNum = lngNum + 1
LineNum = lngNum
End FunctionOpen your query in design view.
Add a calculated column:LineNumber: LineNum([AnyField])
where AnyField is the name of an arbitrary field in the table.
As you will find out, the first time you run the query in a session, the line numbers will start at 1, but for each subsequent run, they will start at the previous highest line number + 1. If you want the line numbers to start at 1 again, you must reset the lngNum variable to 0 in code before opening the query.
-
-
Gwb
AskWoody LoungerJuly 29, 2004 at 8:07 pm #857581
-
-
WSHansV
AskWoody Lounger
-
-
Gwb
AskWoody Lounger
-
-
WSHansV
AskWoody LoungerJuly 29, 2004 at 6:55 pm #857532Although there are solutions, they often work only in certain circumstances. In a report: you can put a text box in a section with Control Source set to =1 and Running Sum set to Over All. If exporting a report is a viable alternative, it is much easier than using cumbersome custom functions in a query.
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Forcing(or trying to) save Local Documents to OneDrive
by
PateWilliam
2 hours, 55 minutes ago -
Hotpatch for Windows client now available (Enterprise)
by
Alex5723
3 hours, 34 minutes ago -
MS-DEFCON 2: Seven months and counting
by
Susan Bradley
28 minutes ago -
My 3 monitors go black & then the Taskbar is moved to center monitor
by
saturn2233
13 hours, 50 minutes ago -
Apple backports fixes
by
Susan Bradley
4 hours, 21 minutes ago -
Win 11 24H2 will not install
by
Michael1950
14 hours, 4 minutes ago -
Advice to convert MBR to GPT and install Windows 11 Pro on unsupported PC
by
Andy M
13 hours, 35 minutes ago -
Photos from iPhone to Win 10 duplicating/reformatting to .mov
by
J9438
15 hours, 42 minutes ago -
Thunderbird in trouble. Here comes Thundermail
by
Alex5723
13 hours, 51 minutes ago -
Get back ” Open With” in context menus
by
CWBillow
1 day, 2 hours ago -
Many AMD Ryzen 9800X3D on ASRock have died
by
Alex5723
1 day, 6 hours ago -
simple general stupid question
by
WSaltamirano
1 day ago -
April 2025 Office non-Security updates
by
PKCano
1 day, 17 hours ago -
Microsoft wants to hear from you
by
Will Fastie
17 hours, 38 minutes ago -
Windows 11 Insider Preview Build 22635.5160 (23H2) released to BETA
by
joep517
1 day, 21 hours ago -
Europe Seeks Alternatives to U.S. Cloud Providers
by
Alex5723
2 days, 2 hours ago -
Test post
by
Susan Bradley
2 days, 4 hours ago -
Used Systems to delete Temp files Gone WRONG what does this mean?
by
Deo
2 days, 6 hours ago -
SSD shuts down on its own
by
CWBillow
1 day, 21 hours ago -
OneDrive File Sharing Changes
by
David Clark
2 days, 14 hours ago -
OneDrive File Sharing Changes
by
David Clark
2 days, 16 hours ago -
Win 10 Pro 22H2 to Win 11 Pro 23H2 Conversion Guide
by
doneager
1 day, 16 hours ago -
Today is world backup day
by
Alex5723
2 days, 8 hours ago -
Windows .exe on Mint
by
Slowpoke47
13 hours, 17 minutes ago -
Reviewing your licensing options
by
Susan Bradley
2 hours, 40 minutes ago -
Apple has been analyzing your photos since September 2024
by
B. Livingston
1 day, 13 hours ago -
What Windows 11 24H2 offers beyond bugs
by
Lance Whitney
1 day, 9 hours ago -
Making sense of Settings in Windows 11
by
Simon Bisson
1 day, 10 hours ago -
Windows 11 pro fails to log in after upgrading Win 10 pro to Win 11 pro 24h2
by
ben_sitaud
2 days, 14 hours ago -
23H2 / 24H2 / Local v. Microsoft Account.
by
CWBillow
1 minute ago
Recent blog posts
- MS-DEFCON 2: Seven months and counting
- Apple backports fixes
- April 2025 Office non-Security updates
- Microsoft wants to hear from you
- Reviewing your licensing options
- Apple has been analyzing your photos since September 2024
- What Windows 11 24H2 offers beyond bugs
- Making sense of Settings in Windows 11
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.