Microsoft Access Insert With Autonumber / Identity

Here's a quick little example of how to retrieve the last identity id or autonumber from an MS Access table when the record was inserted.

Here's a quick little example of how to retrieve the last identity id or autonumber from an MS Access table when the record was inserted.

<%

Dim oRec
Dim sConStr
Dim sNewID

sConStr = "your connection string here."
Set oRec = Server.CreateObject("ADODB.RECORDSET")
oRec.CursorLocation = adUseServer

with oRec
.Open "tblMyTableName",sConStr,adOpenKeySet,adLockOptimistic,adCmdTableDirect
.AddNew
.Fields("MYCOLUMNNAME") = "stuff"
.Update
sNewID = .Fields("MYIDENTITYCOLUMN")
End With

oRec.close
Set oRec = nothing

%>


Submission Date:  9/23/2005 2:57:54 PM
Submitted By:  Robbe Morris
My Home Page:  http://www.robbemorris.com

By Robbe Morris   Popularity  (891 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP
Here's my most recent course on Pluralsight. I think it has some interesting insight on IT professional job interviews and using words in your resume to influence the questions you'll be asked. Resumes, Job Seeking, and Interviews in context.