%
' This code is hereby granted to the public domain.
'
' ** PrintOptions Documentation **
'
' Summary: PrintOptions is an optimized function that prints
' the OPTIONs list for a SELECT form element.
'
' Parameters:
' objCN [in] - Connection string or opened connection object.
' strsql [in] - A query that retrieves the data for populating
' the options. The first column will be the value of each option,
' and the second column will be the text of each option.
' Any other columns will be ignored.
' selectedID [in] - A single value whose matching option will
' be default selected in the options list if it exists.
'
' Notes:
' * When the values column is an integer, make sure to verify that
' selectedID is numeric and cast it using CLng().
' * The values and text of the options will always be HTML encoded.
' * Nulls will be printed as empty strings.
'
' Usage:
'
'
'
' ** PrintOptionsMultiple Documentation **
'
' Summary: PrintOptionsMultiple does the same thing as PrintOptions,
' except that SelectedID may be a comma-separated list of the values
' of the options that should be default selected in a SELECT form
' element having the MULTIPLE keyword.
' Each value is trimmed and compared as a string.
Function PrintOptions(ByRef objCN, strsql, selectedID)
PrintOptionsInternal objCN, strsql, selectedID, False
End Function
Function PrintOptionsMultiple(ByRef objCN, strsql, selectedID)
PrintOptionsInternal objCN, strsql, selectedID, True
End Function
' Do not use this internal function, use the above function stubs.
Function printOptionsInternal(ByRef objCN, strsql, selectedID, bAllowMultiple)
Dim objRS, ArrayRecords, i
Dim objDictionary, arraySelected, bIsSelected
Set objRS = Server.CreateObject("ADODB.Recordset")
' Get all values from selectedID as strings into a dictionary
If bAllowMultiple Then
Set objDictionary = Server.CreateObject("Scripting.Dictionary")
arraySelected = Split(Trim(selectedID),",")
For Each selectedID In ArraySelected
If Trim(selectedID) <> "" Then
objDictionary.Add CStr(Trim(selectedID)), CStr(Trim(selectedID))
End If
Next
End If
objRS.Open strsql,objcn,adOpenForwardOnly,adLockReadOnly,adCmdText
Do While Not objRS.EOF
ArrayRecords = objRS.GetRows(50)
For i = 0 To UBound(ArrayRecords,2)
bIsSelected = False
If bAllowMultiple And Not IsNull(ArrayRecords(0, i)) Then
If objDictionary.Exists(CStr(Trim(ArrayRecords(0, i)))) Then
bIsSelected = True
End If
Else
If ArrayRecords(0, i) = selectedID Then
bIsSelected = True
End If
End If
If bIsSelected Then
Response.write ""
Next
Response.write vbCrLf
Loop
objRS.Close
Set objRS = Nothing
If bAllowMultiple Then
objDictionary.RemoveAll
Set objDictionary = Nothing
End If
End Function
%>