Tuesday, 29 November 2011

INSERT RECORD AND RETRIEVE UNIQUE ID

There are some instances where you will need to insert a record to a database and, at the same time, you will also need to retrieve the unique id that was just created for that record. For example, a visitor to your website may sign up to become a customer. When they complete and submit their registration form, you may want to give them a unique Customer ID #.

The first thing you need to do is create a database called MyCustomers. Then create a table called tblCustomerInfo with these fields:
ID - autonumber
Email - text field
Name - text field
DateEntered - date/time field

Next, create a form page called formpage.asp and copy the below code into your page:

Email:
Name:



Then, create a page called resultspage.asp and copy the below code into your page:



<% DIM objRS Set objRS = Server.CreateObject("ADODB.Recordset") objRS.Open "tblCustomerInfo", objConn, , adLockOptimistic, adCmdTable objRS.AddNew objRS("Email") = Request.Form("Email") objRS("Name") = Request.Form("Name") objRS("DateEntered") = Date() objRS.Update DIM bookmark bookmark = objRS.absolutePosition objRS.Requery objRS.absolutePosition = bookmark DIM strCustomerID strCustomerID = objRS("ID") %>

Thank you for reigstering with us. Here is your Customer ID: <% =strCustomerID %>


<% objRS.Close Set objRS = Nothing objConn.Close Set objConn = Nothing %>

There you have it. Now you can insert a record and retrieve its unique id and display it on your page all in one fell swoop!

http://www.aspwebpro.com/aspscripts/database/retrieveuniqueid.asp

No comments:

Post a Comment