How Can I Include Multiple Subroutines in a Single onClick Parameter in a HTA?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! In your HTA examples, you show us how you can click a button and cause a single subroutine to run. How can I add two or more subroutines to the onClick parameter for a button?

— FM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, FM. You know, there’s an interesting branch of cognitive psychology known as problem finding; the idea is that your ability to answer a question often hinges on how you ask the question in the first place. For example, you – and several others – want to know how to specify multiple subroutines in the onClick parameter of a button. We’ve seen people try several variations like this:

<input type=”button” value=”Run Button” onClick=”Script_1; Script_2; Script_3″>

As you’ve discovered, that doesn’t work.

So let’s put our problem-finding skills to the test and see if we can rephrase this question. (Typically the Scripting Guys don’t need to find problems; problems have a way of finding us.) Here’s the crux of the matter: do we really want to add multiple subroutines to the onClick parameter, or do we just want multiple subroutines to run any time we click the button?

If it’s the latter, then we have an answer for you:

<html>
<head>
<title>Multiple Subroutines</title>

<HTA:APPLICATION ID=”objHTAHelpomatic” APPLICATIONNAME=”MultipleSubroutines” SCROLL=”yes” SINGLEINSTANCE=”yes” WINDOWSTATE=”maximize” > </head>

<SCRIPT Language=”VBScript”>

Sub RunScripts Script_1 Script_2 Script_3 End Sub

Sub Script_1 Msgbox “This is subroutine 1.” End Sub

Sub Script_2 Msgbox “This is subroutine 2.” End Sub

Sub Script_3 Msgbox “This is subroutine 3.” End Sub

</SCRIPT>

<body> <input type=”button” value=”Run Button” onClick=”RunScripts”> </body> </html>

Note. The preceding code is designed to be run from an HTA, an HTML Application. If you want to test the code, just copy the script, paste it into Notepad or some other text editor, and save the file using a .HTA file extension.

If you glance through the code you might notice the HTML tag for the button:

<input type=”button” value=”Run Button” onClick=”RunScripts”>

As you can see, we specify only a single subroutine (RunScripts) in the onClick parameter. Ah, but take a look at the code for the subroutine RunScripts:

Sub RunScripts
    Script_1
    Script_2
    Script_3
End Sub

There’s your answer right there. All we do inside this subroutine is call three other subroutines: Script_1, Script_2, and Script_3. That’s how we can run multiple subroutines from a single button click: we don’t put all those subroutines into the onClick parameter, we put them all into the single subroutine called by onClick.

In other words, problem-finding – and cognitive psychology – to the rescue. And to think people told us that graduate school was a huge waste of time and money: at least we got a Hey, Scripting Guy! column out of it!

0 comments

Discussion is closed.

Feedback usabilla icon