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

Populating a dropdown list from a database

Status
Not open for further replies.

gmagerr

Technical User
Aug 11, 2001
323
US
Hi guys, i have some code that populates a dropdown list from my database, (email addresses) however some of the people don't have email addresses and it creates blank spaces in the dropdown list.. is there anyway to remove those blank spaces? here's the code.

<%
Option Explicit

'declare variables
Dim conn, sql, rsEmployees

'connect to db and open our query
Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.open &quot;DSN=Employees&quot;

sql=&quot;SELECT Email FROM Employees&quot;

Set rsEmployees = conn.execute(sql)
%>

<html>
<head>
<title>
Database Drop Down Menu
</title>
</head>
<body>
<form action=&quot;../../form2.asp&quot; method=post>
<select name=&quot;TelmonEmail&quot;>
<%
If Not rsEmployees.EOF Then
rsEmployees.MoveFirst
Do
Response.Write(&quot;<option value=&quot;&quot;&quot; & rsEmployees(&quot;Email&quot;) & &quot;&quot;&quot;>&quot; & _
rsEmployees(&quot;Email&quot;) & &quot;</option>&quot;)
rsEmployees.movenext
Loop Until rsEmployees.EOF
End If

conn.close
Set conn=Nothing
Set rsEmployees=Nothing
%>
</select>
</form>
</body>
</html>

Thanks :)
 
Try adding a simple If statement :

<%
Option Explicit

'declare variables
Dim conn, sql, rsEmployees

'connect to db and open our query
Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.open &quot;DSN=Employees&quot;

sql=&quot;SELECT Email FROM Employees&quot;

Set rsEmployees = conn.execute(sql)
%>

<html>
<head>
<title>
Database Drop Down Menu
</title>
</head>
<body>
<form action=&quot;../../form2.asp&quot; method=post>
<select name=&quot;TelmonEmail&quot;>
<%
If Not rsEmployees.EOF Then
rsEmployees.MoveFirst
Do
If Not rsEmployees(&quot;Email&quot;) = &quot;&quot; Then
Response.Write(&quot;<option value=&quot;&quot;&quot; & rsEmployees(&quot;Email&quot;) & &quot;&quot;&quot;>&quot; & _
rsEmployees(&quot;Email&quot;) & &quot;</option>&quot;)
End If
rsEmployees.movenext
Loop Until rsEmployees.EOF
End If

conn.close
Set conn=Nothing
Set rsEmployees=Nothing
%>
</select>
</form>
</body>
</html> Regards

Big Dave


** If I am wrong I am sorry, but i'm only trying to help!! **

 

if len(rsEmployees(&quot;Email&quot;))>0 then
Response.Write(&quot;<option value=&quot;&quot;&quot; & rsEmployees(&quot;Email&quot;) & &quot;&quot;&quot;>&quot; & _
rsEmployees(&quot;Email&quot;) & &quot;</option>&quot;)
end if
 
Sure....just check the value first:

if not isnull(rsEmployees(&quot;Email&quot;) or trim(rsEmployees(&quot;Email&quot;))<>&quot;&quot; then............
 
Thank you very much, you guys are amazing.. here's what finally did the trick

<%
If Not rsEmployees.EOF Then
rsEmployees.MoveFirst
Do
if len(rsEmployees(&quot;Email&quot;))>0 then
Response.Write(&quot;<option value=&quot;&quot;&quot; & rsEmployees(&quot;Email&quot;) & &quot;&quot;&quot;>&quot; & _
rsEmployees(&quot;Email&quot;) & &quot;</option>&quot;)
End If
rsEmployees.movenext

Loop Until rsEmployees.EOF
End If

For some reason this piece

if not isnull(rsEmployees(&quot;Email&quot;) or trim(rsEmployees(&quot;Email&quot;))<>&quot;&quot; Then

Still left some blank fields.. but again thank you all so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top