This topic is locked

Display key value on Add new

8/31/2006 4:21:22 AM
ASPRunnerPro General questions
R
rgke author

In my database, a record key is generated directly in the SQL database whenever a new record is inserted.
My users are complaining that that when they have created a new record and pressed the save button, it does not display what the record key is. They then have to go back to the list and find it.
Is there anyway of retrieving this information for display after they have pressed the save button?

J
Jane 8/31/2006

Hi,
you can do it using events.

Proceed to the Events tab, select After record added event and add your code in it.

Here is a sample code:

Sub AfterAdd()

str = "select @@identity"

Set rsTemp = server.CreateObject("ADODB.Recordset")

rsTemp.open str, dbConnection

%><script language="JavaScript">

alert("you added a new record with id=<%=rsTemp(0)%>");

</script>

<%

End Sub

R
rgke author 9/2/2006

OK thanks, this is excellent. If I wanted to display something other than the primary key (as well), I thought I could modify the line as follows (where ProjMainKey is a field in the recordset).
%><script language="JavaScript">

alert("you added a new record with id=<%=rsTemp("ProjKeyMain")%>");

</script>

<%
However, the page just goes blank; no alert, no error. Do you know why?

J
Jane 9/4/2006

Hi,
you can't use rsTemp("ProjKeyMain") because it isn't in the record set.

User following code:

Sub AfterAdd()

'select id of last inserted record

str = "select @@identity"

Set rsTemp = server.CreateObject("ADODB.Recordset")

rsTemp.open str, dbConnection

'select other info of last inserted record

str1 = "select * from TableName where ID=" & rsTemp(0)

Set rsTemp1 = server.CreateObject("ADODB.Recordset")

rsTemp1.open str1, dbConnection
%><script language="JavaScript">

alert("you added a new record with id=<%=rsTemp(0)%>, ProjKeyMain=<%=rsTemp1("ProjKeyMain")%>");

</script>

<%

End Sub



where ID is your actual field name where primery key is stored, TableName is your actual table name.

R
rgke author 9/4/2006

OK, now this sort of works, except that rstemp(0) returns nothing, so I end up with a syntax error. If I replace rstemp(0) with an actual value hardcoded, it is fine, os obviosly rstemp(0) is not producing any results.
Any idea why?

R
rgke author 9/6/2006

OK, now this sort of works, except that rstemp(0) returns nothing, so I end up with a syntax error. If I replace rstemp(0) with an actual value hardcoded, it is fine, os obviosly rstemp(0) is not producing any results.
Any idea why?

J
Jane 9/6/2006

This code works fine on my test box.
Is your record key auto-increment?