{"id":71103,"date":"2004-11-01T16:50:00","date_gmt":"2004-11-01T16:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/01\/how-can-i-make-selections-from-a-form-when-using-a-script\/"},"modified":"2004-11-01T16:50:00","modified_gmt":"2004-11-01T16:50:00","slug":"how-can-i-make-selections-from-a-form-when-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-make-selections-from-a-form-when-using-a-script\/","title":{"rendered":"How Can I Make Selections from a Form When Using a Script?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I\u2019d like to have a form with four radio buttons on it, each button representing a different computer. You choose a radio button, click another button, and then a script runs against the computer you selected. How do I do that?<BR><BR>&#8212; CW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, CW. If we\u2019re talking strictly VBScript and Windows Script Host, the answer is simple: you don\u2019t. Other than displaying a message box, neither VBScript nor WSH have the ability to create graphical user interfaces; there\u2019s no way to use radio buttons, list boxes, dropdown lists, and other graphical elements from within a script.<\/P>\n<P>But &#8211; oh, you must have seen this episode before. That\u2019s right: first we tell you that something can\u2019t be done, and then we tell you a way to do it anyway. (Hey, everyone needs a gimmick, right?) And you\u2019re right: while you\u2019re not going to do this using only VBScript, you definitely <I>can<\/I> do this using an HTA (HTML Application).<\/P>\n<P>We won\u2019t spend much time talking about HTAs today; if you\u2019re interested, you might check out a <B>Webcast<\/B> we did on this topic about a year ago. Suffice to say that HTAs allow us to combine Internet Explorer and scripting code and, in turn, give our scripts a graphical user interface. And while there are other ways to incorporate a graphical user interface into your scripts, this is probably the easiest for those of you just starting out with GUI development.<\/P>\n<P>Let\u2019s start by giving you the code for the HTA, and then we\u2019ll explain how it all works. This sample HTA displays four radio buttons, each representing a different computer. You select a computer and click a <B>Run Script<\/B> button; when you do, a subroutine will run that goes out, connects to the chosen computer, and then reports back the name of the operating system installed on that computer. Cool, huh? To see how this works, copy the code, paste it into Notepad, and then save the file with a <I>.hta<\/I> file extension (for example, os_name.hta). Don\u2019t use a .vbs file extension; that won\u2019t work. It must be .hta.<\/P><PRE class=\"codeSample\">&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;<\/p>\n<p>Sub RunScript<\/p>\n<p>    If ComputerOption(0).Checked Then\n        strComputer = ComputerOption(0).Value\n    End If\n    If ComputerOption(1).Checked Then\n        strComputer = ComputerOption(1).Value\n    End If\n    If ComputerOption(2).Checked Then\n        strComputer = ComputerOption(2).Value\n    End If\n    If ComputerOption(3).Checked Then\n        strComputer = ComputerOption(3).Value\n    End If<\/p>\n<p>    If strComputer = &#8220;&#8221; Then\n        Exit Sub\n    End If<\/p>\n<p>    Set objWMIService = GetObject _\n        (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n    Set colItems = objWMIService.ExecQuery _\n        (&#8220;Select * From Win32_OperatingSystem&#8221;)\n    For Each objItem in ColItems\n        Msgbox objItem.Caption\n    Next<\/p>\n<p>End Sub<\/p>\n<p>Sub CancelScript\n   Self.Close()\nEnd Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;BODY&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-01&#8243;&gt;atl-ws-01&lt;BR&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-02&#8243;&gt;atl-ws-02&lt;BR&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-03&#8243;&gt;atl-ws-03&lt;BR&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-04&#8243;&gt;atl-ws-04&lt;P&gt;<\/p>\n<p>&lt;input id=runbutton class=&#8221;button&#8221; type=&#8221;button&#8221; value=&#8221;Run Script&#8221; name=&#8221;ok_button&#8221; \nonClick=&#8221;RunScript&#8221;&gt;\n&amp;nbsp;&amp;nbsp;&amp;nbsp;\n&lt;input id=runbutton class=&#8221;button&#8221; type=&#8221;button&#8221; value=&#8221;Cancel&#8221; name=&#8221;cancel_button&#8221; \nonClick=&#8221;CancelScript&#8221;&gt;<\/p>\n<p>&lt;\/BODY&gt;\n<\/PRE>\n<P>So what do we have here? We can break this code down into four sections: two that use HTML tags to display the radio buttons and <B>Run Script<\/B> and <B>Cancel<\/B> buttons, and two others to run subroutines depending on whether you click <B>Run Script<\/B> or <B>Cancel<\/B>. Let\u2019s take a closer look at each of these sections.<\/P>\n<P>Here, for example, is the HTML code which displays the four radio buttons. (If you know HTML, there\u2019s nothing special here; this is standard HTML coding.) Note that all the buttons have the same name (ComputerOption); that\u2019s required to ensure that you can only select one button at a time. Note, too that the Value for each button is set to the name of the computer:<\/P><PRE class=\"codeSample\">&lt;BODY&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-01&#8243;&gt;atl-ws-01&lt;BR&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-02&#8243;&gt;atl-ws-02&lt;BR&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-03&#8243;&gt;atl-ws-03&lt;BR&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-04&#8243;&gt;atl-ws-04&lt;P&gt;\n<\/PRE>\n<P>And here is the code that displays the <B>Run Script<\/B> and <B>Cancel<\/B> buttons. The key here is the onClick parameter, which indicates which subroutine will run when a button is clicked. As you can see when you click the first button, the subroutine RunScript runs; click the second button and the subroutine CancelScript runs:<\/P><PRE class=\"codeSample\">&lt;input id=runbutton class=&#8221;button&#8221; type=&#8221;button&#8221; value=&#8221;Run Script&#8221; name=&#8221;ok_button&#8221; \nonClick=&#8221;RunScript&#8221;&gt;\n&amp;nbsp;&amp;nbsp;&amp;nbsp;\n&lt;input id=runbutton class=&#8221;button&#8221; type=&#8221;button&#8221; value=&#8221;Cancel&#8221; name=&#8221;cancel_button&#8221; \nonClick=&#8221;CancelScript&#8221;&gt;<\/p>\n<p>&lt;\/BODY&gt;\n<\/PRE>\n<P>The CancelScript subroutine, by the way, simply closes the HTA. As you can see, not a terribly-complicated procedure by any means:<\/P><PRE class=\"codeSample\">Sub CancelScript\n   Self.Close()\nEnd Sub\n<\/PRE>\n<P>Now &#8211; at last! &#8211; we get to the good stuff. You select a radio button, and then click <B>Run Script<\/B>. How does our HTA know which button you selected, and how does it know which computer to run the script against? For that matter, where the heck <I>is<\/I> the script we want to run? Relax; everything is here in the RunScript routine:<\/P><PRE class=\"codeSample\">Sub RunScript<\/p>\n<p>    If ComputerOption(0).Checked Then\n        strComputer = ComputerOption(0).Value\n    End If\n    If ComputerOption(1).Checked Then\n        strComputer = ComputerOption(1).Value\n    End If\n    If ComputerOption(2).Checked Then\n        strComputer = ComputerOption(2).Value\n    End If\n    If ComputerOption(3).Checked Then\n        strComputer = ComputerOption(3).Value\n    End If<\/p>\n<p>    If strComputer = &#8220;&#8221; Then\n        Exit Sub\n    End If<\/p>\n<p>    Set objWMIService = GetObject _\n        (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n    Set colItems = objWMIService.ExecQuery _\n        (&#8220;Select * From Win32_OperatingSystem&#8221;)\n    For Each objItem in ColItems\n        Msgbox objItem.Caption\n    Next<\/p>\n<p>End Sub\n<\/PRE>\n<P>The first half of this subroutine determines which button you selected. Radio buttons (at least those with the same name) are stored as an array; the first button in the array is item 0, the second button in the array is item 1, etc. What we do here is determine which button was selected; that\u2019s done by looking at the Checked property for each button. For example, this line of code looks to see if the Checked property of button 0 (the first button in the array) is true; if it is, that means this was the button that was selected:<\/P><PRE class=\"codeSample\">If ComputerOption(0).Checked Then\n<\/PRE>\n<P>And what happens if Checked is true? Well, then we assign the value of the radio button (and remember, the value of the button happens to be the name of a computer) to the variable strComputer:<\/P><PRE class=\"codeSample\">strComputer = ComputerOption(0).Value\n<\/PRE>\n<P>And what happens if Checked is False? No big deal; after all, we check the value of each radio button. Sooner or later we\u2019ll find out which button was selected (and there can only be one). If it turns out that none of the buttons were selected, then we simply exit the subroutine. That\u2019s what this code does:<\/P><PRE class=\"codeSample\">If strComputer = &#8220;&#8221; Then\n    Exit Sub\nEnd If\n<\/PRE>\n<P>If a button was selected, then strComputer will be equal to the name of the computer we want to connect to. And that\u2019s what the second half of this subroutine does: it\u2019s a standard WMI script that connects to the specified computer and returns the name of the operating system that\u2019s installed.<\/P>\n<P>Whew! We\u2019re bet you\u2019re glad <I>that\u2019s<\/I> done, aren\u2019t you? Or at least you would be if we were actually done. But there\u2019s one other point we need to make. The sample HTA we showed you here retrieves the name of the operating system installed on a computer, and then displays that name in a message box. That\u2019s fine, but what if you wanted to display, say, a list of all the services installed on a computer? In that case, you\u2019d find yourself responding to scores of message boxes, which is probably not the user experience you\u2019re we\u2019re hoping for.<\/P>\n<P>So can we fix that? Of course. We don\u2019t want to spend much more time on this, but we need to do a few things. First, we have add a SPAN area to our HTA; this is simply an identifiable area on the screen where we can write information. Use code like this to put a SPAN (with the ID of DataArea) beneath all your buttons:<\/P><PRE class=\"codeSample\">&lt;P&gt;\n&lt;span id=DataArea&gt;&lt;\/span&gt;\n<\/PRE>\n<P>Second, instead of displaying all the data in a message box, we need to gather up that data and store it all in a variable. This code sets the value of the variable strText to anything that\u2019s currently in strText <I>plus<\/I> the value of the caption property <I>plus<\/I> the &lt;BR&gt; tag (which is the HTML equivalent of hitting the ENTER key on the keyboard):<\/P><PRE class=\"codeSample\">strText = strText &amp; objItem.Caption &amp; &#8220;&lt;BR&gt;&#8221;\n<\/PRE>\n<P>Finally, we need to set the InnerHTML property of our SPAN to the value of the variable strText:<\/P><PRE class=\"codeSample\">DataArea.InnerHTML = strText\n<\/PRE>\n<P>Got all that? Again, without bothering to go into much explanation, here\u2019s a revised HTA which gathers the names of all the services installed on a computer, and then writes those names in the HTA itself:<\/P><PRE class=\"codeSample\">&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;<\/p>\n<p>Sub RunScript<\/p>\n<p>    If ComputerOption(0).Checked Then\n        strComputer = ComputerOption(0).Value\n    End If\n    If ComputerOption(1).Checked Then\n        strComputer = ComputerOption(1).Value\n    End If\n    If ComputerOption(2).Checked Then\n        strComputer = ComputerOption(2).Value\n    End If\n    If ComputerOption(3).Checked Then\n        strComputer = ComputerOption(3).Value\n    End If<\/p>\n<p>    If strComputer = &#8220;&#8221; Then\n        Exit Sub\n    End If<\/p>\n<p>    Set objWMIService = GetObject _\n        (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\n    Set colItems = objWMIService.ExecQuery _\n        (&#8220;Select * From Win32_Service&#8221;)\n    For Each objItem in ColItems\n        strText = strText &amp; objItem.Name &amp; &#8220;&lt;BR&gt;&#8221;\n    Next<\/p>\n<p>    DataArea.InnerHTML = strText<\/p>\n<p>End Sub<\/p>\n<p>Sub CancelScript\n   Self.Close()\nEnd Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;BODY&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-01&#8243;&gt;atl-ws-01&lt;BR&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-02&#8243;&gt;atl-ws-02&lt;BR&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-03&#8243;&gt;atl-ws-03&lt;BR&gt;\n&lt;input type=&#8221;radio&#8221; name=&#8221;ComputerOption&#8221; value=&#8221;atl-ws-04&#8243;&gt;atl-ws-04&lt;P&gt;<\/p>\n<p>&lt;input id=runbutton class=&#8221;button&#8221; type=&#8221;button&#8221; value=&#8221;Run Script&#8221; name=&#8221;ok_button&#8221; \nonClick=&#8221;RunScript&#8221;&gt;\n&amp;nbsp;&amp;nbsp;&amp;nbsp;\n&lt;input id=runbutton class=&#8221;button&#8221; type=&#8221;button&#8221; value=&#8221;Cancel&#8221; name=&#8221;cancel_button&#8221; \nonClick=&#8221;CancelScript&#8221;&gt;\n&lt;P&gt;\n&lt;span id=DataArea&gt;&lt;\/span&gt;\n&lt;\/BODY&gt;\n<\/PRE><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov04\/hey1101.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov04\/hey1101.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019d like to have a form with four radio buttons on it, each button representing a different computer. You choose a radio button, click another button, and then a script runs against the computer you selected. How do I do that?&#8212; CW Hey, CW. If we\u2019re talking strictly VBScript and Windows Script [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,5,30],"class_list":["post-71103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I\u2019d like to have a form with four radio buttons on it, each button representing a different computer. You choose a radio button, click another button, and then a script runs against the computer you selected. How do I do that?&#8212; CW Hey, CW. If we\u2019re talking strictly VBScript and Windows Script [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71103"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71103\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=71103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}