Advertisement
2002ASP Databases/ Data Access/ DAO/ ADO #571

Returning the Autonumber Value when adding records

When using Autonumber fields in a database for a UID, you might need this value after you add the record, for that record. This is my example of how to get that value back from the database after it is added using DAO or ADO.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
'DAO Example  
'First Open a updateable recordset
Set rs = db.OpenRecordset("SomeTable")
  With rs
    'Start a New Record
    .AddNew
      !Field2 = "Add your data for this new record" 
    'Add the record to the database
    .Update
  
    'Set the bookmark to Last modified
    .Bookmark = .LastModified
    
    lngResult = rs!AutoNumberUID
  End With
  
  rs.Close
'Ado Example
  Set mrsMDB = New ADODB.Recordset
  
  mrsMDB.CursorType = adOpenKeyset
  mrsMDB.LockType = adLockOptimistic
  mrsMDB.Open "SomeTable", mcnnMDB, , , adCmdTable
      
  With mrsMDB
    .AddNew
    !Field2 = "Add your Data for this record"
    .Update
    varBkMark = .Bookmark
    .Requery
    .Bookmark = varBkMark
    lngNewUID = !AutoNumberUID
    
  End With

Original Comments (3)
Recovered from Wayback Machine