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!

Listbox Retrieval Problem

Status
Not open for further replies.

pickletech

IS-IT--Management
Dec 20, 2004
88
US
Hello everyone, I am writing an hta to install printers for people. You pick a department from one listbox, then another listbox shows you which printers are in that department. When you select a printer, a description of the printer is displayed. Choose if you want to set it as your default or not and hit install printer. This is working just fine, but there is a problem with my selecting the printer name from the second listbox. Instead of installing the printer that was selected, it installs the first one in that department. I believe this is my problem right here:
Code:
SelectedPrinter = form.List2.options[form.List2.selectedIndex].text;
Here is the rest of the hta, so you get a better idea of everything.

Code:
<html>
<head>
<title>Install Network Printer</title>
<HTA:APPLICATION
  ID="objInstallPrinter"
  APPLICATIONNAME="Greetings"
  SCROLL="yes"
  SINGLEINSTANCE="yes"
  WINDOWSTATE="normal">
</head>

<script language="javascript">
<!--
function setOption(form, index) {
  for (var i = 0; i < form.List2.length; i++) {
    form.List2.options[i].text = "";
    form.List2.options[i].value = "";
  }
  
    if (index == 0) {
    	form.List2.options[0].text = "";
    form.List2.options[0].value = "";
    }
  else if (index == 1) {
    form.List2.options[0].text = "pamholpr001";
    form.List2.options[0].value = "Accounting LaserJet 8000";
    form.List2.options[1].text = "pamholpr006";
    form.List2.options[1].value = "T&W LaserJet 4000";
    form.List2.options[2].text = "pamholpr007";
    form.List2.options[2].value = "LaserJet 4";
    form.List2.options[3].text = "pamholpr008";
    form.List2.options[3].value = "Copy room LaserJet 4100";
    form.List2.options[4].text = "pamholpr010";
    form.List2.options[4].value = "Copy room copier";
    form.List2.options[5].text = "pamholpr011";
    form.List2.options[5].value = "Engineering Color LaserJet 5500";
    form.List2.options[6].text = "pamholpr012";
    form.List2.options[6].value = "Accounting Color LaserJet 4600";
    form.List2.options[7].text = "pamholpr014";
    form.List2.options[7].value = "T&W Epson FX 890 Impact Printer";
    }
  else if (index == 2) {
  	form.List2.options[0].text = "pamholpr002";
    form.List2.options[0].value = "Human Resources LaserJet 4";
    }
  else if (index == 3) {
  	form.List2.options[0].text = "pamholpr003";
    form.List2.options[0].value = "Maintenance LaserJet 8000";
    form.List2.options[1].text = "pamholpr004";
    form.List2.options[1].value = "Receiving LaserJet 2200";
    }
  else if (index == 4) {
  	form.List2.options[0].text = "pamholpr013";
    form.List2.options[0].value = "Process Control LaserJet 4";
    }
  else if (index == 5) {
  	form.List2.options[0].text = "pamholpr005";
    form.List2.options[0].value = "Production LaserJet 5 (Kurt's Desk)";
    form.List2.options[1].text = "pamholpr009";
    form.List2.options[1].value = "Quality LaserJet 5si";
    form.List2.options[2].text = "pamholpr016";
    form.List2.options[2].value = "Prod Maint/Eng Color LaserJet 5100";
    form.List2.options[3].text = "pamholpr017";
    form.List2.options[3].value = "Color LaserJet 3500";
  }
  form.List2.selectedIndex = 0;
  form.ValueIn2.value = form.List2.options[form.List2.selectedIndex].value;
  SelectedPrinter = form.List2.options[form.List2.selectedIndex].text;
}
//-->
</script>


<body>
<form name="ThisForm">
<select name="List1" OnChange="setOption(this.form, this.selectedIndex)">
<option> Choose Location
<option> Front Office
<option> Human Resources
<option> Maintenance
<option> Process Control
<option> Production
</select>
<select name="List2" OnChange="this.form.ValueIn2.value=this.options[this.selectedIndex].value">
<option>
<option>
<option>
<option>
<option>
<option>
<option>
<option>_________________
</select>
<br>
Description: <input name="ValueIn2" size="30">
<br>
<input type="checkbox" Name="DefaultPrinter">Make it default? </input>
<BUTTON style="border:5px outset;width:100px" onClick="InstallPrinter()" >Install Printer</BUTTON>
</form>
<script language="vbscript">
<!--
Sub InstallPrinter()
	Set objNetwork = CreateObject("WScript.Network")
	objNetwork.AddWindowsPrinterConnection "\\PrintServer\" & SelectedPrinter
	If thisform.DefaultPrinter.checked = true Then
		objNetwork.SetDefaultPrinter "\\PrintServer\" & SelectedPrinter
	End If
End Sub
//-->
</script>
</body>
</html>

Thank You,
Pickletech
 
Well of course, you're setting the selectedIndex to zero two lines above that one:
Code:
  [red]form.List2.selectedIndex = 0;[/red]
  form.ValueIn2.value = form.List2.options[form.List2.selectedIndex].value;
  SelectedPrinter = form.List2.options[form.List2.selectedIndex].text;
Either remove that line or move it.

Adam

¡ph ¥0u c4n r34d d¡5 ¥0u n33d 70 g37 l4¡d
 
pickletech,

[1] SelectedPrinter is not in-sync after onchange fired from List2.
[2] Just in case for win9x/me, AddWindowsPrinterConnection will need a second parameter.

A brut-force revision is something like this.
[tt]
<select name="List2" OnChange="this.form.ValueIn2.value=this.options[this.selectedIndex].value[blue];SelectedPrinter=this.options[this.selectedIndex].text;[/blue]">
[/tt]
and that if for win9x/me, you need this. (But for winnt series, your line is okay.)
[tt]
objNetwork.AddWindowsPrinterConnection "\\PrintServer\" & SelectedPrinter [blue]document.ThisForm.ValueIn2.value 'win9x/me only[/blue]
[/tt]
You can make some re-design (global variable setting, variable passing between handlers) to make thing tidier.

regards - tsuji
 
No, tried that and it had no effect, because that only runs when you select from the first listbox. Which I had totally looked over until you said that. I'm an idiot. I created a new function that runs when you select from the second listbox and its all gravy now. Thanks for your help. Since your post led me very quickly to solve this I will still give you some credit. Thank You,
Pickletech
 
Haha, thanks tsuji. Apparently you solved it while I was writing my last reply. For pointing out my bonehead mistake you can get a star too. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top