Deleting Data from an Access Database using Classic ASP
To make the Microsoft Access database tutorials on Connecting, Adding,
Deleting, and Updating, more interesting we are going to use these tutorials to make
a simple Guestbook.
In the first database tutorial, Part: 1 Connecting to an Access Database, we learned
how to connect to a database and display the contents of a database table in a web page.
In the second database tutorial, Part 2: Adding Data to an Access Database, we learned how to
add data to the database created in the first database tutorial and then use the page '' made in the first
database tutorial to display the updated contents of the database.
In this tutorial we are going to create two pages to delete data from the '' database made in the first database
tutorial. The first page is used to display the contents of the database so you can select which entry you want deleted and the second page is
where all the work is done to delete the entry from the database.
Creating a Page to Select the Database Entry to Delete
First we need to create a page to display the contents of the database so we can select which entry that we want to delete.
I'm not going to go into two much detail on this page as it is almost identical to the page '' we created
in the first database tutorial.
The only difference is that we are selecting all fields from the table '' in the Guestbook database so we
are using the SQL query with the wild card, '' to get all the fields from the table.
The other difference is when we are displaying the contents of the database in the web page using the '' method we are now creating a hyperlink to the '' page which we will be creating later in this tutorial
to delete the entry.
|
|
|
|
<html>
<head>
<title>Delete Entry Select</title>
</head>
<body bgcolor="white" text="black">
<%
Dim adoCon
Dim rsGuestbook
Dim strSQL
Set adoCon = Server.CreateObject("ADODB.Connection")
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("guestbook.mdb")
Set rsGuestbook = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT tblComments.* FROM tblComments;"
rsGuestbook.Open strSQL, adoCon
Do While not rsGuestbook.EOF
Response.Write ("<br>")
Response.Write ("<a href=""delete_entry.asp?ID=" & rsGuestbook("ID_no") & """>")
Response.Write (rsGuestbook("Name"))
Response.Write ("</a>")
Response.Write ("<br>")
Response.Write (rsGuestbook("Comments"))
Response.Write ("<br>")
rsGuestbook.MoveNext
Loop
rsGuestbook.Close
Set rsGuestbook = Nothing
Set adoCon = Nothing
%>
</body>
</html> |
|
|
|
|
Save this page as '' in the same folder as the Guestbook database.
Deleting Data from the Guestbook Database
Now we've got the page to select the entry out of the way, we can now make the page to delete the selected entry from the database.
This page contains no HTML so we can start writing the asp straight away, still don't forget the server side script tags, .
First we need to dimension the variables used in the script, so open your favourite text editor and enter the following code.
|
|
|
|
<%
Dim adoCon
Dim rsDeleteEntry
Dim strSQL
Dim lngRecordNo |
|
|
|
|
Next we need to get the '' of the entry to be deleted from the database. This ID number was passed to the
page we are writing by the script we wrote at the beginning of the tutorial, by adding the '' at
the end of the URL.
To read in the ID number we are going to use the '' method of the ASP '' object, we are also going to use the '' VBScript function to convert the ID number to the data type, 'Long
Integer'.
|
|
|
|
lngRecordNo = CLng(Request.QueryString("ID")) |
|
|
|
|
Next we need to create a database connection object on the server using the ADO Database 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 I am going to show you 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 we created above to open the database by telling the connection
object to use the '' to open the database ''.
You'll notice the ASP method '' in font of the name of the database. This is used as we need to get the
physical path to the database. Server.MapPath returns the physical path to the script, e.g. '', 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.
|
|
|
|
adoCon.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("guestbook.mdb") |
|
|
|
|
Next create an ADO recordset object which will hold the records from the database and the new record to be added to the database.
|
|
|
|
Set rsDeleteEntry = Server.CreateObject("ADODB.Recordset") |
|
|
|
|
To query a database we need to use SQL (Structured Query Language). In the next line we initialise the variable '' with an SQL query to read in all the fields from the '' table where the '' = the entry to be deleted, this way the query will only return one record to the recordset.
|
|
|
|
strSQL = "SELECT tblComments.* FROM tblComments WHERE ID_no=" & lngRecordNo |
|
|
|
|
Because we are going to be deleting the record held in the recordset we need to set the LockType of the recordset to '' so that the recordset is locked when it is deleted. The integer value for this lock type is 3.
|
|
|
|
rsDeleteEntry.LockType = 3 |
|
|
|
|
Now we can open the recordset and run the SQL query on the database to get the database entry that we want to delete.
|
|
|
|
rsDeleteEntry.Open strSQL, adoCon |
|
|
|
|
Once the recordset is open and contains the entry we want to delete we can delete the entry from the database by using the '' method of the '' object.
We have finished using the database in this script so we can now close the recordset and reset the server objects.
|
|
|
|
rsDeleteEntry.Close
Set rsDeleteEntry = Nothing
Set adoCon = Nothing |
|
|
|
|
Now that the database entry has been deleted we are going to use the '' method of the ASP response object to
redirect back to the page we wrote at the beginning of this tutorial, '' so that another entry can
be selected to be deleted from the database. Note that if you are going to use the '' method you must
remember to redirect before any HTML is written.
|
|
|
|
Response.Redirect "delete_select.asp"
%> |
|
|
|
|
Now call the file '' and save it to the same directory as the Guestbook database and the '' page, don't forget the '' extension.
And that's about it, you have now created a way to delete entries from a database, to find out how to change entries in the Guestbook database
read the next tutorial on, Updating Data in an Access Database.
If you find that you are getting errors connecting to the database then please read through the Access
Database Errors FAQ's, practically make sure you have the correct '' installed on your system and if
you are using the, '', make sure the permissions are correct for the database and the directory the
database in.
Part 4: Updating Data in an Access Database (Guestbook Pt.4) >>
Accompanying Tutorials in this Series
Now that you have completed this part you should look at the accompanying Tutorials in this series
|