Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP-Databases - A drop down menu returning query results

Status
Not open for further replies.

neillovell

Programmer
Aug 27, 2002
560
GB
Morning!

How can I create a drop down combo box that only holds the results of a query? I know how to make it static in HTML but...I suspect I need an array, but I'm a bit lost after that. Any help much appreciated!
 
Does it fit?

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
<%
Set Conn=Server.CreateObject(&quot;ADODB.Connection&quot;)
Conn.Open(&quot;Logs&quot;)

Set LogRec=Server.CreateObject(&quot;ADODB.Recordset&quot;)
Sel=&quot;SELECT id, data, diasemana, titulo, corpo, logger FROM TB_Log ORDER BY id DESC&quot;
Set LogRec=Conn.Execute(Sel)

response.write &quot;<Select name='brazilian_soccer_team'>&quot;
response.write &quot;<option selected value=''>Choose a player</option>&quot;
Do while not LoRec.EOF
response.write <option value='&quot;&LogRec(&quot;player_id&quot;)&&quot;'>&quot;&LogRec(&quot;player_name&quot;)&&quot;</option>&quot;
LogRec.Movenext
Loop
response.write &quot;</select>&quot;
?>
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 
Excuse me, I didn't fix my 'zombie' properly.
It should be like this:
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

<%
Set Conn=Server.CreateObject(&quot;ADODB.Connection&quot;)
Conn.Open(&quot;Logs&quot;)

Set LogRec=Server.CreateObject(&quot;ADODB.Recordset&quot;)
Sel=&quot;SELECT player_id, player_name FROM TB_Players ORDER BY player_id DESC&quot;
Set LogRec=Conn.Execute(Sel)

response.write &quot;<Select name='brazilian_soccer_team'>&quot;
response.write &quot;<option selected value=''>Choose a player</option>&quot;
Do while not LoRec.EOF
response.write <option value='&quot;&LogRec(&quot;player_id&quot;)&&quot;'>&quot;&LogRec(&quot;player_name&quot;)&&quot;</option>&quot;
LogRec.Movenext
Loop
response.write &quot;</select>&quot;
?>
--------------------------->>>>--------------------
Something like this......?
 
I don't have any luck with that. This is my code, there is a page before that lets the user submit two names to go in to the database.

<HTML>

<HEAD>
<TITLE>Text to Database</TITLE>

</HEAD>


<BODY>

<%
' Name the database we will use
accessdb=&quot;UnsecuredTimesheet.mdb&quot;

' Create the connection object
cn = &quot;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=&quot; &

Server.MapPath(&quot;UnsecuredTimesheet.mdb&quot;) & &quot;;&quot;


set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)


' Put the SQL statement in to the variable sql. This string will be read and thus the

command given.

