simple way to insert new ms access record and return autoid value

O

olafmol

Hello,

i am wondering if there is a simple way to insert a new record into an MS
Access db and read out the newly created auto-ID field. Using ASP this was
rather simple, but in ASP.NET (VB) it seems to have become rather
complex????

cheers, Olaf
 
E

Elton Wang

Hi Olaf,

To insert a record

In ADO:
Dim conn As New ADODB.Connection
conn.Open connextString
conn.Execute insertSql
conn.Close
Set conn = Nothing

In ADO.NET
Dim conn As New OleDbConnection(connectionString)
conn.Open()
Dim command As OleDbCommand = conn.CreateCommand()
command.CommandType = CommandType.Text
command.CommandText = insertSql
command.ExecuteNonQuery()
conn.Close()

So what's big difference?

HTH

Elton Wang
(e-mail address removed)
 
O

olafmol

Hello Elton,

not much, but where do you read the autonumber value of the newly inserted
record?

cheers, Olaf
 
E

Elton Wang

You can try

After
command.ExecuteNonQuery()

command.CommandText = "SELECT @@IDENTITY"
Dim obj As Object
Dim autoNum As Integer
obj = command.ExecuteScalar()
If not obj Is Nothing Then
autoNum = Integer.Parse(obj)
End If


HTH

Elton
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top