This topic is locked

Problem with Adding Record

4/10/2006 5:43:24 PM
ASPRunnerPro General questions
K
Kym author

I have a database with a parent table and a child table. It has a one to many relationship. The user can add 1 record to the parent table and many records to the child table. In the parent table, the primary key is an autonumber that the database populates. I need to find a way to get that value and carry it to the next screen to prepopulate that value when the user adds records to the child table.
After the user adds a record to the parent table, they are redirected to a page that asks them if they want to add records to the child table. I have tried to use the session variable and the dictionary value but it gives me an error. Can you tell me where this value might be stored. Would it be stored anywhere since the database creates the number? Any ideas you have would be very helpful.
Kym

Sergey Kornilov admin 4/10/2006

Kym,
to retrieve ID of just added record use AfterAdd Event to run the following SQL query select max(id) from tablename. Store this number in Session variable ( Session("MasterTableID") = rs(0) ).

Sub AfterAdd()
set rstmp = Server.CreateObject("ADODB.Recordset")
rstmp.Open "select max(id) from tablename", dbConnection
Session("MasterTableID") = rstmp(0)
rstmp.Close : set rstmp=nothing
End Sub


Set Session("MasterTableID") as a default value for foreign key field in child table.