VBScript Tutorial – Table of Content
In this VBScript Tutorial, we are going to learn about VBScript Procedures, including VBScript Functions and VBScript Sub Procedures. Also, we are going to learn about VBScript Error Handling and approaches to Execute VBScript through this tutorial.
VBScript Tutorial #3: VBScript Procedures
VBScript procedures are the block of statements enclosed by special keywords to perform specific actions. VBScript procedures have the ability to accept input values and return the output value. Here, the input values are represented as the arguments.
VBScript procedures are mainly used to organize the codes in a professional way for code reusability and better maintenance. Let’s assume a big program or codes have some basic arithmetic operation is performed repetitively. So, if there is a change in that operation, we need to change in every place where it’s used. Instead of doing the same, if we write one VBScript procedure for the same operation and use the reference of that in all the places, then we need to change the code in one place. In this way, we can reduce script maintenance efforts.
VBScript procedures can be referred to by using the keyword “Call”. Also, VBScript allows calling any procedure without using this keyword.
The Advantages of VBScript Procedures:
· Code reusability.
· Reduce script maintenance efforts.
· Better readability of codes.
· Better organization of codes.
Types of VBScript Procedures:
The VBScript procedures are accepting inputs, process it and perform some operation based on the types of procedure. Broadly, VBScript procedures are classified into two types which ate specified below –
· VBScript Sub procedure
· VBScript Function procedure
VBScript Sub Procedures:
The VBScript sub procedures group multiple statements without returning any output values. It can accept the inputs as arguments. This type of procedures is to start and end with Sub and End Sub keywords, respectively. VBScript Sub procedures can accept arguments but do not return any output values.
Example – Here we will write a small vbscript sub procedure which accept an argument as alert messages and display it in a popup message box.
‘Call the vbscript sub procedure Call displayAlert(“This is an example of vbscript sub procedure”) Sub displayAlert(alertMessage) Msgbox alertMessage End Sub
If we want to execute a block of statements with returning any output values, we need to use VBScript functions. At the beginning of the VBScript functions, we need to use “Function” keyword to define the function name and at the end the “End Function” keyword is used. VBScript Functions can accept arguments as well as return values. To return value from a function, the value has to be assigned to the function name before closing the function.
Example – In this example, we will calculate the circle area using a vbscript function. Here, the radius will be passed as an argument to the VBScript function and return the area as output.
Dim calcArea 'Call the vbscript function calcArea = calcCircleArea(7) msgbox "The area of the circle is " & calcArea Function calcCircleArea(radius) dim pi, a pi = 22/7 a = pi*radius*radius calcCircleArea = a End Function
Output(Message Box): The area of the circle is 154
ByRef and ByVal Arguments in VBScript Procedures:
ByRef Argument – To keep the changes made in the argument even after the VBScript procedure is called, then we have to send the argument by reference(ByRef). If there is nothing is specified when passing arguments into the VBScript procedures, by default it’s treated as passed by reference. The keyword ByRef is used to pass the argument by reference.
Example of ByRef – Refer below code, here argument Counter has been passed by reference into the procedure. Initially, it’s defined with value four, and in the procedure, it’s incremented by 1. As the argument has been passed by reference, the value of the argument will be five after the function call.
Function incrementCounter(ByRef Counter) Counter = Counter +1 incrementCounter = Counter End Function Dim x myCounter=4 call incrementCounter(myCounter) msgbox myCounter Output => 5
ByVal Argument – When an argument is passed by value(ByVal), any modification, done on the argument’s value in the VBScript functions, will not persist after the function call. The keyword ByVal is used to pass the argument by value.
Example of ByVal – Refer below code, here argument Counter has been passed by value into the procedure. Initially, it’s defined with value four, and in the procedure, it’s incremented by 1. As the argument has been passed by value, the value of the argument will remain four after the function call.
Function incrementCounter(ByVal Counter) Counter = Counter +1 incrementCounter = Counter End Function Dim x myCounter=4 call incrementCounter(myCounter) msgbox myCounter Output => 4
VBScript Tutorial #4: VBScript Error Handling & Execute VBScript
VBScript errors are nothing but unhandled events which is not handled through the code. In vb scripting, if any events are encountered which are not handled through the codes, they are treated as VBScript errors.
Types of VBScript Errors:
The different types of VBScript errors are mentioned below –
Syntax Errors – The primary reasons for this type of VBScript errors are the incorrect structure of the script, typographical errors, incorrect spelling of keywords, syntactical errors. If the syntax errors exist, the script will not be executed at all. It appears during the compilation time of the script.
Logical Errors – This type of VBScript errors are appeared due to some unexpected events such as number, or date conversion failed due to inappropriate data. It appears while the test scripts are getting executed.
VBScript Error Handling:
It is not possible to handle all the unexpected VBScript errors through coding. So, it’s the first priority to handle VBScript errors. Primarily, there is one approach to handle the VBScript error in the scripts. This approach is the combination of using “On Error Resume Next” statements and Err Object’s property.
On Error Resume Next statements:
It was using the On-Error- Resume-Next statements; the exception can be handled partially. In this approach, the test script block should be started with “On Error Resume Next” statements. It signifies that in case of any error, execution will be skipped the current step and proceed with next step. After that, by checking the Error, we can handle the exceptions. Important keywords are –
· On Error Resume Next – In case of Error, VBScript will not raise an error message; instead of that, execution will move to the next step.
· On Error Goto 0 – It will work in reverse procedure with comparison to the above keyword. So, after execution of this step, VBScript will throw errors in case of any exceptions.
· Error.Description – It stores the error description.
· Error.Number – It holds the error number. For success, the value is zero.
· Error.Clear – It reset the Error object.
On Error Resume Next 'Vbscript statement 1 'Vbscript statement 1 . . . . . If error.number <> 0 then 'Checking for error '..Handle the error Else 'Success condition no need to handle anything Error.Clear End If On Error Goto 0
Approach to Execute VBScript:
There are different ways that are available to execute VBScripts. The most used approaches are –
- Execute VBScript through .vbs file
- Execute VBScript as a part of HTML web development
Execute VBScript through .vbs file: The steps to execute vbscript through .vbs file are –
- Write VBScript codes in a simple flat file. Any editor such as, note pad, Note Pad++, Edit Plus, etc can be used to create the file.
- Save the file with .vbs extension.
- Execute the file by double-clicking on it.
- Same file can be executed through the command prompt using the full file path. Command to execute vbs file is WScript “<file_name_with_path>”.
Through out this vbscript tutorial, we are using this approach to run all the demo vbscripts.
Execute VBScript as a part of the web page: In this approach, we need to write the VBScript codes with in tag <script> in the web page. The syntax will be looks like below –
<script type="text/vbscript"> - Vbscript statements … </script>
This approach is used while developing web pages using the classic ASP. Also same can be used in other technologies like PHP, JSP etc.
Tools Uses VBScript: The major tools which supports vbscripts are – UFT, Excel Macro, Visual Basic, Classic ASP (client side scripting)
In this VBScript article, we have learned about the learn about VBScript Procedures and VBScript Error Handling through this tutorial. We hope this tutorial has helped a lot to brush up on your basics of VB Scripting. To get more information on VBScripting, please click here.