Help getting information from Access database - code included

A

anonymous

Hi, excuse as I am new to this.

I have an Access database with two columns. They are
1. ID which is an "autonumber" and the primary key
2. pkData which is a number

I do the following. It works, a new row is added into the access database
table. My problem is that I need to know what the ID (from the autonumber)
is for the row that is inserted. I can't do a query on the table for values
that = "valueToInsert" because there can be more than one of the same value
in the table pkResponse. How do I get the ID autonumber for the newly
inserted row?

Thank you
Pete


Dim db As OleDbConnection
Dim cmd As OleDbCommand

db = New OleDbConnection
db.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data
Source=c:\pkData.mdb"
db.Open()
cmd = New OleDbCommand("INSERT INTO pkResponse VALUES (" +
Str(valueToInsert) +"')", db)
cmd.ExecuteNonQuery()
db.Close()
 
C

Cor Ligthert

Anonymous

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
 

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