Inserting record with Microsoft Access

Kelly kkranabetter at yahoo.com
Thu Feb 9 14:04:34 EST 2006


"jeffhg582003" <jeffhg582003 at yahoo.com> wrote in 
news:mailman.1648.1139475091.27775.python-list at python.org:

> fields. I am trying to capture the number generated from the insert 
but
> I am not exactly sure how to do that after an insert.

Other folks have pointed you to "select @@identity" but I thought I 
should mention that if you use ADO with the AddNew method then 
autonumbers are magically retrieved. Something like this should work:

rs.AddNew()
rs.Fields("somefield").Value= "Blah"
rs.Update()
print "Autonumber is", rs.Fields("TheAutonumberField").Value

This didn't always work so ADO and the MDB can't be ancient versions.

SQL Server will also do this but the recordset must be opened with:

rs.CursorLocation= constants.adUseServer
rs.Open("Tablename", conn, constants.adOpenKeyset,
        constants.adLockOptimistic)




More information about the Python-list mailing list