Tuesday, 22 August 2017

MSGBOX

MSGBOX:

The MsgBox function displays a message box, waits for the user to click a button, and returns a value that indicates which button the user clicked.      
                       MsgBox(prompt[,buttons][,title][,helpfile,context]) 


Parameter Description

·         Prompt - A Required Parameter. A String that is displayed as a message in the dialog box. The maximum length of prompt is approximately 1024 characters. 
·         buttons - An Optional Parameter. A Numeric expression that specifies the type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box. If left blank, the default value for buttons is 0.
·         Title - An Optional Parameter. A String expression displayed in the title bar of the dialog box. If the title is left blank, the application name is placed in the title bar.
·         helpfile - An Optional Parameter. A String expression that identifies the Help file to use to provide context-sensitive help for the dialog box.
·         context - An Optional Parameter. A Numeric expression that identifies the Help context number assigned by the Help author to the appropriate Help topic. If context is provided, helpfile must also be provided.

Simply we can write above syntax as
MsgBox("Message you want to display in the box",Type of box/buttons you want to display,"Title of Box")
The Buttons parameter can take any of the following values which used to display different types of box.
·         0 vbOKOnly displays OK button only.
·         1 vbOKCancel displays OK and Cancel buttons.
·         2 vbAbortRetryIgnore displays Abort, Retry, and Ignore buttons.
·         3 vbYesNoCancel displays Yes, No, and Cancel buttons.
·         4 vbYesNo displays Yes and No buttons.
·         5 vbRetryCancel displays Retry and Cancel buttons.
·         16 vbCritical displays Critical Message icon.
·         32 vbQuestion displays Warning Query icon.
·         48 vbExclamation displays Warning Message icon.
·         64 vbInformation displays Information Message icon.
·         0 vbDefaultButton1 First button is default.
·         256 vbDefaultButton2 Second button is default.
·         512 vbDefaultButton3 Third button is default.
·         768 vbDefaultButton4 Fourth button is default.
·         0 vbApplicationModal Application modal. The current application will not work until the user responds to the message box.
·         4096 vbSystemModal System modal. All applications will not work until the user responds to the message box.
The above values are logically divided into four groups: The first group(0 to 5) indicates the buttons to be displayed in the message box. The second group (16, 32, 48, 64) describes the sytle of the icon to be displayed, the third group (0, 256, 512, 768) indicates which button must be the default, and the fourth group (0, 4096) determines the modality of the message box.

Ex: msgbox "Box Message",0,"Box Title"




msgbox "Box Message",3,"Box Title"


Change the "0" with any of these numbers above and check the output.

Examples of Implicit,Explicit Declarations

As we seen in earlier we have two types of declarations in VB script namely implicit,explicit.
In this post we will see some basic examples for those declaration types.

Implicit:

Ex 1: write the code in notepad or any editor

dim a
a=20
msgbox a

Now I am saving the file with the name example1.vbs.




The file will be saved like above.Now to run the file double click on the file then the result will be displayed in a dialogue box as shown below


In this way implicit way of declaration will work.

Implicit :

Now will see about the explicit declaration.As I said earlier in Explicit declaration variables are declared first before using it and we use “Option Explicit “in the first line of  the script. 

Ex 2: write the code in notepad or any editor

option explicit
dim a
a=20
msgbox a

When you try to run the file it will give output as 20 same like the above example.but when we use another variable b without declaring first then it will show an error

Ex 3:
option explicit
dim a,b
a=20
msgbox a

The above code wont show any error as we declared the variable b.now we will do some changes to the code

Ex 4:
option explicit
dim a
a=20
b=30
msgbox a

When you try to run the file it will give an error stating that Variable is undefined.Because this is explicit declaration and we are using variable b without declaring it first .

We will see about dim,msgbox,typename..etc in next post.

Monday, 21 August 2017

VB Script Placement

VB Script Placement in HTML File

VB Script code is included anywhere in an HTML document. But the most preferred way to include VB Script in your HTML file is as follows:

  • Script placed between <head>...</head> section.
  • Script placed between  <body>...</body> section.
  • Script placed between <body>...</body> and <head>...</head> sections.
  • Script placed in an external file and then include in <head>...</head> section.

VB Script Placement in QTP

  • VB Script is placed in QTP (Quick Test Professional) tool but it is not enclosed within HTML Tags. The Script File is saved with the extension .vbs and it is executed by Quick Test Professional execution engine.

Constants in VB Script

  • Constant is a named memory location used to hold a value that CANNOT be changed during the script execution.
  •  If a user tries to change a Constant Value, the Script execution ends up with an error.
  •  Constants are declared the same way the variables are declared.
  • While naming the constants, you need to be careful not to use the predefined VB Script constants.Otherwise it will throw an error.
  • The best preventive measure is to avoid names starting with vb because all VB Script predefined constants start with vb. 
  • We create user-defined constants in VBScript using the Const statement. Using the Const statement, we can create string or numeric constants with meaningful names and assign them literal values.
    • Const statement
