Solved

Add items to a drop down control at run time

Posted on 2003-11-12
8
782 Views
Last Modified: 2010-08-05
Hi all of you,

I am trying to add items to a drop down control at run time. Items will be added to it on clicking a button. I have associated a VBScript function to the button on it onClick event.
But unfortunately the add statement to the drop down control is not working. Please help.
My GO button declaration is like this
<.........GoButton........................ onClick = "loadAttributes()">
 
lstAttribute is the drop down list which should get loaded with some items.
 
SCRIPT Code section:

<SCRIPT language="VBScript">
sub loadAttributes()
 
lstAttribute.add "ABC",1                         (not working ERROR : Type Mismatch. I have also tried lstAttribute.addItem  “ABC”,1  . it is also not           working )
 
end sub
</SCRIPT>
 
please help out with the example.
 
0
Comment
Question by:ussharma
8 Comments
 
LVL 3

Expert Comment

by:monvelasquez
ID: 9737218
if you wouldn't mind, i would rather do this in javascript. here's a sample..

--[CODE]----------------------------------------------------
<HTML>
<head>
<script language="javascript">
   function loadAttributes() {
     var newOp = new Option("Option 2",2);
     document.frm.lstAttribute.add(newOp);
   }
</script>

</head>
<body>

<form name="frm">
<select name="lstAttribute" size="10">
   <option value="1">Option 1
</select>

<input type="button" name="add" value="Add" onclick="loadAttributes()">

</form>
</bodY>
</HTML>
--[/CODE]---------------------------------------------------
0
 
LVL 3

Expert Comment

by:vijay7248
ID: 9737343
Usha,
R u going to do that in the server side or the client side..
If the values are fetched from DB, then I have another option.
Please clear this
0
 
LVL 3

Accepted Solution

by:
vijay7248 earned 250 total points
ID: 9737458
Try this. Cut and paste the below code run the page
<html>
<head>
<SCRIPT LANGUAGE="VBScript">
<!--
sub loadAttributes()
  Document.frmExercise4.cmbProducts.Additem "NEC MultiSync E1100"
  Document.frmExercise4.cmbProducts.Additem "NEC MultiSync P1150"
  Document.frmExercise4.cmbProducts.Additem "NEC MultiSync E750"
  msgbox ("loaded")
end sub
-->
</SCRIPT>
</head>
<body>
<FORM NAME="frmExercise4">
       <OBJECT ID="cmbProducts" WIDTH=159 HEIGHT=24
            CLASSID="CLSID:8BD21D30-EC42-11CE-9E0D-00AA006002F3">
          <PARAM NAME="DisplayStyle" VALUE="7">
          <PARAM NAME="Size" VALUE="4202;635">
          <PARAM NAME="FontHeight" VALUE="200">
          <PARAM NAME="MatchEntry" VALUE="1">
          <PARAM NAME="ShowDropButtonWhen" VALUE="2">
          <PARAM NAME="FontCharSet" VALUE="0">
          <PARAM NAME="FontPitchAndFamily" VALUE="2">
          <PARAM NAME="FontWeight" VALUE="0">
      </OBJECT>
<input type=button name=but1 onclick="loadAttributes()">
</form>
</body>
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:ussharma
ID: 9737755
i need to access the values from the database in Oracle using ADODB connection. but the loading of the drop down list have to be done using VBScript. i have done the same in javascript. dont know why its not working
in VBScript (neither add nor addItem).  
0
 
LVL 2

Expert Comment

by:Sairam_S
ID: 9737835
Hi Sharma,

I read through your doubt and was trying to solve the problem in VBScript. I could not find any solution or reference for the same. I have developed a javascript version of the same code.. You can also include your VBScript functions in the same. i.e your additem code alone would refer to Javascript and other functions can refer to your VBScript Code

#Code Listing#

<html>
      <head>
            <title>Javascript List Populate</title>

            <style>
                  BODY { font-family: Verdana, Arial, Helvetia; font-size: 11px; }
            </style>
            
            <script language="javascript">
                  
                  function addItem()
                  {
                        var frm;
                        frm = document.listDemo;
                        if (frm.txtText.value != "" && frm.txtValue.value != "")
                        {
                              var newOption = new Option(frm.txtText.value, frm.txtValue.value);
                              frm.lstAttribute.options[frm.lstAttribute.length] = newOption;
                              frm.lstAttribute.value = frm.txtValue.value;
                        }
                        else
                        {
                              alert("Please enter the Text to be displayed and value");
                        }
                  }

            </script>

            <script language = "vbscript">
                                      'Write the other VBScript code here
            </script>

      </head>
      <body>
            <h3>List Box Demo :: Populate a new item </h3>
            <form name="listDemo">
                  <select name="lstAttribute">
                        <option value="1">One</option>
                        <option value="2">Two</option>
                  </select>
                  <br /><br /><b>Add a new item</b><br />
                  Item Text : <input type="text" name="txtText" size="30" /> Item Value <input type="text" name="txtValue" size="30" />
                  <br /><br />
                  <input type="button" name="btnAdd" value="Add the new item" onclick="javascript:addItem();" />
            </form>
      </body>
</html>
0
 
LVL 3

Expert Comment

by:vijay7248
ID: 9739172
If your loading populating the listbox with DB values...
Then here's some ASP code. Try this simple and compact method

Let's say ur query is 'qry' and u r storing the result set in 'rs'
Assuming Conn as your Connection Object
qry="select rec_id,first_name from tab1"
set rs=Conn.Execute(qry)  'You can also use Conn.Open here
Then the following code gives you a listbox

Response.Write "<select name='Listbox1'>" & _
   "<option selected value='All'>--All--</option>"
if not rs.eof then
   Response.Write("<option value='")
   Response.Write rs.getstring(,,"'>", _
             "</option><option value='", "-null-")
   Response.Write("'></option>")
end if
Response.write "</select>"

'The above code stores rec_id in the value of OPTIONS and first_name is displayed as the TEXT..
Try this it is very simple

For further reference regarding this code pls refer
http://www.4guysfromrolla.com/webtech/102600-1.shtml

'NOTE : This is a server-side code.
0
 
LVL 7

Expert Comment

by:vnvk
ID: 12064273
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Accept: vijay7248 {http:#9737458}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

vnvk
EE Cleanup Volunteer
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question