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, 10 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 LoungerWSHansV
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
-
Cyberattack on Washington Post Strikes Journalists’ Microsoft Email Accounts
by
Alex5723
56 minutes ago -
June KB5060842 update broke DHCP server service
by
Alex5723
1 hour, 5 minutes ago -
AMD Ryzen™ Chipset Driver Release Notes 7.06.02.123
by
Alex5723
1 hour, 11 minutes ago -
Excessive security alerts
by
WSSebastian42
1 hour, 4 minutes ago -
* CrystalDiskMark may shorten SSD/USB Memory life
by
Alex5723
10 hours, 39 minutes ago -
Ben’s excellent adventure with Linux
by
Ben Myers
4 minutes ago -
Seconds are back in Windows 10!
by
Susan Bradley
30 minutes ago -
WebBrowserPassView — Take inventory of your stored passwords
by
Deanna McElveen
7 hours, 42 minutes ago -
OS news from WWDC 2025
by
Will Fastie
11 hours, 53 minutes ago -
Need help with graphics…
by
WSBatBytes
3 hours, 22 minutes ago -
AMD : Out of Bounds (OOB) read vulnerability in TPM 2.0 CVE-2025-2884
by
Alex5723
1 day, 2 hours ago -
Totally remove or disable BitLocker
by
CWBillow
1 hour, 15 minutes ago -
Windows 10 gets 6 years of ESU?
by
n0ads
4 hours, 30 minutes ago -
Apple, Google stores still offer China-based VPNs, report says
by
Nibbled To Death By Ducks
1 day, 12 hours ago -
Search Forums only bring up my posts?
by
Deo
1 day, 13 hours ago -
Windows Spotlight broken on Enterprise and Pro for Workstations?
by
steeviebops
2 days ago -
Denmark wants to dump Microsoft for Linux + LibreOffice
by
Alex5723
1 day, 17 hours ago -
How to get Microsoft Defender to honor Group Policy Setting
by
Ralph
2 days, 1 hour ago -
Apple : Paragon’s iOS Mercenary Spyware Finds Journalists Target
by
Alex5723
2 days, 11 hours ago -
Music : The Rose Room – It’s Been A Long, Long Time album
by
Alex5723
2 days, 12 hours ago -
Disengage Bitlocker
by
CWBillow
2 days, 2 hours ago -
Mac Mini M2 Service Program for No Power Issue
by
Alex5723
2 days, 14 hours ago -
New Win 11 Pro Geekom Setup questions
by
Deo
1 day, 13 hours ago -
Windows 11 Insider Preview build 26200.5651 released to DEV
by
joep517
2 days, 21 hours ago -
Windows 11 Insider Preview build 26120.4441 (24H2) released to BETA
by
joep517
2 days, 21 hours ago -
iOS 26,, MacOS 26 : Create your own AI chatbot
by
Alex5723
3 days, 1 hour ago -
New PC transfer program recommendations?
by
DaveBoston
1 day, 6 hours ago -
Windows 11 Insider Preview Build 22631.5545 (23H2) released to Release Preview
by
joep517
3 days, 5 hours ago -
Windows 10 Build 19045.6029 (22H2) to Release Preview Channel
by
joep517
3 days, 5 hours ago -
Best tools for upgrading a Windows 10 to an 11
by
Susan Bradley
2 days, 18 hours ago
Recent blog posts
- Ben’s excellent adventure with Linux
- Seconds are back in Windows 10!
- WebBrowserPassView — Take inventory of your stored passwords
- OS news from WWDC 2025
- Best tools for upgrading a Windows 10 to an 11
- Master patch listing for June 10, 2025
- 24H2 may not be offered June updates
- June 2025 updates are out
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.