Insert New Database Record

There are a lot of instances where you may need to add new records to your database.  For example, you could have your website contact form be inserted to your database for future reference.

To do this, create a simple database called MyData and add a table called tblContact with the following fields:
ID – autonumber
Email – text field
FirstName – text field
LastName – text field
Comments – memo field
DateContacted – date/time field

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

<form name="YourFormName" method="Post" action="confirm.asp">
<tr><td>Email: </td>
<td><input type="text" name="Email" size="50"></td></tr>
<tr><td>First Name: </td>
<td><input type="text" name="FirstName" size="50"></td></tr>
<tr><td>Last Name: </td>
<td><input type="text" name="LastName" size="50"></td></tr>
<tr><td>Comments: </td></tr>
<tr><td><textarea cols="50" name="Comments" ></textarea></td></tr>

<input type="submit" name="Submit" value="Submit">

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

<!--#INCLUDE VIRTUAL="/includes/connection.asp" -->

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "tblContact", objConn, , adLockOptimistic, adCmdTable

objRS("Email") = Request.Form("Email")
objRS("FirstName") = Request.Form("FirstName")
objRS("LastName") = Request.Form("LastName")
objRS("Comments") = Request.Form("Comments")
objRS("DateContacted") = Date()

DIM strFirstName
strFirstName = Request.Form("FirstName")
Thank you for contacting us.  We have received your message and will respond
to you shortly.

Set objRS = Nothing
Set objCONN = Nothing

That’s all there is to it. Now you can add records to your database.

< Back to ASP Scripts