SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Building Lists from your Database
By Scott Isaacs

A common operation when working with databases on the web is to populate a list box with the results of a query. On SiteExperts, we use a function, BuildListBox, to simplify this list box creation process. This function is part of a utility library we have built to simplify our page and site management.

Our BuildListBox function generates single or multiselect list boxes as well as alternative representations using radio buttons or checkboxes. The BuildListBox is a relatively simple function that takes 8 arguments:
Argument Values Description
sFormat "SELECT"
"INPUT"
Defines whether to generate a list box or checkboxes/ radio buttons. Whether checkboxes or radio buttons are used is defined by the bMulti argument.
sName String Defines the name attribute of the list box or each input element. For example, <SELECT NAME="States">
bMulti true
false
Defines whether the list is single or multi-select. If you format the list as INPUT elements, radio buttons are used for single selection and checkboxes are used for multi-selection.
iSize integer For SELECT list boxes, defines the size of the list box. 0 or 1 defines a drop down, >1 defines a list box. For multi-select list boxes, the value must be greater than 1. This argument is ignored for the INPUT format.
sExtra string An optional string to append to the SELECT element or each INPUT element. Normally this is an empty string ("") or is used to add an event handler (eg., "ONCLICK='doThis(this)'")
oRS Recordset Object The recordset object used to populate the list.
sValueField string The name of the field used to populate the VALUE attribute.
sTextField string The name of the field containing the text for each option or input element.

function BuildListBox(sFormat,sName,bMulti,iSize,sExtra,oRS,sValueField,sTextField)
  Dim str
  ' Setup SELECT list
  if sFormat="SELECT" then
    str = "<SELECT"
    if (sName<>"") then str = str & " NAME=""" & sName & """"
    if (bMulti) then str = str & " MULTIPLE " 
    if (iSize>0) then str = str & " SIZE=""" & iSize & """"
    if (sExtra<>"") then str = str & " " & sExtra
    str = str & ">"
  end if
  while not oRS.eof
    str = str & chr(13) & chr(10)
    select case sFormat
      case "SELECT": ' Create option
        str = str & "<OPTION"
      case "INPUT":  ' Create radio or checkbox
        str = str & "<INPUT NAME=""" & sName & """ TYPE="
        if bMulti then
          str = str & """checkbox"""
        else
          str = str & """radio"""
        end if
    end select
    if sValueField<>"" then str = str & " VALUE=""" & oRS.fields(sValueField) & """"
    str = str & ">" & oRS.fields(sTextField) 
    if sFormat="INPUT" then str = str & "<BR>" 
    oRS.movenext
  wend
  ' Close SELECT list
  if sFormat="SELECT" then str = str & "</SELECT>"
  BuildListBox = str
End Function

Next we explain how to use the BuildListBox function.

Page 1:Building Lists from your Database
Page 2:Using the BuildListBox Function