VB Script Basics


Vb Script tool : To Execute VB script we need not any tool all you need to write code in notepad and save it with .vbs extension .Double click the saved file to execute your code

Variables : 
It is container and it is used for temporary storage

in VBS only way to declare variable is Dim 

Syntax : Dim <Variable name>

Example :
Dim a,b,c
a = 100
b = 200
c = 300

Note : Even though if you forget to declare variable you code will get execute . To avoid unwanted variables we need to declare

Declare and Initialize Variable in single statement :

We can declare and initialize variable in single statement by using " : "

Syntax: Dim <Variable Name> : <Variable Name> = <Variable Value>

Example : Dim a : a = 100

There are two ways to declare variable in VBs

1.Implicit declaration

Ex : a = 100

2.Explicit declaration

Ex : Dim a
a = 100


Option Explicit :

To avoid undeclared variables in code we use option explicit

Syntax : Option explicit

Comment :
To Comment code we use  ' single quotation

Example :  ' Code

OutPut :

In VBs we can check our out put by using Msgbox

Syntax : Msgbox <Variable to check holded value>

Example :
Dim name
name = "Learning Space"
Msgbox name

when you execute your code it will display "Learning Space" in window pop up


Rules to Declare Variables :

1.Always start with character or alphabet
2.Alphanumeric are allowed
3.Special character are not allowed except " _ "
4.Should not exceed more than 255 character
5.Should not declare Keyword as variables (Ex :Dim,Option explicit,date,msgbox..etc)

Input :

We can use Input box to take input from user

Syntax :  <Variable> = Input box("Your message")

Example :

Dim name
name = Input box("Please enter your name")
msgbox name
'what ever value you enter in input box will store in name variable

Constant :

Constant is a fixed value which we can't change during runtime

Syntax : Constant <Variable>  = <Variable Value>

Example :

Constant a = 100
Msgbox a

' in above code you can't change " a " value during run time

Data types :

Only one "Variant" type of data we use and there is Sub Type

Sub Type : 

1.Integer
2.Long
3.Double
4.Empty
5.Null
6.Date
7.Boolean
8.String
9.Arrey
10.Object
11.Error

Type Name : Type name we use to find the type of data variable holding

Example :
Dim a
a = 100 ' Integer
b= 1234567  'long
c = 10.3 ' double
d =     'Empty
e = #12/05/2016#  'date
f = true 'Boolean

Msgbox typename( a )   'Like this check for all variables

Operators :

1. Arithmetic Operators

Example:
+   for addition
Dim a,b
a = 7
b = 2
Msgbox a+b ' 9

-    for subtraction 
Dim a,b
a = 7
b = 2
Msgbox a-b ' 5

*   for multiplication
Dim a,b
a = 7
b = 2
Msgbox a*b ' 14

/    for division
Dim a,b
a = 7
b = 2
Msgbox a/b '3.5

\    for integer division  
Dim a,b
a = 7
b = 2
Msgbox a\b ' 3

Mod - for mod
Dim a,b
a = 7
b = 2
Msgbox a  mod b ' 1

^   for power
Dim a,b
a = 7
b = 2
Msgbox a^b '49


2. Logical Operator

Example : AND , OR , NOT

3.Relational

Example:
< less than
> greater than  
< = less than equal to
> = greater than equal to
= equal OR to compare two string
< > Not equal to

Conversion Function : Using conversion functions we can convert one data type into other

1.Cint
2.Clng
3.Cdbl
4.Cstr
5.Cdate
6.Cbool
7.Chr
8.Asc

Example :

Dim a,b
a = Cint(inputbox("Enter Value")
Msgbox typename(a)

in this code what ever you enter in input box it will convert it into integer

Control Statement :

Control statements are to verify conditions
If condition
Syntax :

1.If <Condition> Then
<Statement>
Else
<Statement>
End if

2.If <Condition> AND <Condition> Then
<Statement>
Else
<Statement>
End if

3.If <Condition> Then
<Statement>
ElseIf <Condition> Then
<Statement>
End if

Select Case

Syntax:

Select case (expression)
Case 1
---
---
Case 2
---
---
Case N
---
---
Case else
---
---
End Select

Example :
Dim color
Color = inputbox("enter color ")

Select case color
case "red"
msgbox "stop"
case "green"
msgbox "Go"
case "yellow"
msgbox "wait"
case else
msgbox "invalid color"

End select

Loops in VBs

1.For loop
2.Nested for loop
3.Do loops
4.For each next

For loop: If you know the iteration count then you will go for for loop
syntax :
for counter start number to end number

Next
Example :

Dim i,a
For i = 1 to 5
    a = a & "hi" & Vblf
Next
msgbox a

Output :
hi
hi
hi
hi
hi

In above code "a"initially having nothing once loop start it store "hi" in first iteration and at same time making cursor to next line by adding "Vblf"
"&" use for concatenate like this it will do for all 5 iteration

Nested For Loop: it is nothing but for inside for loop
Syntax :

for counter 1 start number to end number

     for counter 2 start number to end number

    Next
Next

Example :

Dim i,j,a
For i = 1 to 5
      For j = 1 to i
         a = a & "*"
     Next
     a = a & vblf
Next
msgbox a

output :
*
**
***
****
*****

Do Loops : If you don't know the iteration count then will go for Do Loop

Syntax :
1.
Do While (Condition)
----
----
Loop

Example :
Dim i
i = 1
Do While i < 5

   msgbox i
   i = i+1
Loop

2.
Do
---
---
Loop While (Condition)

Dim i
i=1
Do
   msgbox i
   i =i+1
Loop While i<=5

3.
Do Until : Opposite to Do while

Syntax:
Do Until (Condition)
---
---
Loop
Note : If condition is false then it will enter into loop

4.
DO
---
---
Loop until (Condition)

                                                         <    VBs String Functions   >

Stay tune ill post it shortly

Thanks for reading my post
Feel to comment your queries







Comments

  1. Please add more examples , it is really good and effective..thank you so much..

    ReplyDelete
    Replies
    1. Thanks Rajani for your feedback :) I'll share more examples

      Delete

Post a Comment

Popular posts from this blog

How To Run JMeter GUI Mode In Ubuntu Servers

SELENIUM

Sql Basics With Examples