Full code listing for a select box which is dynamically generated when
another select box is changed.
The data comes from the standard Pubs database in SQL 7.0
ONE -----------> MANY
Primary RecordSet Secondary RecordSet
Publishers Titles
pub_id pub_id
pub_name title_id
title
<%@ Language=VBScript %>
<% Option Explicit %>
<%
DIM Conn, rsPrimary, rsSecondary, intFirstPub
' Put your connection information here.
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Pubs", "sa", ""
SET rsPrimary = Server.CreateObject("ADODB.RecordSet")
SET rsSecondary = Server.CreateObject("ADODB.RecordSet")
%>
Dynamic Select Box
<%
rsPrimary.Open "SELECT pub_id, pub_name FROM publishers ORDER BY pub_name", Conn
IF Err.number > 0 THEN
Response.Write "Unable to open Primary list" & " "
Response.Write Err.description
END IF
IF NOT (rsPrimary.BOF AND rsPrimary.EOF) THEN
intFirstPub = rsPrimary("pub_id")
ELSE
intFirstPub = 0
END IF
rsSecondary.Open "SELECT pub_id, title_id, title FROM titles ORDER BY pub_id, title", Conn
IF Err.number > 0 THEN
Response.Write "Unable to open Secondary list" & " "
Response.Write Err.description
END IF
FillArray "arrTitles", rsSecondary, "pub_id", "title_id", "title"
%>
Dynamic Select List
<%
SUB SelectBox(rsOptions, strName, strValue, strDisplay, strSecondary, strArray)
DIM strSelect
strSelect = vbCRLF & ""
END SUB
'Populate arrOptions as a two dimensional array.
'[n][0] =value, [n][1] =display
SUB FillArray(strArrName, rsSource, strKey, strValue, strDisplay)
'Debugging code
'For Each el in rsSource.Fields
' Response.Write " " + CStr(el) + " = " + el.Name
'Next
'Response.Write " strKey: " + strKey + vbCRLF
'Response.Write " strValue: " + strValue + vbCRLF
'Response.Write " strDisplay: " + strDisplay + vbCRLF
Response.Write ""
END SUB
FUNCTION SingleQuote(strTarget)
DIM intPos
'"Escapes" embedded single quote in a text string.
intPos = InStr(1, strTarget, "'")
DO WHILE intPos > 0
strTarget = Left(strTarget, intPos - 1) & "\'" & _
Right(strTarget, Len(strTarget) - intPos)
'Bump up TWO places because the original single quote has moved.
intPos = InStr(intPos + 2, strTarget, "'")
LOOP
SingleQuote = strTarget
END FUNCTION
%>