G
Guest
What is the best way to get the value from an autonumber field of the new
record when you do a SQL Insert in Access VBA?
record when you do a SQL Insert in Access VBA?
Ofer said:If you are a single user then you can use the Dmax to get the last
autonumber =dmax("AutoFieldName","TableName")
If multi user, try and find a unique value that you inserted to the
table and add a filter to the dmax
=dmax("AutoFieldName","TableName", "FieldToFilterOn = " & Value) 'For
Number =dmax("AutoFieldName","TableName", "FieldToFilterOn = '" &
Value & "'") 'For String
=dmax("AutoFieldName","TableName", "FieldToFilterOn = #" & Value &
"#") 'For Date
What is the best way to get the value from an autonumber field of the
new record when you do a SQL Insert in Access VBA?
Tim Ferguson said:You can't do this reliably.
@@IDENTITY will return the most recent autonumber so you can be fairly
sure that it belongs to the insert you think it should, as long as no
other process is going on at the same time (Form_Error events, timer
events, etc).
In a multi-user system, then you can't guarantee that another user
won't have inserted a record after your insert but before you reading
the IDENTITY value.
I think @@IDENTITY is connection-specific, so that users inserting
records on other connections won't affect the value returned for the
current connection. I could be wrong about that, but that's what I
read in ...
http://support.microsoft.com/kb/q232144/ INFO: Jet OLE DB Provider
Version 4.0 Supports SELECT @@Identity
... and what my quick experiments seem to show.
Tim Ferguson said:My understanding was the other way round, but I know far better than
to disagree! Thanks for the update.
I did say I'm not sure, so I wouldn't be *too* quick to change your mind
if I were you. And maybe it has changed with Jet updates. But I would
say that it's a point worth checking into, if it comes up again.