This function can be used to build dynamic dropdowns from a database call. Optimally these functions will be placed in an include file:
<%
'THESE FUNCTIONS ARE GENERIC FOR APPLICATION '================================= dim conn dim rs
function openconn set conn = server.CreateObject("ADODB.Connection") conn.Open session("connstring") 'YOUR CONNECTION STRING end function function closeconn on error resume next conn.Close set conn=nothing on error goto 0 end function function openrs set rs = server.CreateObject("ADODB.Recordset") end function function closers on error resume next rs.Close set rs=nothing on error goto 0 end function
function nz(val) if len(val&"") = 0 then nz = 0 else nz = val end if end function
'====================================================
'THIS IS THE FUNCTION TO BUILD DROPDOWN
function getDropDown(name,sql,dflt,width,base) call openconn call openrs rs.Open sql, conn, 1, 3 %> <select name="<%=name%>" id="<%=name%>" style="WIDTH:<%=width%>px"> <option value="0"><%=base%></option> <% if not rs.EOF then do until rs.EOF if cstr(rs("a")&"") = cstr(nz(dflt)&"") then sel = " selected" else sel = "" end if %> <option value="<%=rs("a")%>"<%=sel%>><%=rs("b")%></option> <% rs.MoveNext loop end if %> </select> <% call closers call closeconn end function %>
CALL THE FUNCTION LIKE THIS:
<% name ="somename" '<--ASSIGN SQL AND POPULATE ALIASES WITH FIELD NAMES '<--"a" is the dropdown value '<--"b" is the dropdown display value sql = "SELECT yourfield as a, yourfield as b from mytable"
'set selected value if wanted (optional) dflt = request("somename")
'set width width="400"
'set default value (optional) base="Select One"
'CALL FUNCTION (SAME AS SUB ROUTINE THOUGH). call getDropDown(name,sql,dflt,width,base) %>
|