Insert New Record to Database ASP Script - ASP Web Pro
  Active Server Pages Programming by ASP Web Pro

ASP Scripts

INSERT NEW RECORD

There are a lot of instances where you may need to add new records to your database. For example, it is always a good idea to have your website contact form be recorded in your database for future reference.

To do this, you first need to create a database called MyData. Then create a table called tblContact with these 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">
<table>
<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>
</table>

<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" -->

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

objRS.AddNew
objRS("Email") = Request.Form("Email")
objRS("FirstName") = Request.Form("FirstName")
objRS("LastName") = Request.Form("LastName")
objRS("Comments") = Request.Form("Comments")
objRS("DateContacted") = Date()
objRS.Update
%>

<p>
<%
DIM strFirstName
strFirstName = Request.Form("FirstName")
%>,<br>
</p>

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

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

< Back to ASP Scripts


 

 

Main Menu
Home
ASP Scripts
ASP Tutorials
JavaScripts
Awards
Contact Us

Email This Page

Other Resources
ASP Web Hosting
Search Engine Submission
Programming Help

 

 

 

Other ASP Web Sites

Site Map

 

Last Updated:
02 December 2008