When you call a SQL statement, you can call multiple statements separated by a semicolon. The following is an example of a SQL statement that inserts a new record and then gets the ID assigned to that record, be it an autonumber or guid:
INSERT INTO TheTable (Field1, Field2) VALUES ('Value1','Value2'); SELECT @@IDENTITY AS NewestIDAn example of how to use this would be (this assumes you've already set up your database connection and named it dbConn):
rID = ""Now you have the record ID of your newest record in a variable!
strSQL = ""
strSQL = "INSERT INTO TheTable (Field1, Field2) VALUES ('Value1','Value2'); SELECT @@IDENTITY AS NewestID"
set rs = dbConn.Execute(strSQL)
rID = rs("NewestID")
2 comments:
It defiantly works in Access 2000, as I use this all the time when using Access.
In SQL server I tend to use a Stored Procedure to cut down on the server requirements. :o)
doesn't appear to work on SQL2005
Post a Comment