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

Display value in combo box. 1

Status
Not open for further replies.

Syerston

Programmer
Jun 2, 2001
142
GB
Once you have a combo box populated with data from a lookup table, is there an easy way to get the combo box to display one of the values, dependant on the value from a recordset.

Many Thanks
 
It's okay.

I got it to work using the "SELECTED" attribute of the OPTION Element.



John
 
I think you were headed in the right direction above, lets see what you have and we should be able to point out which part is off.

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Do you know how hot your computer is running at home? I do
 
Thanks for replying

Below is the code I am using to populate the combo box. I have also returned another recordset with the property details, the field that relates to this particular combo is rsProperty("District"). Therfore I need to populate the combo as below and then set the value to the above. I've tried using "selected" within the option tag but it does not seem to work.

Any idea.


<SELECT name=&quot;cboDistrictOffice&quot; size=&quot;1&quot;>
<%
'Check for database errors
Call CheckforErrors(oConn)

'Create Recordset and Connection
Set objRS=Server.CreateObject(&quot;ADODB.Recordset&quot;)
sqlText = &quot;SELECT DistrictOffice, RAD FROM ltblDistrictOffice&quot;
objRS.Open sqlText, oConn, adOpenKeyset

'if objRS is not empty then
'loop through the recordset to populate the combobox

Do While Not objRS.EOF
%>
<OPTION VALUE=&quot;<%=objRS(&quot;RAD&quot;)%>&quot; SELECTED=314><%=objRS(&quot;DistrictOffice&quot;)%></OPTION>
<%
objRS.MoveNext
Loop

'close the recorset
set objRS = nothing
%>
</SELECT>
 
Ok, the problem is that your setting every option to selected instead of just the on record that matches in your second recordset. Do a view source in your browser after loading your asp page and you'll see what I mean.

All you really need to do is limit your print statement a little. I'm going to switch it to Response.Writes just to keep from going back and forth as much:
Code:
Do While Not objRS.EOF
   'Write out the beginning of the tag
   Response.Write &quot;<OPTION VALUE=&quot;&quot;&quot; & objRS(&quot;RAD&quot;) & &quot;&quot;&quot;&quot;
   'Write the selected attribute on the matching tag only
   If rsProperty(&quot;District&quot;) = objRS(&quot;RAD&quot;) Then Response.Write &quot; SELECTED&quot;
   'write the rest of the tag
   Response.Write &quot;>&quot; & objRS(&quot;DistrictOffice&quot;) & &quot;</OPTION>&quot;
   objRS.MoveNext
Loop

I think you were just a litle off with the way the selected tag works, but were definately most of the way there already. Basically all we are doing above is restricting it to write out selected only in the option that corresponds to rsProperty(&quot;District&quot;). I'm not sure I am comparing it against the correct field, but I think you'll probably get the idea.

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Do you know how hot your computer is running at home? I do
 
Sorry to trouble you again Tarwn but the page is not displaying. This is the code as it now appears.

Code:
	<SELECT name=&quot;cboDistrictOffice&quot; size=&quot;1&quot;>
	<%
	'Check for database errors
	Call CheckforErrors(oConn)

	'Create Recordset and Connection
	Set objRS=Server.CreateObject(&quot;ADODB.Recordset&quot;)
	sqlText = &quot;SELECT DistrictOffice, RAD FROM ltblDistrictOffice&quot;
	objRS.Open sqlText, oConn, adOpenKeyset
												
	'if objRS is not empty then
	'loop through the recordset to populate the combobox

	Do While Not objRS.EOF
	   'Write out the beginning of the tag
	   Response.Write &quot;<OPTION VALUE=&quot;&quot;&quot; & objRS(&quot;RAD&quot;) & &quot;&quot;&quot;&quot;
	   'Write the selected attribute on the matching tag only
	   If rsRecord(&quot;RAD&quot;) = objRS(&quot;RAD&quot;) Then 
	   Response.Write &quot; SELECTED&quot;
	   'write the rest of the tag
	   Response.Write &quot;>&quot; & objRS(&quot;DistrictOffice&quot;) & &quot;</OPTION>&quot;
	   objRS.MoveNext
	Loop
											
	'close the recorset
	set objRS = nothing
	%>			
	</SELECT>
 
Tarwn

A big star for you. Your example works perfectly. My own stupid typing doesn't.

Many Thanks
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top