VBScript Tutorials: Learn VBScript From Scratch (15+ In-Depth Tutorials)

Introduction to Microsoft VBScript (Visual Basic Script): VBScript Tutorial #1

In today’s scenario, VBScript has turned out to be a very important topic, especially for beginners who wish to learn the scripting language or automation tools like QTP/UFT.

=> SCROLL DOWN to see the complete list of 15+ In-Depth VBScript Tutorials for Beginners

We will be covering a series of VB Scripting tutorials to help the developers and testers to learn VBScript quickly in an easily understandable way. 

VBscript tutorial

In my subsequent tutorials, I will cover other important topics of VBScript like Variables, Constants, Operators, Arrays, Functions, Procedures, Excel Objects, Connections Objects, etc., which in turn will create an easy understanding among the users for learning VBScript Programming Language easily and effectively.

***************************************************************

==> Learn VBScript with these 15 Tutorials  <==

Tutorial #1: Introduction to VBScript
Tutorial #2: Declaring and Using Variables in VBScript
Tutorial #3: Operators, Operator Precedence and Constants in VBScript
Tutorial #4: Using Conditional Statements in VBScript
Tutorial #5: Loops in VBScript  and also Part 2 here
Tutorial #6: Using Procedures and Functions in VBScript
Tutorial #7: Arrays in VBScript
Tutorial #8: Date Functions in VBScript
Tutorial #9: Working with Strings and Cookies in VBScript
Tutorial #10: Working with Events in VBScript
Tutorial #11: Working with Excel Objects in VBScript
Tutorial #12: Working with Connection Objects in VBScript
Tutorial #13: Working with Files in VBScript
Tutorial #14: Error Handling in VBScript
Tutorial #15: VBScript Interview Questions

***************************************************************

Initially, to start with I have chosen the first topic as ‘Introduction to VBScript’.

In this tutorial, I will discuss the basics of VBScript, thereby focusing more on its features, data types supported by it and coding methodologies along with the procedure to handle comments and formats in scripts.

What is VBScript?

As the name itself explains, VBScript is a ‘Scripting Language’. It is a lightweight case insensitive programming language developed by Microsoft. It is a subset of ‘Visual Basic’ or we may also say it as a lighter version of Microsoft’s programming language Visual Basic.

Most of us would have used Visual Basic during our course curriculum in our school or college. Visual Basic is an event-driven programming language and an Integrated Development Environment from Microsoft.

VBScript language is used in QTP for coding and running Automated Test Scripts. This is not a very difficult language to learn and with a little knowledge of basic programming skills and passion for writing code, anyone can learn this easily. For those who know Visual Basic, obviously, it is an added advantage.

Automation Testers, who want to create, maintain and execute the tests in QTP need to have basic programming skills using VBScript.

Now let’s move on to some basics topics that are revolving around VBScript in order to enable clear understanding and knowledge about VBScript.

Environments Supporting VBScript

Primarily, there are 3 Environments where VBScript can be run.

They include:

#1) IIS (Internet Information Server): Internet Information Server is the Microsoft’s Web Server.

#2) WSH (Windows Script Host): Windows Script Host is the hosting environment of the Windows Operating System.

#3) IE (Internet Explorer): Internet Explorer is a simple hosting environment that is most frequently used to run scripts.

Data Types in VBScript

Unlike other languages, VBScript has only 1 data type called Variant.

As this is the only data type that is used in VBScript, it’s the only data type that is returned by all the functions in the VBScript.

A variant data type can contain different kinds of information, depending on how it is used. For Example, If we use this data type in String context then this will behave like a String and if we use this in the Numeric context then this will behave like a Number. This is the speciality of a Variant data type.

A Variant data type can contain several subtypes. Now, let’s take a look at what all values/data will be returned if a particular subtype is used.

Subtypes include:

#1) Empty: This subtype indicates that the value will be 0 in case of Numeric Variables and “” for String Variables.

#2) Null: This subtype indicates that there is no valid data.

#3) Boolean: This subtype indicates that the resultant value will be either true or false.

#4) Byte: This subtype exhibits that the resultant value will lie in the range between 0 to 255 i.e. the result will be from any value ranging from 0 to 255.

#5) Integer: This subtype shows that the resultant value will lie in the range between -32768 to 32767 i.e. the result will be from any value ranging from -32768 to 32767

#6) Currency: This subtype indicates that the resultant value will lie in the range between -922,337,203,685,477.5808 to 922,337,203,685,477.5807 i.e. the result will be from any value ranging from -327-922,337,203,685,477.5808 to 922,337,203,685,477.5807.

#7) Long: This subtype shows that the resultant value will lie in the range from -2,147,483,648 to 2,147,483,647 i.e. result will be from any value in between -2,147,483,648 to 2,147,483,647.

#8) Single: This subtype exhibits that the resultant value will be from any value in between -3.402823E38 to -1.401298E-45 in case of negative values.

And for positive values, the result will be from any value in between 1.401298E-45 to 3.402823E38.


#9) Double: This subtype indicates that the resultant value will be from any value in between -1.79769313486232E308 to 4.94065645841247E-324 in case of negative values.

And for positive values, the result will be from any value in between 4.94065645841247E-324 to 1.79769313486232E308.

#10) Date (Time): This subtype will return a number which will represent a date value in between January 1, 100 to December 31, 9999

#11) String: This subtype will return a variable length string value which can approximately be up to 2 billion characters in length.

#12) Object: This subtype will return an object.

#13) Error: This subtype will return an error number.

How to Create a Simple VBScript?

