Published: Friday, October 01, 1999
Using Forms to Do Batch Database Updates
Two days ago I posted an article on Using SQL Set Notation to
do Batch Deletes. For that article we used an example database table named Products,
which was defined as follows:
| Products |
| ProductID | Autonumber |
| Name | Text |
| Cost | Currency |
We listed each product in the Products table with a checkbox next to each one. The user
could then select one to many checkboxes, click on the Delete Selected Products, and the
chosen products would be deleted.
Wouldn't it be nice to be able to extend that interface and have the user also be able to update
the costs of the products? In this article, we are going to show how to allow for multiple products'
costs to be updated. Our interface will look like:

To allow for multiple updates, we need some way
to identify each product cost with a
ProductID. Also, since
the number of products the user can update is dynamic, we have to
pass this number to the ASP page that will actually carry out the database updates.
We need to modify
ListProducts.asp from our previous article. This modification will need
to include several new changes. First, we will need to add a text box for the currency. We will
also need to create a new button,
Update Product Costs. Most importantly, we must provide
a way to tie each input box with a uniqe
ProductID.
Let's start our coding for
ListProducts.asp:
<% Option Explicit %>
<!--#include file="adovbs.inc"-->
<%
'First things first, connect to the database and get the Product information
'Open a connection to the database
Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "DSN=TestDB"
objConn.Open
'Get the table information for products
Dim objRS
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "Products", objConn, adOpenForwardOnly, adLockReadOnly, adCmdTable
'Display the FORM and the top of the TABLE
Response.Write "
|
Note that we use the iCount variable to identify each cost and ProductID.
In the ASP page that performs the updates, UpdateCosts.asp, we will loop from 0 to
iCount, using Request(currentiCountLoop & ".Cost") to get the cost of a
product, and Request(currentiCountLoop & ".ID") to get the ProductID. Don't
worry, we'll discuss that in more detail later. Right now, let's finish the code for
ListProducts.asp.
Now that we've listed all of the products with delete checkboxes and a text box to modify the product
cost, we need to close and clean up our ADO objects. Also, we need to show another button,
Update Product Costs. We will have both of our buttons, Update Product Costs
and Deleted Selected Products, call JavaScript functions when they are clicked. If
the user is clicked the delete button, they will be prompted the verify that they do, in fact, wish to
delete the products. If they click the update button, the form's action will be changed to
UpdateCosts.asp and the form will be submitted.
Before we get to the JavaScript, let's look at the code to close and free our ADO objects, and display
the remainder of our HTML page and form.