Apologies I suspect this question has been asked before but I have yet to find a satisfactory answer.
With an MS Access/ASP combination what is the best way to insert a record and then get the autonumber value? The way I am doing it at the moment is to add the record then do a select all and order by ID DESC then assume the first record in the recordset is the last one added. As well as being a bit inefficient I am not sure if it is totally foolproof especially if the database is being used by a lot of people.
Would it be possible to create a recordset, then write that recordset then get the ID field before closing the recordset?
cheers!
Ed
<%
Set DB = Server.CreateObject("ADODB.Connection")
Set TBL = Server.CreateObject("ADODB.RecordSet")
DB.Mode = adModeReadWrite
DB.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("../directory/userdb.mdb"))
Title=Request.QueryString("Title")
AuthorID=Request.QueryString("AuthorID")
DocsDate=Request.QueryString("DocsDate")
FilePath=Request.QueryString("FilePath")
TBL.Open "INSERT INTO DocsMaster (Title, AuthorID, DocsDate, FilePath) VALUES ('" & Title & "', '" & AuthorID & "', '" & DocsDate & "', '" & FilePath & "')", DB
TBL.Open "SELECT * FROM DocsMaster ORDER BY DocID DESC", DB
DocID=TBL("DocID")
TBL.Close
Set TBL=Nothing
Set DB=Nothing
URL="adddoc2.asp?DocID=" & DocID
Response.Redirect(URL)
%>
With an MS Access/ASP combination what is the best way to insert a record and then get the autonumber value? The way I am doing it at the moment is to add the record then do a select all and order by ID DESC then assume the first record in the recordset is the last one added. As well as being a bit inefficient I am not sure if it is totally foolproof especially if the database is being used by a lot of people.
Would it be possible to create a recordset, then write that recordset then get the ID field before closing the recordset?
cheers!
Ed
<%
Set DB = Server.CreateObject("ADODB.Connection")
Set TBL = Server.CreateObject("ADODB.RecordSet")
DB.Mode = adModeReadWrite
DB.Open ("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" + Server.MapPath("../directory/userdb.mdb"))
Title=Request.QueryString("Title")
AuthorID=Request.QueryString("AuthorID")
DocsDate=Request.QueryString("DocsDate")
FilePath=Request.QueryString("FilePath")
TBL.Open "INSERT INTO DocsMaster (Title, AuthorID, DocsDate, FilePath) VALUES ('" & Title & "', '" & AuthorID & "', '" & DocsDate & "', '" & FilePath & "')", DB
TBL.Open "SELECT * FROM DocsMaster ORDER BY DocID DESC", DB
DocID=TBL("DocID")
TBL.Close
Set TBL=Nothing
Set DB=Nothing
URL="adddoc2.asp?DocID=" & DocID
Response.Redirect(URL)
%>