473,785 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ 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 51269
Gekko . via AccessMonster.c om 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.c om" <fo***@AccessMo nster.com> wrote in message news:<10******* *************** ********@Access Monster.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(lngFldV al As Long, strSource As String, strKey
As String) As Long
Dim strCrit As String
strCrit = "[" & strKey & "] < " & CStr(lngFldVal)
LineNum = Nz(DCount(strKe y, strSource, strCrit)) + 1
End Function

Then it can be called like:

SELECT LineNum([TableID],"tblSource","T ableID") 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.Table ID AS QID...). I also tried an
old query I had lying around as qryNew :-):

qryNew:
SELECT qryOrdinalIPs.I P AS IP1, qryOrdinalIPs.I D
FROM tblIPs INNER JOIN qryOrdinalIPs ON tblIPs.ID = qryOrdinalIPs.I D
WHERE (((qryOrdinalIP s.theOrdinal)=1 ))
GROUP BY qryOrdinalIPs.I P, qryOrdinalIPs.I D;

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
1540
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 this query ?
9
60102
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 when running the follwoing (in a C++ program using ADO): ...... cmdStr = "CREATE TABLE mytab " "( AutoNumber, " " TEXT(50), " "CONSTRAINT PRIMARY KEY ()); ";
2
4004
by: MLH | last post by:
Fields in MyTable: PostID PostDate RollQtyXfer RollDenomination RollCount37 RollCount23
2
5484
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.6 40 2.4 40 2.7
4
4602
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", "tbl_orders")+1. This problem is quite different and I am hoping someone out there can advice. I have a group of orders in a table. I tick a box to select which records I want to batch for that particular month. An update query then copies...
7
3781
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
3541
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 to handle it? Should I simply create a new text field ( and then use VB in a newrecord event that: Me.newID = DatePart("YY", Now) & "-" & Me.ID This works, but I want to see if anyone thinks there is a better way? Peter
1
2790
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 low for the code, it seems like an easy task but haven't solved the problem yet. Here is the current scenario: I import an text file of numbers, once imported, I need to assign a unique (autonumber) value to each number imported, starting with 1...
10
5073
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 ProjectID, which is the left-most field. Sorting by ProjectName means that the ProjectID field is not in numerical order. ProjectID 109156 105428 2812 117973 The boss wants to have an ordinal number field called something like 'ItemNumber'...
6
59185
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 query I am calling for different sets of records off of the client id (ie: id's 50000-55000 or 32000-37000). Each time I run the query I need it to number the rows 1, 2, 3, 4, 5, ...5,000. I hope this is understandable and someone can help. Thanks
0
9646
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9957
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7505
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5518
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.