Select Statement help please...


OK - I am not an HTML programmer - I am a VB Programmer and I understand HTML and this is our first venture into ASP with HTML - and I'm stuck.

The Problem.
We have designed a web site for a heavy plant machinery customer where by they can advertise for sale pieces of plant.

The web site, which we have adopted from another web site allows the user to upload a photo and add description price etc etc.

The problem is when they come to edit an entry. We have added a select statement that allows them to choose from a specific list of equipment type and for a new entry this works fine, but when they edit the item the same select list is used but it defaults to the first item in the select list.

What we require is the field to display the current selection that has been saved in the access database - with also the ability to use the drop down list and select a different model type if required.

Our standard field code looks like this:
<input name="Price" size="40" value="<%=rs("Price")%>"></font></td>                   - "Price" being the stored value from the database.

Our Select Statement looks like this:
<select name="Type" size="1" value="<%=rs("Type")%>">
 <option VALUE="BackHoe/Loaders">BackHoe/Loaders
 <option VALUE="Breakers">Breakers
 <option VALUE="Compressors">Compressors
 <option VALUE="Dumpers">Dumpers
 <option VALUE="Excavators">Excavators
<option VALUE="Misc">Miscellaneous
<option VALUE="Mixers">Mixers
<option VALUE="Rollers-Tandem/Pedestrian">Rollers-Tandem/Pedestrian
<option VALUE="Skid Steel Loaders">SkidSteelLoaders
<option VALUE="Telescopic Handlers/Forklifts">Telescopic Handlers/Forklifts                                    

However the above code section - value="<%=rs("Type")%>"> does not display the stored database value - instead is SHOWS (But has not been stored yet) the first int he select list - "BackHoe/Loaders"

Could anyone provide me with the actual code I need.

Many thanks

Who is Participating?
Ryan ChongCommented:
try this...

   function chkOpt(v1, v2, selectedValue)
         if v1 = v2 then
              chkOpt = selectedValue
              chkOpt = ""
        end if
  end function


<select name="Type" size="1">
 <option VALUE="BackHoe/Loaders" <%=chkOpt("BackHoe/Loaders","" & rs("Type"), " selected")%>>BackHoe/Loaders
 <option VALUE="Breakers" <%=chkOpt("Breakers","" & rs("Type"), " selected")%>>Breakers
 <option VALUE="Compressors" <%=chkOpt("Compressors","" & rs("Type"), " selected")%>>Compressors
 <option VALUE="Dumpers" <%=chkOpt("Dumpers","" & rs("Type"), " selected")%>>Dumpers


<option VALUE="Telescopic Handlers/Forklifts" <%=chkOpt("Telescopic Handlers/Forklifts","" & rs("Type"), " selected")%>>Telescopic Handlers/Forklifts                              

for the <select> tag we do not have to have the "value" attribute, instead it should be for the <option> tag:

Here is one sample HTML which you can try it out:

            <script language="javascript">
                  function dispVal(obj){
                        alert("You have Chosen: "+obj[obj.selectedIndex].value);
<form name="testSelect">
      <select name="test" onchange="javascript:dispVal(this)">
            <option value="No Value" selected>-- Select --
            <option value="First Value">Value 1
            <option value="Second Value">Value 2
            <option value="Third Value">Value 3
            <option value="Fourth Value">Value 4
...please close option tags :)

 <option VALUE="BackHoe/Loaders">BackHoe/Loaders</option>
 <option VALUE="Breakers">Breakers</option>
 <option VALUE="Compressors">Compressors</option>
 <option VALUE="Dumpers">Dumpers</option>
 <option VALUE="Excavators">Excavators</option>
<option VALUE="Misc">Miscellaneous</option>
<option VALUE="Mixers">Mixers</option>
<option VALUE="Rollers-Tandem/Pedestrian">Rollers-Tandem/Pedestrian</option>
<option VALUE="Skid Steel Loaders">SkidSteelLoaders</option>
<option VALUE="Telescopic Handlers/Forklifts">Telescopic Handlers/Forklifts</option>

...humor me about the ASP (it's been a while).  I assume that rs represents a single record from the database?  If not won't you need to iterate through many records to retrieve the data?

...any way.  Typically I believe what you're looking fro is to have a code render block within the select tag that fills the value and text for each option tag.  This would be accomplished with a loop, typically you'll use the primary key of the lookup table as the value.  Makes for quicker processing once you submit.


Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Andy there is a small change. Ignore the previous and try out this code:

            <title>Test Div over Select</title>
                        border:1px solid black
                  <script language="">
                        function loadFrame(){
                              var DivRef=document.getElementById('ac_menu1');
                              var IfrRef=document.getElementById('DivShim');
                     - 1;
<body onload="loadFrame();">
      <iframe id="DivShim" style="display:none;position: absolute;" frameborder="0" border="0"></iframe>
      <div id="ac_menu1" class="ac_menu" style="visibility: visible; position: absolute; z-index: 100; ">
            <div class="ac_normal">
            <div class="ac_normal">
            simple text
            <div class="ac_normal">
            simple text to
            <div class="ac_normal">
            simple text to cover a
            <div class="ac_normal">
            simple text to cover a select
            <div class="ac_normal">
            simple text to cover a select box
      <select id="testSelect" size="5" multiple="yes">
            <option value="1">test 1</option>
            <option value="2">test 2</option>
            <option value="3">test 3</option>
            <option value="4">test 4</option>
            <option value="5">test 5</option>
AndyKeenAuthor Commented:
Hi Folks.

Thanks for all your help.

The problem has been sorted and I will accept RYANCYS answer. My personal opinion is that Ryan's answer was the simplest and I understood it more because it looked more like a VB function - implementation worked the first time and seem's the cleanest - that said I would like to split the points between you all with most going to Ryan. I am concious that people take the time to help each other and I think that in itself deserves a reward.

Thank you all.
Andy actually my second was a wrong post, it was entirely for a different query. Sorry for this but my first post was entirely for you and it is a valid one.
AndyKeenAuthor Commented:
Hi Gops1.

Thanks for that - I have to admit I looked at it and I thought. rriiiggghhhhtttttttt..... what the hecks that all about........ way over my head....

Thanks for the info - I have accepted an answer and split the points (see above) - thanks for the input - much appreciated.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.