sql = &quot;INSERT INTO testtable (fname, lname) &quot;
sql = sql & &quot;values ('&quot; & firstname
sql = sql & &quot;','&quot; & lastname & &quot;')&quot;



' test response.write sql This will come out //**********************************
'response.write sql

' Open in read and write mode
rs.Open sql, cn,3,3
%>

<h2>Drop Down Form</h2><br />
Drop Down Should Go Here




</BODY>
</HTML>
 
You've got me confused now...

ok, lets assume that you've inserted these two values into your DB and then you want to show a combo with data from the same DB. alright?
what do you want to fetch from your DB? The same values?
I didn't understand what's the purpose of your page.

You'll do it, just explain things calmly and some one here will help you.
 
All I want is to generate a drop down menu with values retrieved from a query.

Ignore the first and last name entry bits, I just posted the code to show what my connection was looking like.
 
just do it like this :


<%
Dim SearchTextcompany, MyConncompany, SQLcompany, RScompany

Set MyConncompany=Server.CreateObject(&quot;ADODB.Connection&quot;)
MyConncompany.Open &quot;dsnname&quot;,&quot;susername&quot;,&quot;password&quot;

SQLcompany = &quot;SELECT * FROM table&quot;

Set RScompany = MyConncompany.Execute(SQLcompany)
%>


<Select name = &quot;company&quot; >
<option selected>--Select company--

<% While Not RScompany.EOF
%>
<option style=&quot;color:black&quot; value=&quot;<%=RScompany(&quot;ID&quot;)%>&quot;>


<%=RScompany.Fields(&quot;Name&quot;)%>

<%
RScompany.MoveNext
Wend
%>

<%
RScompany.Close
MyConncompany.Close
Set RScompany = Nothing
Set MyConncompany = Nothing
%>

</select>
 
ok. so, pay attention:
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

'create a form
response.write &quot;<form name='any_name' action='u_know' method='get'>&quot;

'create a combo
response.write &quot;<select name='any_name'>&quot;

'Create a Loop...
Do while not cn.EOF

'...that will repeat this instruction:
response.write &quot;<option value='&quot;& rs(&quot;field_name&quot;) &&quot;'>&quot;& rs(&quot;field_name&quot;) &&quot;</option>&quot;
rs.Movenext
' until cn is at the End Of File
Loop

'Close the combo
response.write &quot;</select>&quot;
response.write &quot;</select>&quot;

//// then, your ASP code should look like this :::::

response.write &quot;<form name='any_name' action='u_know' method='get'>&quot;
response.write &quot;<select name='any_name'>&quot;
Do while not cn.EOF
response.write &quot;<option value='&quot;& rs(&quot;field_name&quot;) &&quot;'>&quot;& rs(&quot;field_name&quot;) &&quot;</option>&quot;
rs.Movenext
Loop
response.write &quot;</select>&quot;
response.write &quot;</form>&quot;

??????? The user's browser would receive:


<form name=&quot;any_name&quot; action=&quot;u_know&quot; method=&quot;get&quot;>
<select name=&quot;any_name&quot;>
<option value=&quot;value1&quot;>Value1</option>
<option value=&quot;value2&quot;>Value2</option>
<option value=&quot;value3&quot;>Value3</option>
<option value=&quot;value4&quot;>Value4</option>
</select>
</form>
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_

I hope it fits....
 
welshone, your example is nice, too.
but did you know that the processment of your code by the server will be faster if you dont close/open your ASP?
instead of it, I'd rather use a lot of 'response.write'

but everyone must find his own way to do things. and that's the fun!
:D
 
I have tried both but I must be missing something obvious. Could I bother you once more, if I supply the information?

*The query is called QueryFavProjects. The field to be referenced is ProjID

*The database is called UnsecuredTimesheet.mdb

*I access the database by doing the following
<%
' Name the database we will use
accessdb=&quot;UnsecuredTimesheet.mdb&quot;

' Create the connection object
cn = &quot;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=&quot; &
Server.MapPath(&quot;UnsecuredTimesheet.mdb&quot;) & &quot;;&quot;


set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)

' This is an example of inputting in to the database, just
' to show that it can be done.
sql = &quot;INSERT INTO testtable (fname, lname) &quot;
sql = sql & &quot;values ('&quot; & firstname
sql = sql & &quot;','&quot; & lastname & &quot;')&quot;


' Open in read and write mode
rs.Open sql, cn,3,3
%>


sorry I'm having such a problem inplementing this, I've not used ASP and databases before.
 
Can you tell us what is the error msg you received when you ran the asp?
 
Just it?
It may seem a little &quot;duh&quot;, you know, but if you are running ASP in a localhost, have you properly configured your Personal Web Server?

Another thing, try chnge your line:
cn = &quot;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=&quot; &
Server.MapPath(&quot;UnsecuredTimesheet.mdb&quot;) & &quot;;&quot;

By this:
cn.Open(&quot;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=&quot; &
Server.MapPath(&quot;\UnsecuredTimesheet.mdb&quot;))

Else consider using the ADODB.Connection. Whatever you choose tell us what happened.
so long
 
thanks for the info. macmonteiro , I didn't know opening / closing the asp would effect the speed.
I'll try your way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top