UD40 Ultradev Tutorials
Home

Tutorials

Search

Links

Credits

Feedback
Home | Search | Back
Tutorial 10 - Update repeated region
This tutorial shows you how to update multiple records with their proper values using a single form. Typically the multiple records are displayed in a repeated region, hence the title of this tutorial.

I am going to use a table that stores the musical genre of a number of famous artists. These will be displayed in a repeated region and at the end of this tutorial you will be able to update and match the various artists with their proper musical genre.

There are a few parts to this tutorial, and Part 1 is to create a repeated region that shows the records. As usual we shall start by creating a recordset to get the necessary data from our database, just a simple one as shown on the right.

Then we will build the page formatting as shown in the screenshot below. A few points to take note here are:
  • you must enclose the entire table within a form
  • submit the form to a page that will perform the updating
  • name the *genre field as Type and then assign a counter to it (see Part 2 below to learn how to do this)


In Part 2 we will add the necessary coding for the multiple update to correctly identify which record to update and with what. The trick here is to assign a "counter" to each of your field names so they become Type1, Type2 and so on.

Let us start with the counter. Insert the below to any area before the repeated region. It can be put just before the form or within the <head></head> section, as long as it is before the repeated region:

<% Dim iCnt
  iCnt = 0
%>

This declares the variable iCnt (i for integer, Cnt for counter) and defines its value as zero. Next, within the repeated region add these that will increase the value of Cnt by one everytime it is looped:

<% iCnt = iCnt + 1 %>

Now add a hidden field next to the counter. Name it following the AutoNumber field of our database, ID, and assign the value of this ID column as its value. We can add the counter to the hidden field name like this: <% = iCnt %>.ID. In the same vain we shall name our *genre field as <%=iCnt%>Type.

Lastly we need to know how many loops in total we have gone through at the end of our recordset. We can know this by making another hidden field just after the repeated region. I shall call it tCnt and assign its value as:
<% = iCnt %>.

The result of our hard work so far looks something like the screenshot below:




Part 3.

In Part 3 we are going to create the codes to do the necessary updating and then redirect to a suitable page.

This part involves some hand-coding and creation of an update Command which has been covered in some of my previous tutorials. Here is everything that is in that page.

I am not going to go into detail every line of code that is there. However there is just a few points which I would like to bring your attention to:
  • You can use a textbox field instead of a dropdown list as shown in this tutorial.
  • Line 13 and 14 is not required if you are using a dropdown list because you will never get an empty field. They are, however, required if you are using a plain textbox field.
  • Make sure your Update button is called "Submit", otherwise modify line 3 accordingly.
Demo | Back


Copyright © 2001, Swee Hoe Ong
All rights reserved.