Example:
Const MyString = "This is my string."
Const MyAge =25
Const CutoffDate = #6-1-97# 

  • Note that String literal is enclosed in quotation marks (" "). Date literals and time literals are Represented by enclosing them in number signs (#).
  • We declare multiple constants by separating each constant name and value with a comma.
 For example:
Const price= 100, city= "Bengaluru", x= 25

The Constant can be of type Public or Private.
 The Use of Public or Private is Optional.
 The Public constants are available for all the scripts and procedures while the Private Constants are available within the procedure or Class. 
One can assign any value such as number, String or Date to the declared Constant.

Vb Script Operators

An Operator works either on values or variables to perform some task.Operators are very important in programming  assign values to variables or perform tasks without operators is not possible.
There are mainly three kinds of operators in VB Script: 
  1. Arithmetic
  2. Comparison 
  3. Logical operators.
  4. Concatenation Operator

Arithmetic Operators :

  • + (addition) :The addition Operator is used to find the addition of given numbers,For example 8+2 is equal to 10
  • - (subtraction) :The subtraction Operator is used to find the subtraction of given numbers,For example 10-2 is equal to 8
  • * (multiplication) : The multiplication Operator is used to find the multiplication of given numbers,For example 4*2 is equal to 8
  • / (division) : The division operator is used to find the Quotient.For example 5/2 is equal to 2.
  • % (modulus) :The modulus operator is used to find the remainder after a division. For example, 20%3 is equal to 2
  • ^ (exponentiation) : The exponentiation operator is equivalent to “to the power of” in mathematics. For example, 2^3 is equal to 8.
  • & (concatenation) : The concatenation operator is used to concatenate two string values.For example, Visual&Basic is equal to VisualBasic

Comparison Operators:

Comparison operators are used to compare two values.
Comparision Operator always written a Boolean value,either TRUE or FALSE
  • == (Equal to ) :
  • <> (Not equal to)
  • <  (Less Than )
  • >  (Greater Than)
  • <=  (less than or Equal to )
  • >= (Greater than or Equal to )
 For example,if  you have two variables a and b with values a=4 and b=9 respectively, then the results for the following comparison will be like this:

a==b  will return false.
a<>b will return true.
a<b will return true.
a>b will return false.
a<=b will return true.
a>=b will return false.

Logical operators:

Logical operators are used to perform logical operations.
Logical Operator always written a Boolean value,either TRUE or FALSE


  • AND
  • OR
  • NOT 
  •  XOR


 For example,you have two variables a and b with values true and false respectively, then the results for the following logical operations will be like this:


a AND b  will return false.

a  OR b will return true.
NOT(a  OR b) will return false.
a  XOR b will return true.

Concatenation Operators

Concatenation operators are used to perform Concatenation  operations.
In Vb script we have following concatenation operators

+ : Adds two Values as Variable Values are Numeric

& : Concatenates two Values

If variable A holds 10 and variable B holds 20 then answer will be:
+ :30 
& :1020

Scope Of Variables

Variables can be declared using the following statements which determines the scope of the variables.The scope of the variables plays vital role when they are used with classes,procedures.

Scope of variables

  • DIM
  • PUBLIC
  • PRIVATE

DIM:

Variables declared using “Dim” keyword at procedure level are available within same procedures,in the same way variables declared in using dim keyword at class level are available to all procedures within same script.

Eg: dim var1

PUBLIC:

  •  Variables declared using public keyword are available to all the procedures across all the associated scripts.
  • Variable type Public is declared by using keyword public in place of dim keyword.
Eg: public var2

PRIVATE:

  • Variables declared using public keyword are have the scope within that script in which they are declared.
  • Variable type private is declared by using keyword private in place of dim keyword.

Eg: private var3

Examples for these scope of variables will be discussed in further posts.

Life Time of Variables:


  • The lifetime of a variable depends on how long it exists. 
  • The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running. 
  • At procedure level, a variable exists only as long as you are in the procedure. 


Click to know about Operators in Vb Script 

Data types in VB Script ( VARIANT )

Basically we will store numbers,names,date,time.. etc in different variables,so these numbers(Integer),name (String),Date( date),Time(Time) are known as data types.
In fact  VB Script we have only one type of data type Called VARIANT.
We declare variables in Vb Script using DIM keyword.
Ex. dim variable1;
Here variable1 is the type of VARIANT.
Variant is a special kind of data type which can hold different kinds of information.
These different categories of information that can be contained in a Variant are called sub types.

Variant Sub types:


  • Integer : Using 2 bytes to express signed integer in the range -32,768 to 32,767.
  • Single : Using 4 bytes to express real numbers in floating-point format ranging from -3.402823e38 to -1.401298e-45 for negative values, and from 1.401298e-45 to 3.402823e38 for positive value.
  • Double : Using 8 bytes to express real numbers in floating-point format ranging from -1.79769313486232e308 to -4.94065645841247e-324 for negative values, and from 4.94065645841247e-324 to 1.79769313486232e308 for positive values.
  • Long : Using 4 bytes to express signed integers ranging from -2,147,483,648 to 2,147,483,647.
  • String : Using 1 byte per character to express a sequence of characters that can be up to approximately 2 billion characters.
  • Boolean : Using 2 bytes to contain either True or False.
  • Empty : A special subtype to represent a variable that has not been assigned with any value yet.
  • Nothing
  • Null : A special subtype to represent a variable assigned with a null value.
  • Object :  A special subtype to represent a reference to an object.
  • Error : A special subtype to represent an error number.
  • Date : A special subtype to represent  the date.
  • Time: A special subtype to represent  the time.
  • Byte: Using 1 byte to express integer in the range 0 to 255.
  • Currency: Using 8 bytes to express real numbers in decimal format ranging from -922,337,293,685,477.5808 to 922,337,293,685,477.5807

Click to know about Scope of variables in Vb Script

Friday, 18 August 2017

Variable Declaration in VB Script

In Vb Script we have two types of declarations

  1. Implicit
  2. Explicit
  • Implicit: In this variables are used without declaration.
  • Explicit: In Explicit variables are declared first before using it. In Explicit ,if variables are not declared first then error will be thrown.In explicit declaration we use “Option Explicit “in the first line of  the script.
In large programs it is good practice to use explicit declaration,as it will prevent duplication of variables. 

Declaring Variables:

  • Declaring variables is same as creating variables because you are instructing the computer to reserve memory space.
  • We can name the variable the way you want. 
  • It can be any name  as your wish . 
  • Providing clear and meaningful names to variables is considered as a good programming practice. 
  • We can declare variables in vb script using Dim,public,private statements.
  • We can declare multiple variables by separating each variable name with a comma
Ex : Dim name ;
public name
private name

Assigning Values to the Variables

Values are assigned similar to an algebraic expression. The variable name on the left hand side followed by an equal to (=) symbol and then its value on the right hand side.

Rules:

·         The numeric values should be declared without double quotes. Ex: a=10
·         The String values should be enclosed within doublequotes(") Ex: name="vbscript"

·         Date and Time variables should be enclosed within hash symbol(#) Ex: date=#08/22/2017#  time=#01.11.33 PM#

There will be different scope for variables declared using dim,public,private.That we will discuss further.

Variables,Naming Convention for variables in VB Script

Variables are used to hold a value or an expression.Its keeps some value in the memory.Whenever you have a piece of data to work with, you will declare a variable.

For example, if you have to store names of students or roll number of student, you will be using variables like  student_name or roll_number

As like every language Vb script also have rules for naming convention for variables.


Naming Convention for variables in VB Script :

Variable names follow the standard rules for naming anything in VB Script.Those are
  1. Always first character of variable should be alpha character.
  2. we cant use special character as variable names except underscore ( _ ) .
  3. Keywords are not allowed to use as variable name like dim,break,if..etc.
  4. Variable name should not exceed 255 characters.
  5. Must be unique in the scope in which it is declared.




VB Script Introduction


  • Vb Script stands for visual basic script.
  • It is developed by Microsoft.
  • It is a client side scripting language.
  • It comes by default with Operating system(windows).
  • Vb script is primary scripting language for QTP(Quick Test Proffessional),Which is an automation tool.
  • It is also used in AUTOCAD
  • As vb script is the scripting language it cannot run on its own,so we need programming language to host it.
  • Vb Script uses .vbs as extension.

Execution environment of Vb script:


we have 3 environments where VB Script can run.
  1. IIS (Internet Information Server) – Microsoft’s web server
  2. WSH (Windows Script Host)–The native hosting environment of  the Windows OS which is used mostly by Windows System administrators for automating  the Windows Desktop.
  3. IE (Internet Explorer)– The simplest hosting environment we can use to run VBScripts.

Features of Vb Script:


  • Vb script uses .vbs as extension.
  • VB Script is a light weight scripting language
  • VB Script is case insensitive. It has a very simple syntax, easy to learn and easy to implement.
  • It uses interpreter while execution.

Disadvantage of VB Script

  •  The main disadvantage of VB Script is that most browsers except Internet Explorer will not process VB Script code.
  • Means,if your site has visitors who use a web browser other than Internet Explorer like Chrome, Firefox  or Opera, then VBScript will not be useful.
  • Moreover, VBScript will not run on computers that run on operating systems other than Microsoft Windows including Linux, Mac etc.
  • Like any other scripting language, VBScript has gone through many changes over the years.
  • Now, VB Script is used as the default scripting language of ASP and QTP.

Editor for VB Script:

  • The default editor for Vb script is Notepad.
  • Vb script is saved with .vbs extension.
  • When we use notepad as editor WHS provide execution part.
  • As notepad doesn’t provide debugging We can also use other editors like notepad ++, editplus  as editor for vb script.

Click To know about Variables in Vb script