Getting the value of the next Autonumber

N

Nathan Sokalski

I am using ASP.NET to insert records into a Microsoft Access Database. My
primary keys are of type Autonumber. However, because some of the tables
have relationships I need to know the value of the Autonumber field when I
use the Insert command so that I can put it in the foreign key field of
other tables. I am using VB.NET to code my ASP.NET application. How can I do
this? Thanks.
 
C

Cor Ligthert

Nathan,

What you need to do directly after an update or insert is

dim cmd as New OleDbCommand("SELECT @@IDENTITY", conn)
dim mylastId = cmd.executescalar

I hope this helps,

Cor
 
N

Nathan Sokalski

I think I understand most of that, except for one thing:

When executing the command, how does it know what table to get it from? I
have multiple tables in my DB, and different tables have different numbers
of records. How does it know which table to get the last value from?

Thanks.
 
W

William \(Bill\) Vaughn

In Access the @@IDENTITY is the identity from last table you updated on your
connection.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top