T
Tony Epton
Access 2003, SQL 2000, Windows XP
I usually worked in DAO and JET and have the luxury of being able to
do the following:
-----------------------------------------
dim lngClientId as long
dim rs as recordset
set rs = currentdb().openrecordset("tblClient")
rs.addnew
rs![Surname] = "test surname"
rs![FirstName] = "test firstname"
lngClientId = rs![ClientId] 'ClientId is an autonumber field
rs.update
rs.close
set rs = nothing
---------------------------------------
I end up with the value of the autonumber field after adding a new
record.
Of course this does not work for a SQL back end.
Possibilities that spring to mind are :
====
1
====
set rs = currentdb().openrecordset _
("select ClientId from tblClient order by ClientId")
if rs.bof and rs.eof then
lngClientId = 0
else
rs.movelast
lngClientId = rs![ClientId]
endif
rs.close
set rs = nothing
Though this does run the risk of another user adding another record in
between.
====
2
====
or opening a recordset that filters on many of the values in the
client record to find only the record I just added - but this would be
slow or would need a large index overhead.
I'm sure this question has been answered many times - but I cannot
find the right article when I search the knowledge base.
Could someone point me in the right direction please.
I would be prepared to use a different method (ADO) if this would
help.
Many thanks
Tony Epton
I usually worked in DAO and JET and have the luxury of being able to
do the following:
-----------------------------------------
dim lngClientId as long
dim rs as recordset
set rs = currentdb().openrecordset("tblClient")
rs.addnew
rs![Surname] = "test surname"
rs![FirstName] = "test firstname"
lngClientId = rs![ClientId] 'ClientId is an autonumber field
rs.update
rs.close
set rs = nothing
---------------------------------------
I end up with the value of the autonumber field after adding a new
record.
Of course this does not work for a SQL back end.
Possibilities that spring to mind are :
====
1
====
set rs = currentdb().openrecordset _
("select ClientId from tblClient order by ClientId")
if rs.bof and rs.eof then
lngClientId = 0
else
rs.movelast
lngClientId = rs![ClientId]
endif
rs.close
set rs = nothing
Though this does run the risk of another user adding another record in
between.
====
2
====
or opening a recordset that filters on many of the values in the
client record to find only the record I just added - but this would be
slow or would need a large index overhead.
I'm sure this question has been answered many times - but I cannot
find the right article when I search the knowledge base.
Could someone point me in the right direction please.
I would be prepared to use a different method (ADO) if this would
help.
Many thanks
Tony Epton