How to call a VBScript function from button "on click"

Posted on 2008-11-04
Last Modified: 2010-08-05
I've got the following code trying to call a function and pass variables to the function, but I get the message that I cannot use parantheses while calling a sub....

I'm not calling a sub though....


<link rel="stylesheet" type="text/css"


<TITLE>Profile Cleanup Utility</TITLE>











<script language="VBScript">


'Defining variables


Const ForReading = 1

const ForWriting = 2

Const ForAppending = 8


strDate = Date()

intDel = 60

strTotalSize = 0

strMonth = Month(Now)

strDay = Day(Now)

strYear = Year(Now)

Set objConnection = CreateObject("ADODB.Connection")

Set objCommand =   CreateObject("ADODB.Command")

objConnection.Provider = "ADsDSOObject"

objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000

objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 


'Determine location to run script


    Sub RunLocation


		strLocation = SelectLocation.Value

		select case strLocation

			case ""

				GroupSelect.innerHTML = ""


			case "Boise"

				GroupSelect.innerHTML = "<select size='1' width='50' name='SelectGroup' onchange='BoiseSetGroupInfo'>" &_

					"<option selected value=''>Make a Selection</option>" &_

					"<option value='PPD'>PPD</option>" &_

					"<option value='WDTS'>WDTS</option>" &_

					"<option value='TRN'>TRN</option></select>"


		End Select


	End Sub


	Sub BoiseSetGroupInfo


		strGroup = SelectGroup.Value


				select case strGroup


					case ""


					case "PPD"

						Offline = "\\server\path\file.txt"

						Deleted = "\\server\path\file.txt"

						Checked = "\\server\path\file.txt"

						strExcelPath = "\\server\path\file.txt"

						strMachineGroup = "PPD*"

						strGroupLocation = "LDAP://ou=path,ou=path,ou=path,dc=dc,dc=domain,dc=com"

						StartAction.innerHTML = "<input type='button' value='Start Cleanup' name='start_cleanup' onClick='ProcessGroup(Offline,Deleted,Checked,strExcelPath,strMachineGroup,strGroupLocation)'>"								End Select		

	End Sub


	Function ProcessGroup(Offline,Deleted,Checked,strExcelPath,strMachineGroup,strGroupLocation)


		objCommand.CommandText = "SELECT Name, dNSHostName FROM '" & strGroupLocation & "' WHERE objectCategory='computer' AND dNSHostName='" & strMachineGroup & "'"

		Set objRecordSet = objCommand.Execute




		Do Until objRecordSet.EOF

			strComputer = objRecordSet.Fields("Name").Value





		msgbox("Action Complete")

	End Function





<DIV id="Background_DIV">

<img src= "\\server\path\image.bmp" height=100% width=100%/>

<script type="text/javascript">






<DIV id="LocationSelect_DIV">

<select size="1" width="30" name="SelectLocation" onchange="RunLocation">

    <option selected value="">Make a Selection</option>

    <option value="Boise">Boise</option>

    <option value="Seattle">Seattle</option>



<DIV id="GroupSelect_DIV" align="Center">

<span id="GroupSelect"></span>


<DIV id="ActionButton_DIV" align="Center">

<span id="StartAction"></span>


<DIV id="Status_DIV">

<span id="CurrentStatus"></span>





Open in new window

Question by:piattnd
    LVL 14

    Expert Comment

    The function call should not have parenthesis when you use multiple variables.  Use the following in your code and all should work fine (at least for me it did);
    StartAction.innerHTML = "<input type='button' value='Start Cleanup' name='start_cleanup' onClick='ProcessGroup Offline,Deleted,Checked,strExcelPath,strMachineGroup,strGroupLocation'>"
    LVL 7

    Expert Comment

    Try using:

                                                    StartAction.innerHTML = "<input type='button' value='Start Cleanup' name='start_cleanup' onClick='Call ProcessGroup(Offline,Deleted,Checked,strExcelPath,strMachineGroup,strGroupLocation)'>"

    (Add Call infront of it)
    LVL 12

    Author Comment

    I've tried using call and it doesn't work.

    I also rtied what you said, rejoinder, and I don't get a script error,  but the echo returns a null value for any of my variables.  In other words, the values aren't being passed.
    LVL 12

    Author Comment

    Ok so I talked to my scripting buddy and he said I should change the "onclick" portion to something similar to this:

    <....onclick= FunctionName '" & variablename & "'>

    I was able to get it to work for one of my variables, then tried to do the same thing for all of them, and now I consistently get the error:

    "Wrong number of arguments or invalid property assignment"

    Keep in mind I have adjusted my Function to reflect that I'm only trying to pass 1 variable for the time being (until I get the dang thing to work with 1 variable).

    LVL 14

    Accepted Solution

    I know why my method returned null... please use this line instead;
    StartAction.innerHTML = "<input type='button' value='Start Cleanup' name='start_cleanup' onClick='ProcessGroup """ & Offline & """,""" & Deleted & """,""" & Checked & """,""" & strExcelPath & """,""" & strMachineGroup & """,""" & strGroupLocation & """'>"

    Drop the () and use double quotes plus ampersand to get your variables into the line.  So easy I forgot that you were passing variables from above :-)

    I tested this by using a msgbox in the function and I was able to see all the variables being passed.
    LVL 12

    Author Closing Comment

    Holy cow, thank you so much!  I was literally bashing my head on my desk all morning......

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
    This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now