How to add records using ASP and ADO

My purpose of this example is to show how to get records from an html form and submit that form to an ASP page.  This ASP page will then use ADO to enter the records into the database    When I started doing ASP, getting records from an ASP page to a database was my 1st thing I wanted to do.  I quickly discovered there was a few ways you could do that. This example uses ADO to take data from a html form and submit to an ASP page that processes the data using ADO.  A good book on ADO is by Wrox Press called ADO 2.0 Programmer’s reference.)     Below is the code I used in this example, go ahead and try entering something in.   

1st page the Input form- This is just a standard html Input form

<title>ADO add record example</title>
<h3 align=”center”>ADO example</h3>
<td width=”424″><form method=”post” name=”form1″ action=”addrecordado2.asp”>
<p><strong>First Name</strong><br>
<input type=”text” size=”40″ name=”FirstName”><br>
<strong>Last Name</strong><br>
<input type=”text” size=”40″ name=”LastName”><br>
<input type=”text” size=”40″ name=”FavoriteColor”><br>
<p><input type=”Submit” value=”Submit New Example” name”b1″> </font></p>

2nd The ASP page that process the information

<%@ Language = “VBScript”%>
‘Declare all local variables

dim conn
dim rs
dim strID
dim strconn

‘set a local variable to my DSN-less connection String
strconn = “DRIVER=Microsoft Access Driver (*.mdb);DBQ=” & Server.MapPath(“adoandsqladd.mdb”)

‘Create the Connection object
set conn = server.createobject(“adodb.connection”) strconn

‘Create the recordset object
set rs = server.createobject(“adodb.recordset”)
‘This statement opens the table so we can add a record notice the addnew
‘The 2, 2 is how the table is opened there are many ways it can be opened “tblAdoAdd”, conn, 2, 2

‘Use the addnew method of the recordset object to add a record
‘Set the table column = to my input text box from my form
rs(“FirstName”) = request(“FirstName”)
rs(“LastName”) = request(“LastName”)
rs(“FavoriteColor”) = request(“FavoriteColor”)
‘I do a movelast here to get the ID that is automatically generated
‘I also set the value to a local variable so I can write out to the database
strID = rs(“ID”)


<title>sign on summary</title>

Your Record # is:<% = strID %>
Your First Name is <% = request(“FirstName”) %>
Your Last Name is <% = request(“LastName”) %>
Your Favorite Color is <% = request(“Favoritecolor”) %>


‘Always! Always set your objects to nothing.  This clears them out of servers memory
‘Your network admins will like this

set rs= nothing
set conn = nothing

[gp-comments width="770" linklove="off" ]