Advertisement
2002C Databases/ Data Access/ DAO/ ADO #9175

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サマリー: 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.

ソースコード
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

オリジナルのコメント (3)
Wayback Machineから復元