Home | Advertising Info44 USERS CURRENTLY ONLINE   
PowerASP
   Site Search Contact Us Thursday, March 13, 2025  


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.

   Active Server Pages Rule The World
Contact Us  
All artwork, design & content contained in this site are Copyright © 1998 - 2025 PowerASP.com and Christopher J. Williams
Banner ads ,other site logos, etc are copyright of their respective companies.
STATS Unless otherwise noted - All Rights Reserved.

Active Server Pages ASP programs help tutorial tutorials routine routines jobs listserve mailinglist bulletin board bulletin boards programming snippet snippets 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