Help getting information from Access database - code included

  • Thread starter Thread starter anonymous
  • Start date Start date
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()
 
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
 
Back
Top