By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

Value of an AutoNumber Field?

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.