ASPProtect ( Click Here For More Details !!! )
Return To Home Page - Link to "The Wanker" - Contact Us


Return To Home Page

Forms - Populating a drop down menu with info from a database



Below is an example of a basic drop down menu populated from a database.

<%
Dim DataConn
Dim CmdPopulateStates
Dim SQL
    

Set DataConn = Server.CreateObject("ADODB.Connection")
Set CmdPopulateStates = Server.CreateObject("ADODB.Recordset")

%>

Take away the comment on Sytem DSN version below if you want to use a system DSN instead and add a comment to the DSN-LESS connection version below it

<%
' DataConn.Open "DSN=System_DSN_Name"
   

DataConn.Open "DBQ=" & Server.Mappath("_database/zipcodes.mdb") &   ";Driver={Microsoft Access Driver (*.mdb)};"
    

SQL = "SELECT DISTINCT STATE_NAME FROM STATES"
CmdPopulateStates.Open SQL, DataConn
%>

<form method="POST" action="somepage.asp">
<Select Name="STATE_NAME" size="1">
<%While Not CmdPopulateStates.EOF%>

<option value="<%= CmdPopulateStates("STATE_NAME") %>"><%= CmdPopulateStates("STATE_NAME") %></option>

<%
CmdPopulateStates.MoveNext
Wend

   
CmdPopulateStates.Close
Set CmdPopulateStates = Nothing
DataConn.Close
Set DataConn = Nothing
%>

</Select>
<input type="submit" value="Submit">
</form>


And this is what the output would look like.


Below is an example of a more advanced drop down menu populated from a database.
It is intelligent and if the database record already has a value for the field it will make sure the correct value is selected in the menu.

<%
Dim DataConn
Dim CmdPopulateStates
Dim SQL
Dim CURRENT_STATE_NAME
%>

This is just an example.. when doing this for real you would do a query to determine the existing values for all the form fields. For this example we will simply set a variable to the value for demonstration purposes.

<%
CURRENT_STATE_NAME = "New York"
    

Set DataConn = Server.CreateObject("ADODB.Connection")
Set CmdPopulateStates = Server.CreateObject("ADODB.Recordset")

%>

Take away the comment on Sytem DSN version below if you want to use a system DSN instead and add a comment to the DSN-LESS connection version below it

<%
' DataConn.Open "DSN=System_DSN_Name"
   

DataConn.Open "DBQ=" & Server.Mappath("_database/zipcodes.mdb") &   ";Driver={Microsoft Access Driver (*.mdb)};"
   
SQL = "SELECT DISTINCT STATE_NAME FROM STATES"
CmdPopulateStates.Open SQL, DataConn
%>

<form method="POST" action="somepage.asp">
<Select Name="STATE_NAME" size="1">
<%While Not CmdPopulateStates.EOF%>

<option <% If CURRENT_STATE_NAME = CmdPopulateStates("STATE_NAME") Then Response.Write(" selected ") %>value="<%= CmdPopulateStates("STATE_NAME") %>"><%= CmdPopulateStates("STATE_NAME") %></option>

<%
CmdPopulateStates.MoveNext
Wend
   

CmdPopulateStates.Close
Set CmdPopulateStates = Nothing
DataConn.Close
Set DataConn = Nothing
%>

</Select>
<input type="submit" value="Submit">
</form>


And this is what the output would look like.


Banner
ASP (Active Server Pages) is a technology developed by Microsoft. Pages using ASP are primarily developed in JScript, or VBScript and are integrated into the HTML of your Web pages. The ASP code is compiled on-the-fly by the server and the resulting output is standard HTML. By using ASP, Web pages can be dynamic, full of ever-changing content, and browser independent.
Active Server Pages asp source code database MS SQL MS Access .mdb adovbs.inc cookies calendar codes sql commands scripts asp programming tutorials iis web server components CJWSoft ASPProtect ASPBanner ASPClassifieds www.aspclassifieds.com, www.powerasp.com,www.cjwsoft.com,www.aspphotogallery.com,www.codewanker.com,www.aspprotect.com,www.aspbanner.com,www.abay.com

 

Link to "The Wanker"
© 2007 CodeWanker - Active Server Pages Articles, Code Snippets, & Tutorials