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

Count selected items in dropdown listbox? 1

Status
Not open for further replies.

FancyPrairie

Programmer
Oct 16, 2001
2,917
US
I have 4 multiselect dropdown listboxes (<select>). The 1st one contains the names of facilites. Then 2nd one contains the names of departments. The 3rd one contains the names of supervisors. And the 4th one contains the names of employees. The values shown in each listbox is dependent on the values selected in the previous listbox. That is, for example, if a specific departemnt is selected, then only supervisors and employees assigned to that department are shown. Note that the employee listbox could contain as many as 6000+ names.

How do I count the number of items selected in each listbox. The code below is too slow. Is there another way?
Code:
	lngCount = 0

	for each oChild in lstEmp.children
		if (oChild.selected) then lngCount = lngCount + 1
	next
 

lngCount = lstEmp.SelCount

Assuming you are referring to vb 6 syntax.
 
How is lstEmp defined ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Sorry, I'm scripting in an HTML page.

<SELECT ...> <OPTION ...>blahblahblah</OPTION></SELECT>
 
I'm pretty sure this is the syntax for getting the number of options in a select element:
lstEmp.options.count
 
The values shown in each listbox is dependent on the values selected in the previous listbox

Why don't you grab the values as they are being selected? That way you won't have to go through a huge loop.
 
Skie, couldn't get lstEmp.options.count to work (illegal syntax).

monksnake, I thought about that. But it seems simple but starts to get complicated. User can shift select or control select. Also, suppose the user selects a specfic facility and then several depts (say 3)within the facility. Three items have been selected. Now suppose the user decides to select a different facility now a whole new set of depts will be displayed but vbscript thinks there are still 3 selected.

I was thinking that if I could find a way to count them quickly I could also use that method to deselect them quickly.
 
Perhaps the options collection is faster than the children ?
lngCount = 0
For Each oOption In lstEmp.options
If oOption.selected Then lngCount = lngCount + 1
Next

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
[1] .count should be read .length. But that return is not meant for the number of selected options, just the number of options under it.
[2] .options is the way to go.
[3] It should not be "that" slow using the proper referencing to the options.
 
Thanks everyone. PHV, your suggestion was, on the average, about .02 seconds faster than what I was doing. However, your method consistently took about the same amount of time. While my method did not. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top