ASP Hosting - ASP Web Hosting  
ASP Hosting Plans Internet Marketing SQL Hosting Plans Customer Support Contact Us

Access Database and ASP - Adding Records

In this session we use an HTML form to take a site visitors name and comments and add these to the database. We will then use the page 'guestbook.asp' made in the previous ASP session to display the contents of the database.

Creating an HTML Page to Capture User Input

First we need to create an HTML page with a form on it to take the input from the user.

On this page we will have two text boxes, one called 'name' with the other called 'comments'. We will then use the post method to send the page to the file, 'add_to_guestbook.asp' that we are going to be creating later in this session. This page will add the user input into the database.

<html>
<head>
<title>Guestbook Form</title>
</head>
<body bgcolor="white" text="black">
<!-- Begin form code -->
 <form name="form" method="post" action="add_to_guestbook.asp">
     Name: <input type="text" name="name" maxlength="20">
     <br>
     Comments: <input type="text" name="comments" maxlength="50">
     <input type="submit" name="Submit" value="Submit">
 </form>
<!-- End form code -->
</body>
</html>

Save the page as 'guestbook_form.htm' in the same folder as the Guestbook database.

Adding Data to the Guestbook Database

Now that we've got the form to capture the data out of the way we can make the the page that does all the work,  the one that actually adds the data to the database.

This page contains no HTML so we can start writing the asp straight away. Remember to use the server side script tags, <% .... %>.

First we need to dimension the variables used in the script, so open your favorite text editor and enter the following code.

<%
'Dimension variables
Dim adoCon              'Holds the Database Connection Object
Dim rsAddComments   'Holds the recordset for the new record to be added
Dim strSQL               'Holds the SQL query to query the database

Next we need to create a database connection object on the server using the ADO Database connection object.

'Create an ADO connection object
Set adoCon = Server.CreateObject("ADODB.Connection")


Now we need to open a connection to the database. There are a couple of ways of doing this either by using a system DSN or a DSN-less connection. First we'll see how to make a DSN-less connection, as this is faster and simpler to set up than a DSN connection.

To create a DSN-less connection to an Access database we need tell the connection object that we created above to use the 'Microsoft Access Driver' to open the database 'guestbook.mdb'.

You'll notice the ASP method 'Server.MapPath' in front of the name of the database. This is used to get the physical path to the database. Server.MapPath returns the physical path to the script, e.g. 'c:\website\', as long as the database is in the same folder as the script. It now has the physical path to the database and the database name.

'Set an active connection to the Connection object using a DSN-less connection
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")


If on the other hand you want to use a slower DSN connection to the database then you will need to replace the line above with the one below..

'Set an active connection to the Connection object using DSN connection
adoCon.Open "DSN=guestbook"


Next create an ADO recordset object which will hold the records from the database and the new record to be added to the database.

'Create an ADO recordset object
Set rsAddComments = Server.CreateObject("ADODB.Recordset")


To query a database we need to use SQL (Structured Query Language). In the next line we initialize the variable 'strSQL' with an SQL query to read in the fields 'Name' and 'Comments' form the 'tblComments' table.

'Initialize the strSQL variable with an SQL statement to query the database
strSQL = "SELECT tblComments.Name, tblComments.Comments FROM tblComments;"

Set the cursor type we are using to 'adLockOptimistic' so we can move through the record set. The integer value for this is 2.

'Set the cursor type we are using so we can navigate through the recordset
rsAddComments.CursorType = 2


Because we are going to be saving an updated recordset back to the database, we need to set the LockType of the recordset to 'adoLockOptimistic' so that the recordset is locked, but only when it is updated. The integer value for this lock type is 3.

'Set the lock type so that the record is locked by ADO when it is updated
rsAddComments.LockType = 3


Now we can open the recordset and run the SQL query on the database returning the results of the query to the recordset.

'Open the recordset with the SQL query
rsAddComments.Open strSQL, adoCon


Once the recordset is open we can add a new record onto the end of the recordset. In the next line, we let the recordset know we are adding a new record to it.

'Tell the recordset we are adding a new record to it
rsAddComments.AddNew

Now we can add a new record to the recordset. The details taken from the form we created at the start of this session are entered into their relevant fields in the recordset. To get the data entered by the user from the form we use the 'Form' method of the ASP 'Request' object to request the data entered into the text boxes, 'name' and 'comments'.

'Add a new record to the recordset
rsAddComments.Fields("Name") = Request.Form("name")
rsAddComments.Fields("Comments") = Request.Form("comments")


The data has been entered into the recordset. We now save the recordset to the database using the 'Update' method of the recordset object.

'Write the updated recordset to the database
rsAddComments.Update


We have finished using the database in this script so we now close the recordset and reset the server objects.

'Reset server objects
rsAddComments.Close
Set rsAddComments = Nothing
Set adoCon = Nothing


Now that the database is updated we are going to use the 'Redirect' method of the ASP response object to redirect to the 'guestbook.asp' page we created in the first database session, ASP - Connecting a Database, so we can display the updated database. Note that if you are going to use the 'Response.Redirect' method you must remember to redirect before any HTML is written.

'Redirect to the guestbook.asp page
Response.Redirect "guestbook.asp"
%>


Name the file 'add_to_guestbook.asp' and save it to the same directory as the database and the 'guestbook.asp' page. Don't forget the '.asp' extension.

And that's about it! You have now created a simple Guestbook for your web site. To find out how to edit or delete any of the comments from the Guestbook database read the next sessions - Editing a Database and Deleting a Database.

If you find that you are getting errors connecting to the database then please read through the Access Database Errors FAQ's, particularly make sure you have the correct 'ODBC Drivers' installed on your system and, if you are using the, 'NTFS file system', make sure the permissions are correct for the database and the directory the database in.

 

 

Main Page  |  Hosting Plans  |  E-Commerce  |  Sign Up Now!  |  Site Map

asp web hosting by HyperlinkHost.com