Wrox Home  
Search P2P Archive for: Go

  Return to Index  

access_asp thread: Adding Record and Retrieving Record just added


Message #1 by "Stephen Proctor" <steveproctor@c...> on Sun, 2 Sep 2001 21:31:25
I am using Access 2000.  As an index field I am using EmpID in autonumber 

format.



I have used the example on page 595 of Beginning ASP 3.0 as a model.  It 

has worked successfully to add the record.



But the part of the program that calls up the record just added does not 

work.  The program opens a database, uses "MoveLast" then increments the 

index by one, giving it the variable intIDForNewRecord.  However, the 

autonumber in Access fills in the index field and I cannot use the 

increment method.



As a result, the following code does not work right:



strSQL="SELECT * FROM Personnel WHERE EmpID= " & intIDForNewRecord

   Response.Write (strSQL)

   objRS.Open strSQL, strLocalCounselConnect, adOpenForwardOnly, 

adLockReadOnly, adCmdText



intIDForNewRecord is not the EmpID just added.  Does anyone know  how I 

can find the EmpID number (an autonumber field) in a record just added in 

the same program?  The difficulty is that I cannot assign a variable to 

the new EmpID during update, so I cannot call up the variable to read the 

record just added.



Thanks for your help.



Steve
Message #2 by "Paul Bruce" <pbruce@c...> on Tue, 4 Sep 2001 02:53:15
Steve, 



I got around this problem by leaving the RecordSet Object Open, take note 

of the following code.  What I am doing hear is adding a new record, then 

storing the autonumber field into a variable called Bookingid 



objRec.Open "booking", objConn, adOpenKeyset, adLockOptimistic, adCmdTable



  objRec.AddNew

		

   objRec("booked_by")  = username

   objRec("phone") = phonenum

   objRec("email") = emailaddress 

   objRec("Usernotes") = notes



   objRec.Update



' Need to grab the booking id from the database, because at this stage, we 

' don't know what the booking id is (as it is allocated by the database)

   

   Bookingid = objRec("bookingid")

		

	

  objRec.Close



After this code, I could write something like 



strSQL= "SELECT * FROM Equipment WHERE BookID= " & Bookingid 



objRec.Open strSQL, objConn, adOpenForwardOnly, adLockReadOnly, adCmdText



The SQL statement now contains the auto number from the field just added. 



















> I am using Access 2000.  As an index field I am using EmpID in 

autonumber 

> format.

> 

> I have used the example on page 595 of Beginning ASP 3.0 as a model.  It 

> has worked successfully to add the record.

> 

> But the part of the program that calls up the record just added does not 

> work.  The program opens a database, uses "MoveLast" then increments the 

> index by one, giving it the variable intIDForNewRecord.  However, the 

> autonumber in Access fills in the index field and I cannot use the 

> increment method.

> 

> As a result, the following code does not work right:

> 

> strSQL="SELECT * FROM Personnel WHERE EmpID= " & intIDForNewRecord

>    Response.Write (strSQL)

>    objRS.Open strSQL, strLocalCounselConnect, adOpenForwardOnly, 

> adLockReadOnly, adCmdText

> 

> intIDForNewRecord is not the EmpID just added.  Does anyone know  how I 

> can find the EmpID number (an autonumber field) in a record just added 

in 

> the same program?  The difficulty is that I cannot assign a variable to 

> the new EmpID during update, so I cannot call up the variable to read 

the 

> record just added.

> 

> Thanks for your help.

> 

> Steve

  Return to Index