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

Help getting information from Access database - code included

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

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
Cor, thank you much. This worked well, neat!
Pete

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.