Sunday, February 2, 2020

VBScript tutorial 1 | Introduction and Variables

This is the first tutorial in the VBScript Tutorials for Beginners. This VBScript beginner tutorial explains what is VBScript. Please view the VBScript tutorial 1 or read on...VBScript is a scripting language that is useful in automation of administrative tasks (e.g. sending emails, copying files or listing the installed software in a computer), web server scripting, test automation (in UFT One and TestComplete). A VBScript program can be run directly, without the need of compiling it first. VBScript programs run on all Windows computers. The good thing is that VBScript is simple and therefore, easy to learn.

Let us start with learning VB Script variables. A variable is a name or a reference for a memory location that stores some data. We can define our own variable names with a VBScrpt Dim statement. But a variable name has to begin with an alphabet and be less than 256 characters in length. Variables names are not case-sensitive. Some examples of VBScript variable names are num1, x, sum, strCustomer, address_1. Now, let us see the VBScript code to add two numbers. You can use any text editor to type the VB script code

' This is a comment. A comment starts with a single quote.
' VBScript code
Dim x, y, z
' Assign the right hand side value i.e. 1 to variable, x
x = 1
' Assign the value 2 to variable, y
y = 2
' Assign the value of expression x + y to variable, z
z = x + y
' Display the value of z i.e. display 3
MsgBox z

Next, we need to save the code in a text file with .vbs file extension e.g. sum.vbs. There are multiple ways to execute the VBscript:
1) In File Explorer, we can double-click the script.
2) We can open the command window (cmd command). Then, change directory (cd command) to the folder that has our script. Type the script name e.g. sum.vbs and press the Enter key.

Instead of fixed x and y values, we can ask for the numbers from the user. We can modify the above VBScript to the following:

' VBScript code
' Option Explicit mandates defining variables names before use.
Option Explicit
' Define variable names that are meaningful to the reader.
Dim firstNumber, secondNumber, sum
' Assign the firstNumber using inputBox function
' inputBox shows a dialog box with the given prompt and title
firstNumber = inputBox ("Enter a number", "First Number")
' CInt function converts the string entered by the user to an integer value
firstNumber = CInt(firstNumber)
' The last argument given to the inputBox function is the default value
secondNumber = CInt(inputBox ("Enter another number", "Second Number", 0))
sum = firstNumber + secondNumber' Display the value of the sum variable
MsgBox "The sum is " & sum

Want to learn more details and how to debug your VBScript? Please view my VBScript tutorial 1. Thank you.

1 comment:

  1. For many Web-application developers, VBScript is the most important programming language.Certainly, the most important new feature of Version 5.0 is the ability to use the Class statement to create your own class objects.Thanks to share blog on VB script tutorial.We are a global IT solutions company provide full-cycle services in the areas of software development, web-based enterprise solutions, web application and portal development. website development company in pune | website developer in pune