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

drop-down menu with pre-selected entry selected 1

Status
Not open for further replies.

raniweb

Technical User
Apr 26, 2004
66
US
I am having a horrible time with this and if anyone could shed some light on it for me I would appreciate it. I'm trying to make an update page where I have a drop down menu that pulls the options from a table. I have another table that will be the one needing to be updated. How in the world do you get the drop down menu to display all the options and pre-select the option from the table that is to be updated?

hours - is the recordset I'm using for the options
Recordset1 - is the recordset of the table to be updated

Here is a piece of the drop down menu code that I've been arguing with:

<option value="<%=(hours.Fields.Item("hourid").Value)%>" <%If (CStr(hours.Fields.Item("hourid").Value) = CStr(Recordset1.Fields.Item("bhours").Value)) Then Response.Write("SELECTED") : Response.Write("")%> ><%=(hours.Fields.Item("hhours").Value)%></option>

Thank you all in advance!
 
try this...
Code:
<%
If (CStr(hours("hourid")) = CStr(Recordset1("bhours"))) Then
  strSelected = " SELECTED"
Else
  strSelected = ""
End If

Response.Write "<OPTION value='" & (hours("hourid")) & "'" & strSelected & ">" & hours("hhours") & "</OPTION>" & vbcrlf
%>
If hours("hourid") and Recordset1("bhours") are integers then you should use Cint() instead of CStr().

By the way, I removed the Fields.Items and the .Value bits from the code to make it easier to read. These are not really necessary since they are the default.

I've also placed the whole code in ASP delimiters <% %>. This will make the code more efficient since the server will not have to keep switching in and out of the ASP scripting engine.

Tony
________________________________________________________________________________
 
Thank you so much for replying back to me. I've inserted the code above and the page is now displaying the drop down menu with a 1 in it (when it should be 3) and the rest of the form is not showing. I messed with it a little but I cannot get the rest of the form to come back. The code below is the entire drop-down menu. Maybe I went wrong some where in it.

<select name="bhours" id="bhours">
<%
While (NOT hours.EOF)

If (CStr(hours("hourid")) = CStr(Recordset1("bhours"))) Then
strSelected = " SELECTED"
Else
strSelected = ""
End If

Response.Write "<OPTION value='" & (hours("hourid")) & "'" & strSelected & ">" & hours("hhours") & "</OPTION>" & vbcrlf
%>
<%
hours.MoveNext()
Wend
If (hours.CursorType > 0) Then
hours.MoveFirst
Else
hours.Requery
End If
%>
</select>
 
what values are hours("hourid") and Recordset1("bhours")?? Are they numbers or text?

Tony
________________________________________________________________________________
 
I have them as char in the database and text on the page.
 
Thank you again for all your help. I've got the rest of the form back but it's only displaying a 1 in the drop down menu (should be a three). I need to have it display all the options with the correct number selected. Any ideas? Thank you in advance.
 
raniweb,

Back to your original post, here's how to fix it:
Code:
<%
hours.MoveFirst
do while not hours.EOF
  response.write "<option value='" & hours.Fields("hourid") & "'"
  if Cstr(hours.Fields("hourid"))=Cstr(Recordset1.Fields("bhours")) then
    response.write(" selected")
  end if
  response.write ">" & hours.Fields("hhours") & "</option>"
  hours.MoveNext
loop
%>
 
Oh my, it works, it really, really works!!! Thank you so much!
 
One last question, I tried to use that code for another drop down menu and I keep getting an 'invalid use of Cstr' error. Any ideas on what I am doing wrong?

minutes - holds the options for the drop down menu
Recordset1 - data to be selected

Here's the code:

<select name="bminutes"><%
minutes.MoveFirst
do while not minutes.EOF
response.write "<option value='" & minutes.Fields("minuteid") & "'"
if Cstr(minutes.Fields("minuteid"))=Cstr(Recordset1.Fields("bminutes")) then
response.write(" selected")
end if
response.write ">" & minutes.Fields("mminutes") & "</option>"
minutes.MoveNext
loop
%>
</select>

Maybe I miss interpreted the code. Quite possible. Thank you in advance.
 
You probably have a NULL value on one of the fields. You may try commenting out your SELECT tag entries, and inspect the contents by doing like this:
Code:
minutes.MoveFirst
do while not minutes.EOF
  response.write "minuteid='" & minutes.Fields("minuteid") & "'"
  response.write "bminutes='" & Recordset1.Fields("bminutes") & "'<br>"
  minutes.MoveNext
loop
Let me know what comes out.
 
Thank you for getting back with me. I really appreciate your help on this.
Here is what displayed on the page after I commented out the select tags.

minuteid='1'bminutes='45'
minuteid='2'bminutes='45'
minuteid='3'bminutes='45'
minuteid='5'bminutes='45'

When I changed minuteid to mminutes in the code above this is what showed on the page:

mminutes='15 'bminutes='45'
mminutes='30 'bminutes='45'
mminutes='45 'bminutes='45'
mminutes='00 'bminutes='45'


The bminutes 45 is right because that's what is in the field in the table that I'm updating. It seems I just cannot get it to select 45 from the minutes recordset.
 
Can you tell what data types are minuteid and bminutes fields?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top