473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Easy sequential record numbers in Access queries

This is my modification on an original solution posted by Trevor Best
(tr****@microprism.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)+ZeroCounter()
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 17757
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******@hotmail.com> wrote in message
news:4b**************************@posting.google.c om...
This is my modification on an original solution posted by Trevor Best
(tr****@microprism.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)+ZeroCounter()
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
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...
3
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...
5
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...
4
by: James | last post by:
Hello there, Does anyone know how to create a sequential record number field in a query?? Thanks, James
14
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. ...
14
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...
2
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...
4
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...
9
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.