Value of an AutoNumber Field?

D

Dan Keeley

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
 
S

steve

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
 
A

Armin Zingler

Dan Keeley said:
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.
 
D

Dan Keeley

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
 
R

Ron Allen

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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top