In order to create a VBScript, there are only 2 things required.

They are:

  • Text Editors like Notepad++ or even Notepad to write the VBScript Code.
  • IE (good to have IE6 or above) to run the VBScript Code.

Now, let’s see few VBScript Code for clarity purpose but before that, it is important to know where can the Scripts be inserted in an HTML Page.

Where to Insert Scripts in an HTML Page?

VBScript provides you the liberty to place a code in any of the following sections:

  • Within the Header Tags i.e. in between <head> and </head>.
  • Within the Document’s Body i.e. between <body> and </body> tags.

First VBScript Code in HTML:

Now, let’s take a simple example to understand how VBScript code can be written inside HTML tags.

<html>
<head>
<title> Testing VBScript Skills </title>
</head>
<body>
<script type=”text/vbscript”>
variable1 = 1
variable2 = 2
output = (variable1 + variable2) / 1
document.write (“resultant from the above equation is ” & output)
</script> 
</body>
</html>

Note: Whatever is put inside the brackets of ‘document.write’, will be displayed as an output on the display page.

The Output of this program is: resultant from the above equation is 3

After completing the code, you can save this in a file and give a file name as anyfilename.html.

To run, just open this file in IE.

Important to Know:

We have just seen the implementation of VBScript code in HTML file. However, VBScript in QTP is not placed inside the HTML tags. It is saved with an extension ‘.vbs’ and is executed by the QTP Execution Engine.

To understand the practical implementation of VBScript in terms of QTP, you must have knowledge of variables, constants, etc. and I will cover that in my upcoming tutorials and for time being, I just want to show you the VBScript code with the concept of an external file.

VBScript in External File:

<html>
<head>
<script type=”text/vbscript” src=”nameofthefile.vbs”> </script>
</head>
<body>
variable1 = 22
variable2 = 21
subtraction = variable1 - variable2
document.write (“subtraction of 2 numbers is” & subtraction)
</body>
</html>

To access this code from an external source, save this code in a text file with an extension “.vbs”.

How Comments are Handled in VBScript

It is considered to be a good programming practice to include comments in the Scripts for better readability and understanding purposes.

There are 2 ways in which Comments can be handled in a VBScript:

#1) Any Statement that starts with a Single Quote (‘) is treated as a comment:

<html>
<head>
<script type=”text/vbscript”</script>
</head>
<body>
‘let’s do subtraction of 2 numbers 
variable1 = 11
variable2 = 10
subtraction = variable1 - variable2
document.write (“subtraction of 2 numbers is” & subtraction)
</body>
</html

Single Quote

#2) Any Statements that start with the keyword REM are treated as Comments.

<html>
<head>
<script type=”text/vbscript”></script>
</head>
<body>
REM let’s do subtraction of 2 numbers           
variable1 = 11
variable2 = 10
subtraction = variable1 - variable2
document.write (“subtraction of 2 numbers is” & subtraction)
</body>
</html>

REM

Formatting Tips:

#1) No Semicolon is required to end the particular statement in VBScript.

#2) If 2 or more lines are written in the same line in VBScript then Colons (:) act as a line separator.

Let’s understand this with the help of an Example:

<html>
<head>
<script type=”text/vbscript”></script>
</head>
<body>
variable1 = 11:variable2 = 21:variable3=34
</body>
</html>

Formatting Tips
#3)
If a statement is lengthy and required to break into multiple statements then you can use underscore “_”.

Let’s see its Example:

<html>
<head>
<script type=”text/vbscript”></script>
</head>
<body>
variable1 = 11
variable2 = 10
output = (variable1 - variable2) * 10
document.write (“output generated from the calculation”& _
“of using variable1 and variable2 with the multiplication of resultant”&_
from 10 is” & output)
</body>
</html>

Underscore

Reserved Keywords

In any language, there are a set of words which work as Reserved Words and they cannot be used as Variable names, Constant names or any other Identifier names.

Following is the list of Reserved Keywords in VBScript:

LoopNewNullParamArray

LSetNextOnPreserve
MeNothingOptionPrivate
ModNotOptionalPublic
RaiseEventReDimDemRem
ResumeRSetSelectSet
SharedSingleStaticStop
SubThenToTrue
TypeAndAsBoolean
CaseClassConstCurrency
DebugDimDoDouble
EachElseElselfEmpty
EventExitFalseFor
FunctionGoToIfImp
ImplementsInIntegerIs
UntilVariantWendWhile
WithXorEvalExecute
MsgBoxEraseExecuteGlobalOption
OptionExplicitRandomizeSendKeysLet
LetLikeLongType
EndEndIfEnumEqv

Conclusion

That’s it! This is all about the basic concepts that are involved in VBScript.

I hope that this VBScript tutorial would have a given you a brief overview and clear understanding of this scripting language with easy examples.

About the author: Thanks to STH Team member Varsha for helping us to compile this series. She is in the Software Testing field with core VBScripting skills and received many internal excellence awards.

NEXT Tutorial #2 => In my upcoming VBS tutorial, I will cover about Variables in VBScript.

Stay tuned for much more updates, and feel free to share your thoughts about this tutorial.




Recommended reading

5 comments ↓

#1 Sheetal Mahadik

Awesome tutorials! Thanks for this.
Can you publish similar for Python also?

#2 Tushar

Thanks. These are helpful

#3 Anannya ahmed

So useful tutorials! Thanks for provide this.

#4 Alexey Taran

good series. Have you covered date functions?

#5 Varsha

Yes.Date functions are covered in Tutorial#8.

Leave a Comment