473,498 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Value of an AutoNumber Field?

Hi.

I'm inserting a record into a DB, how do i get the value back of an
AutoNumber field so i can use it in subsequent SQL statements?

At the moment I have code vaguely like this:

Dim selectCMD As OleDbCommand = New OleDbCommand("INSERT into AuditItems (
ParentAuditID, section, score, comments) values('1')", conn)

MsgBox(selectCMD.ExecuteNonQuery())

I presume this is quite easy, maybe i'm doing it the wrong way...

Thanks for any help in advance!

Rgds,
Dan

Nov 20 '05 #1
4 1410
i cringe hearing "autonumber"...you must be using access! i don't think you
can afaik.

in msde (which is FAR better than access) you can just append this statement
"select @@identity newId" to the tail end of an insert statment and get the
value from the newId column of the returned recordset.

hth,

steve
"Dan Keeley" <ma********@hotmail.com> wrote in message
news:i1*****************@newsfep4-winn.server.ntli.net...
Hi.

I'm inserting a record into a DB, how do i get the value back of an
AutoNumber field so i can use it in subsequent SQL statements?

At the moment I have code vaguely like this:

Dim selectCMD As OleDbCommand = New OleDbCommand("INSERT into AuditItems (
ParentAuditID, section, score, comments) values('1')", conn)

MsgBox(selectCMD.ExecuteNonQuery())

I presume this is quite easy, maybe i'm doing it the wrong way...

Thanks for any help in advance!

Rgds,
Dan

Nov 20 '05 #2
"Dan Keeley" <ma********@hotmail.com> schrieb
Hi.

I'm inserting a record into a DB, how do i get the value back of
an AutoNumber field so i can use it in subsequent SQL statements?

At the moment I have code vaguely like this:

Dim selectCMD As OleDbCommand = New OleDbCommand("INSERT into
AuditItems ( ParentAuditID, section, score, comments) values('1')",
conn)

MsgBox(selectCMD.ExecuteNonQuery())

I presume this is quite easy, maybe i'm doing it the wrong way...


It's probably already been discussed at
microsoft.public.dotnet.framework.adonet. Have a look there.
--
Armin

Nov 20 '05 #3
Excellent thanks.

The answer truns out to be:

selectCMD.CommandText = "Select @@identity"

MsgBox(selectCMD.ExecuteScalar())

I will certainly trawl the microsoft newsgroup a bit now.

Thanks again!
Dan

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Om**************@TK2MSFTNGP12.phx.gbl...
"Dan Keeley" <ma********@hotmail.com> schrieb
Hi.

I'm inserting a record into a DB, how do i get the value back of
an AutoNumber field so i can use it in subsequent SQL statements?

At the moment I have code vaguely like this:

Dim selectCMD As OleDbCommand = New OleDbCommand("INSERT into
AuditItems ( ParentAuditID, section, score, comments) values('1')",
conn)

MsgBox(selectCMD.ExecuteNonQuery())

I presume this is quite easy, maybe i'm doing it the wrong way...


It's probably already been discussed at
microsoft.public.dotnet.framework.adonet. Have a look there.
--
Armin

Nov 20 '05 #4
Dan,
I suggest that you look for a copy of David Sceppa's book 'ADO.NET
Core Reference' which goes over this (and the requirements to make it work
correctly). The book is very useful and has a lot of examples.

Ron Allen

"Dan Keeley" <ma********@hotmail.com> wrote in message
news:7q*****************@newsfep4-winn.server.ntli.net...
Excellent thanks.

The answer truns out to be:

selectCMD.CommandText = "Select @@identity"

MsgBox(selectCMD.ExecuteScalar())

I will certainly trawl the microsoft newsgroup a bit now.

Thanks again!
Dan

"Armin Zingler" <az*******@freenet.de> wrote in message
news:Om**************@TK2MSFTNGP12.phx.gbl...
"Dan Keeley" <ma********@hotmail.com> schrieb
Hi.

I'm inserting a record into a DB, how do i get the value back of
an AutoNumber field so i can use it in subsequent SQL statements?

At the moment I have code vaguely like this:

Dim selectCMD As OleDbCommand = New OleDbCommand("INSERT into
AuditItems ( ParentAuditID, section, score, comments) values('1')",
conn)

MsgBox(selectCMD.ExecuteNonQuery())

I presume this is quite easy, maybe i'm doing it the wrong way...


It's probably already been discussed at
microsoft.public.dotnet.framework.adonet. Have a look there.
--
Armin


Nov 20 '05 #5

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

Similar topics

2
2820
by: Jan Hendrickx | last post by:
Hi all, I use (something like) following code to add new records to a database, but it doesn't work!!! I need to know the value of an AutoNumber-field when I add the record. Why doesn't it...
4
6443
by: Phillip J. Allen | last post by:
Hi all, I have a table with an “autonumber” primary key field that also acts as a foreign key in 2 other tables. I would like to programmatically add a new record to the first table and the...
2
2267
by: Megan | last post by:
Hi everybody- I have 2 tables, Hearings and Rulings. Both have primary keys called, CaseID, that are autonumbers. I don't want both tables to have the same autonumber. For example, if Hearings...
4
4520
by: DHRRush | last post by:
I have a simple complaint tracking Db with one main table and one main form for entering data. I need for the users to be able to see the record number for future reference when they come back to...
15
2943
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
5
2431
by: marko | last post by:
I would like my autonumber field to start at 100000. How can i do that?
7
2969
by: jmar | last post by:
I'm hoping someone out there can give me a little guidance. I have an Access Database "Customer.MDB" with a table "CustInfo" that has the following design: Field: DataType CustID AutoNumber...
6
1889
by: PeteCresswell | last post by:
Got a table with "RecordID"=AutoNumber=PrimaryKey. Call it "tblIssuerRating". Ran a query that appends to that table from a work table. Inadvertantly appended from the work table's...
6
8030
by: Wayne | last post by:
I'm using the following SQL statement to find the next highest autonumber value in a table where "CDUGActID is the autonumber field in the "CDUGActuals" table: SELECT CDUGActuals.CDUGActID,...
2
2034
by: Richard Sherratt | last post by:
I've inherited a system that was designed by someone with no understanding of database design. From the logical design point of view, there is no logical design. The physical design is a nightmare....
0
7124
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
6998
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
7163
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
7200
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...
1
6884
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.