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, 8 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
-
W11 24H2 – Susan Bradley
by
G Pickerell
2 hours, 7 minutes ago -
7 tips to get the most out of Windows 11
by
Alex5723
4 hours, 15 minutes ago -
Using Office apps with non-Microsoft cloud services
by
Peter Deegan
44 minutes ago -
I installed Windows 11 24H2
by
Will Fastie
2 hours, 18 minutes ago -
NotifyIcons — Put that System tray to work!
by
Deanna McElveen
3 minutes ago -
Decisions to be made before moving to Windows 11
by
Susan Bradley
53 minutes ago -
Port of Seattle says ransomware breach impacts 90,000 people
by
Nibbled To Death By Ducks
8 hours, 18 minutes ago -
Looking for personal finance software with budgeting capabilities
by
cellsee6
9 hours, 40 minutes ago -
ATT/Yahoo Secure Mail Key
by
Lil88reb
20 hours, 59 minutes ago -
Devices with apps using sprotect.sys driver might stop responding
by
Alex5723
1 day, 1 hour ago -
Neowin – 20 times computers embarrassed themselves with public BSODs and goofups
by
EP
1 day, 9 hours ago -
Slow Down in Windows 10 performance after March 2025 updates ??
by
arbrich
12 hours, 11 minutes ago -
Mail from certain domains not delivered to my outlook.com address
by
pumphouse
18 hours, 19 minutes ago -
Is data that is in OneDrive also taking up space on my computer?
by
WShollis1818
1 day, 4 hours ago -
Nvidia just fixed an AMD Linux bug
by
Alex5723
2 days, 20 hours ago -
50 years and counting
by
Susan Bradley
11 hours, 18 minutes ago -
Fix Bluetooth Device Failed to Delete in Windows Settings
by
Drcard:))
2 days, 3 hours ago -
Licensing and pricing updates for on-premises server products coming July 2025
by
Alex5723
3 days, 7 hours ago -
Edge : Deprecating window.external.getHostEnvironmentValue()
by
Alex5723
3 days, 7 hours ago -
Rethinking Extension Data Consent: Clarity, Consistency, and Control
by
Alex5723
3 days, 7 hours ago -
OneNote and MS Word 365
by
CWBillow
3 days, 9 hours ago -
Ultimate Mac Buyers Guide 2025: Which Mac is Right For You?
by
Alex5723
3 days, 9 hours ago -
Intel Unison support ends on Windows 11 in June
by
Alex5723
3 days, 10 hours ago -
April 2025 — still issues with AMD + 24H2
by
Kevin Jones
1 day, 1 hour ago -
Windows 11 Insider Preview build 26200.5518 released to DEV
by
joep517
3 days, 21 hours ago -
Windows 11 Insider Preview build 26120.3671 (24H2) released to BETA
by
joep517
3 days, 21 hours ago -
Forcing(or trying to) save Local Documents to OneDrive
by
PateWilliam
4 days, 6 hours ago -
Hotpatch for Windows client now available (Enterprise)
by
Alex5723
3 days, 18 hours ago -
MS-DEFCON 2: Seven months and counting
by
Susan Bradley
2 days, 19 hours ago -
My 3 monitors go black & then the Taskbar is moved to center monitor
by
saturn2233
4 days, 15 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.