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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a listbox from Access

Status
Not open for further replies.

kaontong

Programmer
Jun 30, 2001
3
IL
Urgent...

Can some body please tell me how to populate a listbox from Access in ASP and include the data connection. The database name is Test.mdb and the table is called tblImageList and I only have 2 fields in it...an ID and the PictureName. What I want to do is for the listbox to retrieve all the names of the images for the Access database so that a user can select the file name of the file and pass it to the second page.

Thank you in advance...I am new at this and could use some of your guys' expertise. Sample code is always appreciated.
 
Set up a system DSN on your server -- call it testDSN -- make it point to your database. You can do this through your Control Panel --> ODBC section

Once you have that, here is some sample code:

<%
dim con, rs
set con = server.createobject(&quot;ADODB.Connection&quot;)
set rs = server.createobject(&quot;ADODB.Recordset&quot;)
con.open (&quot;DSN=testDSN&quot;)
rs.ActiveConnection = con
rs.open &quot;SELECT * FROM tblImageList&quot;
%>

So now you have your recordset with all your data in it. For this example, I'll assume that you want the values of your select box to be the pictureID's and the actual text that appears to be the pictureName.

<select name=pictures>
<option value=none>SELECT ONE</select>
<%
while not rs.eof%>
<option value=<%=rs(&quot;id&quot;)%>><%=rs(&quot;pictureName&quot;)%></option>
<%rs.movenext
wend
%>
</select>

Now there, I'm assuming that 'id' and 'pictureName' are your field names... if they aren't, then you'll need to adjust the code to reflect the actual field names.

and there you have it. pretty simple, huh.

hope that helps! :)
Paul Prewett
penny.gif
penny.gif
 
Thanks for the tip :)

Does this also work for DSN-less connections?

Kato
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top