James Martin
2010-03-12 16:42:58 UTC
I have some code which adds a record to a table whose key is an auto-number
field. I need to retain the value of the auto-number field of the new record
to use for subsequent code. Everything was working fine in Access, but when
I migrated the back-end to SQL Server, the code started failing.
Here's the code I'm using (simplified). (TransactionID is the auto-number
field.)
rs.AddNew
lngTransactionID = rs.TransactionID
...
...
...
rs.Update
The code is using lngTransactionID to retain the value of the auto-number
field once the update is done. This is subsequently used to populate a child
table with records linked to that transaction ID.
As far as I can tell, the reason is that, in Access, when a new record is
started, the auto-number field is populated right away, so it's possible to
read its value. But with the back-end in SQL, the auto-number field stays
null until the record is committed.
If I move the lngTransactionID = rs.TransactionID statement to after the
rs.Update, it ends up returning the transaction ID of the first record in
the table, not the record that was just added.
Is there an easy way to force SQL Server to populate the TransactionID field
before the record is updated? Or to retain the value of the new transaction
ID after the record is update?
Thanks in advance.
James
field. I need to retain the value of the auto-number field of the new record
to use for subsequent code. Everything was working fine in Access, but when
I migrated the back-end to SQL Server, the code started failing.
Here's the code I'm using (simplified). (TransactionID is the auto-number
field.)
rs.AddNew
lngTransactionID = rs.TransactionID
...
...
...
rs.Update
The code is using lngTransactionID to retain the value of the auto-number
field once the update is done. This is subsequently used to populate a child
table with records linked to that transaction ID.
As far as I can tell, the reason is that, in Access, when a new record is
started, the auto-number field is populated right away, so it's possible to
read its value. But with the back-end in SQL, the auto-number field stays
null until the record is committed.
If I move the lngTransactionID = rs.TransactionID statement to after the
rs.Update, it ends up returning the transaction ID of the first record in
the table, not the record that was just added.
Is there an easy way to force SQL Server to populate the TransactionID field
before the record is updated? Or to retain the value of the new transaction
ID after the record is update?
Thanks in advance.
James