
Originally Posted by
siteguru
I empathise with your comments, Andrew. But there's only so much exasperation one can take at times.
BTW - you haven't actually answer the question asked. Which was ... if I have an item predefined in a database, how can I get that item in a droplist pre-selected?

That's what I was trying to get the OP to think through - what does the HTML look like, then how can you make the ASP code create that HTML based on a known option value? The OP has the guts of the code already there, but isn't implementing it right.
I could give the solution, but that won't help the OP learn.
Thanks Ian,
The point I'm trying to make is that on several recent occasions I've come to the forum and seen similar situations where a question is posted and it's as if the OP is being scolded for not knowing the basics. If we're going to mentor people we have to be prepared to break things down into baby steps so that they understand the two or three prerequisite steps required before they can loop through a recordset.
I don't know lucky20's background but it seems to me that he or she hasn't had answers that say ok, in order to do database transactions, you always need three basic things... A connection object, a recordset object and a SQL query.
Now for the answer to the dropdown lucky20 if all you want is a method to automatically fill a dropdown you can use the connection established above because it doesn't change:
Code:
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
Then you use a query that grabs all of the DISTINCT country records from the database and order them by the names of the countries. DISTINCT means that the query will only grab a record of each country once:
Code:
set rs=Server.CreateObject("ADODB.recordset")
sql="SELECT DISTINCT Country FROM Customers ORDER BY Country"
rs.Open sql,conn
Now if you want to create a dropdown you can use the following code which will only create the HTML select menu without the form or any other text, etc...:
Code:
<select name="country">
<%
do until rs.EOF
response.write("<option")
if rs.fields("country")=country then
response.write(" selected")
end if
response.write(">")
response.write(rs.fields("Country"))
rs.MoveNext
loop
rs.Close
set rs=Nothing
%>
</select>
I find that code a bit messy and hard to read with all of the response.write statements and I don't like to leave my html elements open so I would clean it up.
Code:
<%
do until rs.EOF
if rs.fields("country")=country then
selected = "selected = selected" 'NOTE: this preselects the list item if the page is posted back to itself
end if
response.write "<option " & selected & "> " & rs.fields("Country") & " </option>"
rs.MoveNext
loop
rs.Close
set rs=Nothing
%>
Don't forget to close & destroy your connection object at the end of the page so that you don't create memory leaks.
Code:
conn.Close
set conn=Nothing
Bookmarks