Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


More Useful Form-Building Routines

Use automatic control-building routines to create and populate HTML combo and list box controls from a database.


ast month's column contained some ASP routines that made the task of building a form easy and elegant. This month I'll extend the concept by showing you how to build List and Combo boxes automatically. Usually, these form controls need to display data from a database; therefore, the form-building routines must accommodate database access, which makes the code interesting.

Last month's article discussed:

  • A manual way to build a form containing labels and text boxes
  • Automating the form-building process with functions that create label and text boxes
  • Handling form field validation
Generating Combo and List Boxes Automatically
In HTML, the SELECT tag specifies either a drop down combo box or a list box. The SELECT tag's SIZE attribute value if critical. When the SIZE is 1, the tag creates a combo box; any value greater than 1 creates a list box that number of lines in height. The SELECT tag is a block element that contains one or more OPTION tags that specify the items of the combo or list box. For example, the following code produces a combo box containing three items:


The same code, with just the SIZE attribute changed, produces a list box with three items:


If you were writing code to generate these three combo boxes, for each combo box you would have to:

  • Access the database and obtain a recordset of all records from the appropriate table
  • Output the SELECT tag
  • Within the SELECT tag, for each record in the recordset, output an OPTION tag
  • If the form field already contains a predetermined value, then make sure to pre-select it in the combo box control.
  • Repeat the same steps for the other combo boxes.
You can simplify all the above by writing a function that will handle everything and simply call the function three times to obtain the three combo boxes. In the tradition of our earlier article, this is the code necessary to generate the three combo boxes in addition to the other fields:

WriteFormBegin "frmInfo", "pgApply.asp", _ "POST", "return ValidateForm(this);" WriteTableBegin 600 DisplayTextField "First Name:", "first_name", _ objRS("first_name"), 60, 35, True DisplayTextField "Last Name:", "last_name", _ objRS("last_name"), 60, 35, True DisplayTextField "Title:", "title", _ objRS("title"), 60, 80, False DisplayTextField "Company:", "company", _ objRS("company"), 60, 80, True DisplayTextField "Address:", "address_1", _ objRS("address_1"), 60, 80, True DisplayTextField "", "address_2", _ objRS("address_2"), 60, 80, False DisplayTextField "City:", "city", _ objRS("city"), 40, 35, True DisplayTextField "Zip:", "zipcode", _ objRS("zipcode"), 40, 10, True DisplayComboBox "Industry:", _ "Industry_ID", _ objRS("Industry_ID"), _ "tblIndustry", _ "Industry_ID", _ "Industry_Name", _ True DisplayComboBox "Market:", _ "Market_ID", _ objRS("Market_ID"), _ "tblMarket", _ "Market_ID", _ "Market_Name", _ True DisplayComboBox "State:", _ "State_ID", _ objRS("State_ID"), _ "tblState", _ "State_ID", _ "State_Name", _ True DisplaySubmitButton "Submit", "cmd" WriteTableEnd

The new code is the call to the "DisplayComboBox" sub routine. For the "Industry" combo box, it reads as follows:

DisplayComboBox "Industry:", _ "Industry_ID", _ objRS("Industry_ID"), _ "tblIndustry", _ "Industry_ID", _ "Industry_Name", _ True

The DisplayComboBox Routine
Let's take a look at what this routine does. The DisplayComboBox routine accepts the 7 parameters shown in Table 1.

Table 1: A list of the parameters required by the DisplayComboBox routine and a description of each.

Parameter Description
strLabel The text for the label to use
strFieldName The name of the combo box
strValue The value preselected in the combo box. Not the text, but the value associated with the text of the item.
strLookupTable The database table to use to find all available values for this Combo box
strLookupID The primary key, unique id within the table
strLookupDesc The text of the item to display
blnRequired A flag to indicate this combo box is a required control on the form.


Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date