473,466 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Create a query with autonumber

I want to create a query that the first field should be an autonumber. For example I want the name and last name from the Emp table, but I want that the first field is an autonumber that identifies the record so the results of the query should look something like

Field1 Name LastName
1 Gekko NoLast
2 Mary Jane
3 Jon Doe

*****************************************
* This message was posted via http://www.accessmonster.com
*
* Report spam or abuse by clicking the following URL:
* http://www.accessmonster.com/Uwe/Abu...9df4d473689eb7
*****************************************
Nov 13 '05 #1
3 51151
Gekko . via AccessMonster.com wrote:
I want to create a query that the first field should be an autonumber. For example I want the name and last name from the Emp table, but I want that the first field is an autonumber that identifies the record so the results of the query should look something like

Field1 Name LastName
1 Gekko NoLast
2 Mary Jane
3 Jon Doe


http://www.trevor.easynet.co.uk/AccF...es.htm#counter

--
Pretentious? Moi?
Nov 13 '05 #2
My way to do this is to put in the Table a numeric field with name
"DumNr" or so, with only "one" in it, I mean the number 1. DefaultValue
also put to 1.

If you create this field afterwards you could find this action query
useful:

update mytable set dumnr = 1

In any report where I want to number the rows I put a textfield with any
name and the property RunningSum set to 1 or 2.

1 will restart numbering again and again after every grouping.
2 will number all rows in the report without restarting.

This works in a report. I didn't find a way to do the same in a query

toi pretentious? et moi, alors?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #3
"Gekko . via AccessMonster.com" <fo***@AccessMonster.com> wrote in message news:<10******************************@AccessMonst er.com>...
I want to create a query that the first field should be an autonumber. For example I want the name and last name from the Emp table, but I want that the first field is an autonumber that identifies the record so the results of the query should look something like

Field1 Name LastName
1 Gekko NoLast
2 Mary Jane
3 Jon Doe

*****************************************
* This message was posted via http://www.accessmonster.com
*
* Report spam or abuse by clicking the following URL:
* http://www.accessmonster.com/Uwe/Abu...9df4d473689eb7
*****************************************


If you don't have a primary key or indexed field in a table you are
trying to query don't count on Access knowing even its own record
order. If you have a primary key you can do something like:

SELECT (SELECT Count(theField) + 1 FROM tblSource AS A WHERE A.TableID
< B.TableID) As RowNumber, TableID, theField
FROM tblSource AS B;

E.g.,
tblSource
TableID theField
1 Data1
3 Data12
5 Data14
19 Data27
20 Data28
22 Data3
25 Data32
26 Data33
29 Data6

produces:

RowNumber TableID theField
1 1 Data1
2 3 Data12
3 5 Data14
4 19 Data27
5 20 Data28
6 22 Data3
7 25 Data32
8 26 Data33
9 29 Data6

This idea can be put into a public function:

Public Function LineNum(lngFldVal As Long, strSource As String, strKey
As String) As Long
Dim strCrit As String
strCrit = "[" & strKey & "] < " & CStr(lngFldVal)
LineNum = Nz(DCount(strKey, strSource, strCrit)) + 1
End Function

Then it can be called like:

SELECT LineNum([TableID],"tblSource","TableID") AS RowNumber, TableID,
theField FROM tblSource;

Or

SELECT LineNum([QID], "qryNew", "QID") As RowNumber, * FROM qryNew;

Note: One way to get a unique QID is to use the key from a source
table (qryNew: SELECT tblSource.TableID AS QID...). I also tried an
old query I had lying around as qryNew :-):

qryNew:
SELECT qryOrdinalIPs.IP AS IP1, qryOrdinalIPs.ID
FROM tblIPs INNER JOIN qryOrdinalIPs ON tblIPs.ID = qryOrdinalIPs.ID
WHERE (((qryOrdinalIPs.theOrdinal)=1))
GROUP BY qryOrdinalIPs.IP, qryOrdinalIPs.ID;

SELECT LineNum([ID], "qryNew", "ID") As RowNumber, * FROM qryNew;

produced:

RowNumber IP1 ID
1 2.2.2.2 1
2 7.7.7.7 2

so getting a unique ID in a query is not usually too difficult,
thereby allowing this function to be used. Since the underlying PK is
uniquely indexed there are no tie values to consider. You can still
go back to writing a subquery if the function call slows the query too
much. The function is nice because you can usually add the row
numbers to an existing query without having to spend time determining
the SQL for the subquery.

James A. Fortune
Temporary email: jimfortune AT compumarc DOT com
Nov 13 '05 #4

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

Similar topics

1
by: Snuyt | last post by:
Hello, I have a insert query in an Access database: q="INSERT INTO tbl (f1, f2) VALUES ('1', '2')" the table has 3 fields: id: autonumber, f2: text, f3: text how do I retrieve the id from...
9
by: minjie | last post by:
I need to upgrade a MS Access database (Version 2002) with a script only, i.e., via SQL statement, not via Access GUI, and I'm having trouble defining an AutoNumber field. I got an exception error...
2
by: MLH | last post by:
Fields in MyTable: PostID PostDate RollQtyXfer RollDenomination RollCount37 RollCount23
2
by: mael.iosa | last post by:
Hi, I'm new to this group and fairly new to Access. I have a bunch of data, and after several other queries, I generate the following query which has two fields: Bin, Time 20 3.5 20 3.9 20...
4
by: jacc14 | last post by:
Hi This is the second query I have had with Dmax. The first one I resolved as it was in a form and by using DMAX everytime I click for new record it gave a new order number ie dmax("ordernumber",...
7
by: Jim | last post by:
How do I program visual basic to create a "Find Unmatched Query"? I have two tables: - TodaysImport - YesterdaysImport Both tables have the same 6 fields: - User
4
by: peterkennett | last post by:
Autonumber works great, but my boss needs the ID to be a number that begins with the 2 digit year. For example 08-465 and 08-466. I know this must be a common requirement, so what is the best way...
1
by: sanju4kk | last post by:
I am attempting to create a module that I can call to create a new field in my temp table that will be an autonumber, and then assigned that new field as the primary key. I have been search high and...
10
by: kujito | last post by:
Ok, here it goes. I got my queries straightened out and they return the data I want in the format I want(finally). The data are sorted alpha. descending by ProjectName. Each project has a unique...
6
by: Amy Badgett | last post by:
I need to create an Autonumber field in a query I am currently running. The query right now calls fields such as names, addresses, phone numbers and is distinct on the client id. Each time I run the...
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
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
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...
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.