473,549 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Easy sequential record numbers in Access queries

This is my modification on an original solution posted by Trevor Best
(tr****@micropr ism.com) back in 1996.

Insert the following code in a Module in your Access data base.
Option Compare Database
Option Explicit
Dim mlngCounter As Long

Function ZeroCounter()
mlngCounter = 0
ZeroCounter = 0
End Function

Function GetCounter(pvar As Variant)
mlngCounter = mlngCounter + 1
GetCounter = mlngCounter
End Function

Insert a column in your query and insert the following code in the
Field box.
Expr1: GetCounter(any other field name in the
query)+ZeroCoun ter()
For example Expr1: GetCounter([tblData_Entry]![txtPayment_Year])
+
ZeroCounter()

This will start the record numbers at 1 each time you run the query.
If you want the record numbers to keep incrementing just remove the
+ZeroCounter() from the expression. As in multiple appends to a
table.

Enjoy!
Nov 12 '05 #1
1 17781
That solution is fine for a specific situation but if the query is used
for the recordsource behind a Form or Report, or viewed directly in a
datasheet view, then the function will fail as the expression will be
continually evaluated as the user scrolls through the recordset.
Have a look here:
http://www.lebans.com/rownumber.htm
Rownumber.zip is a database containing functions for the automatic row
numbering of Forms, SubForms and Queries.

Updated Oct. 13 by Allen Browne. Includes error handling and cleaned
code.
--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
"systems analyst" <a7******@hotma il.com> wrote in message
news:4b******** *************** ***@posting.goo gle.com...
This is my modification on an original solution posted by Trevor Best
(tr****@micropr ism.com) back in 1996.

Insert the following code in a Module in your Access data base.
Option Compare Database
Option Explicit
Dim mlngCounter As Long

Function ZeroCounter()
mlngCounter = 0
ZeroCounter = 0
End Function

Function GetCounter(pvar As Variant)
mlngCounter = mlngCounter + 1
GetCounter = mlngCounter
End Function

Insert a column in your query and insert the following code in the
Field box.
Expr1: GetCounter(any other field name in the
query)+ZeroCoun ter()
For example Expr1: GetCounter([tblData_Entry]![txtPayment_Year])
+
ZeroCounter()

This will start the record numbers at 1 each time you run the query.
If you want the record numbers to keep incrementing just remove the
+ZeroCounter() from the expression. As in multiple appends to a
table.

Enjoy!


Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4320
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one child table (StudentProgress). The course progress records how a student progresses on a course. I have one course (History) and one student called...
3
14115
by: John Ortt | last post by:
> I have a table of dates in ascending order but with varying intervals. I > would like to create a query to pull out the date (in field 1) and then pull > the date from the subsequent record (and store it in field 2). > > I would like to run a query that returns all the data in the table plus the > record number, in a similar sense to the...
5
3200
by: Lapchien | last post by:
I have list of numbers in a table (originally from autonumber in a different database) from 1 to 1,000,000. The list is not in sequential order - there are loads of numbers missing. How can I identify what numbers are missing? Thanks, Lap (I'd like to then use this 'missing number list' to use for new records, instead of autonumber -...
4
8825
by: James | last post by:
Hello there, Does anyone know how to create a sequential record number field in a query?? Thanks, James
14
12008
by: amywolfie | last post by:
Hi All: I know this is simple, but I just can't seem to get there: I need to sort a table by a text field (txtDescription), then assign sequential numbers to the field SEQUENCE in table. Every time a new record is added, the all SEQUENCE #'s must be reset. The re-numbering will be triggered by an EXPORT button.
14
2446
by: google | last post by:
I am creating a new database for use within our company, that I'd like to make reasonably secure (short of a true server based solution). The back-end of a non-server based database seems to be the biggest security hole, due to the ease of opening, linking, and/or importing raw data directly from the back-end database. I've read information...
2
5096
by: John | last post by:
Hi I need to assign sequential invoice numbers to orders starting from the last highest number + 1. I have tried the following code; UPDATE Orders SET Orders. = DMax("","Orders")+1 WHERE Orders.) Is Null AND ... The problem is that all orders get the same number which is the last highest number + 1. Apparently the query does not...
4
2085
by: Bruce | last post by:
Surely someone has done this before, and I am guessing there is a simple solution that is eluding me. I have a simple report based on a recordset. For each record there is a field (RecNum) that is basically a sequential number from 1 to n. Occasionally there are skips between numbers. I would like to print 'record skipped' for each record...
9
2933
by: Axxe | last post by:
I have searched high and low for cogent, well-explained coding to complete a project on which I have spent six months of work. I stumbled across something on this site that is close to what I seek, but, as is rather typical, I could understand neither the question nor the answer. I am hopeful that someone in this forum can provide me...
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7718
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7956
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7470
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6041
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.