|
 |
access_asp thread: need to select a selectbox option using data from database
Message #1 by "Tom Devins" <tdev01@y...> on Mon, 8 Apr 2002 23:25:14
|
|
Hi,
I want to select a selectbox option based on whatever value is in the
database for that field.
However, I have a very long list of items (60+) and was wondering how I
could do this with a loop, an array or something so I don't have to type
out all the if/elseifs by hand.
thanks.
Message #2 by "Blossom" <bmathew1@s...> on Mon, 8 Apr 2002 23:36:30
|
|
Do you want to know how to list the values from a field in a select box (
a dynamic selection list)? Sorry, if I misunderstood your questions.
<tr>
<td><font color = white><B>Category:</td>
<td>
<select name="txtCategory" size="1">
<% do until objRst.EOF %>
<option value="<%=objRst("category")%>">
<%=objRst("category")%>
</option>
<% objRst.MoveNext
loop
%>
</td>
</tr>
Blossom
Message #3 by "Tom Devins" <tdev01@y...> on Tue, 9 Apr 2002 00:06:54
|
|
Not quite.
I have a select box listing all the States and Provinces in the US and
Canada. So on the initial input form, a user selects the state or province
where they live and it goes into the "state_prov" field in the database.
I'm writing an update record script and want the record to show which
state/province was selected in the checkbox. Previously I've just used "if
statements" as the selectboxes where only a couple records but to do an if
statement for this (60+ options) would be tedious.
So I figured the logic/pseudo code would be something like this:
Do Until objRS("state_prov") equals the selectbox option
then mark that option as selected
But I'm having a total brain cramp and have no idea how to write it.
Is my logic off as much as my thinking power or is this kinda what I want
to do?
Message #4 by "Ken Schaefer" <ken@a...> on Tue, 9 Apr 2002 11:49:28 +1000
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Tom Devins" <tdev01@y...>
Subject: [access_asp] need to select a selectbox option using data from
database
: I want to select a selectbox option based on whatever value is in the
: database for that field.
:
: However, I have a very long list of items (60+) and was wondering how I
: could do this with a loop, an array or something so I don't have to type
: out all the if/elseifs by hand.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My suggestion:
a) Use the Recordset object's .GetRows method to move data from your
recordset to a VBScript array. This enables you to dispose of the recordset
(and DB Connection) straight away
b) Use a routine to handle the writing of the select box. The routine would
be stored in an external include file. I write out select lists like this:
<%=WriteFormSelectList("Name", "ID", "", arrOptions, intFormat, "",
intSelectedID)%>
where:
Name is the HTML name you want to give to the select list
ID is the HTML ID you want to assign to the select list
The third option is any additional HTML (eg a class="")
The fourth option is a 2D array of select list options
intFormat indicates the format of the array
the fifth option is an optional first <option> element
and intSelectedID is which ID should be selected.
The actual routine that handles all this is (watch for wrapping):
'------------------------------------------------------------
' WriteFormSelectList
'------------------------------------------------------------
' Returns HTML for a <select> element
' Accepts strName as string
' Accepts strID as string
' Accepts strHTMLAttributes as string - literal text for any other HTML
attributes
' Accepts form option elements as array
' Accepts array format as integer: 0 = rows/cols, 1 = cols/rows (eg from
getRows)
' Accepts optional text for first <option></option> tag
' Accepts strSelectedValue as option to be selected
'------------------------------------------------------------
Function WriteFormSelectList( _
ByVal strName, _
ByVal strID, _
ByVal strHTMLAttributes, _
ByVal arrOptions, _
ByVal intArrayFormat, _
ByVal strFirstOption, _
ByVal strSelectedValue _
)
Dim i ' array 1st dimension counter
If not isArray(arrOptions) then
Exit Function
End If
WriteFormSelectList = "<select name=""" & strName & """ ID=""" & strID &
""""
If Len(strHTMLAttributes & "") > 0 then
WriteFormSelectList = WriteFormSelectList & " " & strHTMLAttributes & """"
End If
WriteFormSelectList = WriteFormSelectList & ">" & vbCrLf
If Len(strFirstOption) > 0 then
WriteFormSelectList = WriteFormSelectList & strFirstOption & vbCrLf
End If
If intArrayFormat = 0 then
For i = 0 to Ubound(arrOptions, 1)
WriteFormSelectList = WriteFormSelectList & "<option value=""" &
arrOptions(i,0) & """>" & arrOptions(i,1) & "</option>" & vbCrLf
Next
Else
For i = 0 to Ubound(arrOptions, 2)
WriteFormSelectList = WriteFormSelectList & "<option value=""" &
arrOptions(0,i) & """>" & arrOptions(1,i) & "</option>" & vbCrLf
Next
End If
WriteFormSelectList = WriteFormSelectList & "</select>" & vbCrLf
If Len(strSelectedValue & "") > 0 then
WriteFormSelectList = Replace(WriteFormSelectList, "value=""" &
strSelectedValue & """>", "value=""" & strSelectedValue & """ selected>")
End If
End Function
'------------------------------------------------------------
' --- WriteFormSelectList
'------------------------------------------------------------
|
|
 |