Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data base

Status
Not open for further replies.

thieg

IS-IT--Management
Jul 1, 2002
5
0
0
US
Hi, I insert a record in a data base (access). And I use a autonumber data type for this primary key.
How can I know the ID of this record, just after my insert?
Thank you...
 
Well, as long as you haven't moved to a different record, then you could just access the value of the column through normal procedures. For instance, if I just inserted the recordset and had not yet moved to the next record, and wanted to output it to the screen, then I would do something like this:
Code:
response.write(myRecordset("idFieldName"))

hope it helps!:)
Paul Prewett
 
If you are using SQL Server check out this link:

If you are using Access, then the most efficent way to do this is to use the AddNew Method of the Recordset object.

this assumes that you have already created a connection object named c, and that you have #include d the adovbs.inc
Code:
dim rs
set rs = server.createobject("adodb.recordset")
rs.open "select * from table", c, adOpenKeySet, adLockOptimistic

rs.AddNew
rs("field1") = value1
rs("field2") = value2
rs.Update
idOfAddedRecord = rs("autonumber")

You want to do it this way, because there is a chance that two people might try to update the DB at the same time. the locking ensures that the users will only get the autonumber that is correctly assigned to them.

good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top