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 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
-
iOS 26,, MacOS 26 : Create your own AI chatbot
by
Alex5723
2 hours, 44 minutes ago -
New PC transfer program recommendations?
by
DaveBoston
1 hour, 45 minutes ago -
Windows 11 Insider Preview Build 22631.5545 (23H2) released to Release Preview
by
joep517
6 hours, 49 minutes ago -
Windows 10 Build 19045.6029 (22H2) to Release Preview Channel
by
joep517
6 hours, 50 minutes ago -
Best tools for upgrading a Windows 10 to an 11
by
Susan Bradley
2 hours, 8 minutes ago -
The end of Windows 10 is approaching, consider Linux and LibreOffice
by
Alex5723
1 hour, 6 minutes ago -
Extended Windows Built-in Disk Cleanup Utility
by
bbearren
4 hours, 59 minutes ago -
Win 11 24H2 June 2025 Update breaks WIFI
by
dportenlanger
1 day, 1 hour ago -
Update from WinPro 10 v. 1511 on T460p?
by
CatoRenasci
33 minutes ago -
System Restore and Updates Paused
by
veteran
1 day, 4 hours ago -
Windows 10/11 clock app
by
Kathy Stevens
15 hours, 25 minutes ago -
Turn off right-click draw
by
Charles Billow
1 day, 7 hours ago -
Introducing ChromeOS M137 to The Stable Channel
by
Alex5723
1 day, 11 hours ago -
Brian Wilson (The Beach Boys) R.I.P
by
Alex5723
4 hours, 58 minutes ago -
Master patch listing for June 10, 2025
by
Susan Bradley
1 day, 12 hours ago -
Suggestions for New All in One Printer and a Photo Printer Windows 10
by
Win7and10
15 hours, 34 minutes ago -
Purchasing New Printer. Uninstall old Printer Software First?
by
Win7and10
1 day, 18 hours ago -
KB5060842 Issue (Minor)
by
AC641
6 hours, 48 minutes ago -
EchoLeak : Zero Click M365 Copilot leak sensitive information
by
Alex5723
2 days, 1 hour ago -
24H2 may not be offered June updates
by
Susan Bradley
18 hours, 25 minutes ago -
Acronis : Tracking Chaos RAT’s evolution (Windows, Linux)
by
Alex5723
2 days, 14 hours ago -
June 2025 updates are out
by
Susan Bradley
1 hour, 4 minutes ago -
Mozilla shutting Deep Fake Detector
by
Alex5723
3 days, 5 hours ago -
Windows-Maintenance-Tool (.bat)
by
Alex5723
2 days, 14 hours ago -
Windows 11 Insider Preview build 26200.5641 released to DEV
by
joep517
3 days, 7 hours ago -
Windows 11 Insider Preview build 26120.4250 (24H2) released to BETA
by
joep517
3 days, 7 hours ago -
Install Office 365 Outlook classic on new Win11 machine
by
WSrcull999
3 days, 7 hours ago -
win 10 to win 11 with cpu/mb replacement
by
aquatarkus
2 days, 23 hours ago -
re-install Windows Security
by
CWBillow
3 days, 10 hours ago -
WWDC 2025 Recap: All of Apple’s NEW Features in 10 Minutes!
by
Alex5723
3 days, 14 hours ago
Recent blog posts
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.