Sie sind auf Seite 1von 34

HP Global Soft Pvt ltd

Agenda

Introduction to VBScript
Executing VBScript
Variables
Data Types
Data types Functions
Operators
Msgbox and Inputbox functions

2
3
Introduction to VBScript
Introduction What is VBScript?
VBScript is a Powerful Interpreted Scripting language that brings the
Active scripting to a variety of environments, both Client-Side and Server-
Side.

Difference Between Compiler and Interpreter

A Compiler first takes in the entire program, checks for errors, compiles it and
then executes it

An Interpreter does this line by line, so it takes one line, checks it for errors
and then executes it

Difference Between Server-Side and Client-Side

Server-Side code runs on the server. Client-Side code runs
in the clients-browser.

VBScript is a subset of Microsofts Visual Basic.

VBScript is Interpreted at Runtime.

4
Introduction Advantages of Runtime Compilation?
Can be embedded with other types of code.
5
Introduction What can you do with VBScript?
Can do Client Side validations.
Reduces load on web server by performing some validations at
Client Side
Can fetch data from Database
Introduction Advantages of using VBScript?
Good Platform coverage.
VBScript source implementation from Microsoft is completely free of
charge.
6
Introduction Disadvantages of Runtime Compilation?
Compilation will be slower.
Transparent code.
Syntax errors arent caught until runtime.
Executing VBScript
Windows Script Host
The WSH is just one host that allows you to run VBScript.This host
allows you to run VBScript directly from within the windows operating
system.

Saving VBScript
Open the Notepad or WordPad and write a VBScript program and save
the file with .vbs extension.

Ex: filename.vbs

Once you saved the script, execute the script by double clicking on the
Script file.




7
Variables
What are Variables?
A variable is a virtual container in the computer's memory that's used
to hold information.

How do I create variable?
When you create a variable, you have to give it a name. That way, when
you need to find out what's contained in the variable, you use its name
to let the computer know which variable you are referring to.

What are the two ways to create a variable?
Explicit Method
Implicit Method
8
Variables
Explicit Method
The first way, called the explicit method, is where you use the Dim
keyword to tell VBScript you are about to create a variable. You then
follow this keyword with the name of the variable. If, for example, you
want to create a variable called Quantity, you would enter

Dim Quantity

If you want to create more than one variable, you can put several on
the same line and separate them by commas, such as

Dim X,Y,Z


Note: Declaring variables in VBScript is Optional














9
Variables
Implicit Method
The second way to create a variable is called the implicit method. In this
case, you don't need to use the Dim statement to create a variable. You can
just start using it in your code and VBScript creates it automatically. If, for
example, you want to store the quantity of an item the user is ordering, you
can simply enter

Quantity = 10

What is Option Explicit Statement?
Option explicit statement should be declared at the first line of the code.
Once you declare the statement option explicit, this tells VBScript that our
code requires that all the variables be explicitly declared before they can be
used.







10
Variables
Rules for naming Variables
The first rule is that the name must begin with a letter.
Example : 1) strName
2) Some_Thing
illegal variable names:
1) +strname
2) 99RedBalloons

The second rule is Numbers and underscore(_)characters can be used within
the variable name, but all other non-alphanumeric characters are illegal
Example: 1) lngposition99
2) world1_world2_
Illegal variable names:
1) str&Name
2) SomeThing@

The length of a variable name cannot exceed 255 characters.





11
Data types
What are Data Types?
Data Types help a Programming Language compiler generate the proper
machine instructions from the code that a Programmer types in.

Different Data Types
1. Boolean
2. Byte
3. Integer
4. Long
5. Single
6. Double
7. Date(time)
8. String
9 Object
10.Error
11.empty
12 Null
12
Data Types
Boolean
You can set the Boolean data type to either true or false, which are represented
by -1 and 0, respectively, in VBScript. This subtype is useful when working with
variables where you want to determine or set a condition.

Byte
The byte data type can store an integer value between 0 and 255. It is used to
preserve binary data and store simple data that doesn't need to exceed this
range.

Integer
The integer data type is a number that cannot contain a decimal point. Integers
can range from -32,768 to +32,767.

Long
Variables of the long data type are also integers, but they have a much higher
range, -2,147,483,648 to 2,147,683,647 to be exact.
13
Data Types
Single
The single subtype represents floating-point, or decimal, values, which
means that numbers represented by this subtype include decimal
points.

Double
The Double is another floating-point data type, but this one has an
even larger range than the single data type.

Date(time)
The date subtype is often used because it places the date in a
predefined format that other functions in VBScript can act on.

String
The string data type is used to store alphanumeric data-that is,
numbers, letters, and symbols.
14
Data Types
Object
The object data type is a subtype used to reference entities such as
control or browser objects within a VBScript application or another
application.

Error
The error subtype is used for error handling and debugging purposes.

Empty
The empty subtype is used for variables that have been created but not
yet assigned any data. Numeric variables are assigned 0 and string
variables are assigned "" in this uninitialized condition.

Null
The null subtype refers to variables that have been set to contain no
data.
15
Data Types
Examples for VarType Return types
Dim Result
Dim Distance
Distance = 6
Result = VarType(Distance) ' Result returns 2
(integer)

Distance = 65535
Result = VarType(Distance) ' Result returns 3
(long)

Distance = 6/95
Result = VarType(Distance) ' Result returns 5
(double)

Distance = 6.5
Result = VarType(Distance) ' Result returns 5
(double)

Distance = "6"
Result = VarType(Distance) ' Result returns 8
(string)

Internal Representation Value
Empty 0
Null 1
Integer 2
Long 3
Single 4
Double 5
Currency 6
Date/time 7
String 8
OLE automation object 9
Error 10
Boolean 11
Variant 12
Non-OLE automation object 13
Byte 17
Array 8192
16
VarType Return types
Data Types- Functions
The Is Function
The Is function is useful when you want to check what type of data a
variable might hold initially. This is important because using a
conversion function on wrong type of data can cause runtime error.

Different Is functions in VBScript.
1) IsDate() It is used to check whether the user entered a valid date.
2) IsNumeric() - It is used to check whether the user entered a valid
Number.
3) IsEmpty() It returns a variant value of the Boolean subtype with
the value of True if the variable is empty, and false if not.
4) IsNull() This function is used to test for null value.
5) IsError This function contains either the result of the function or
an error number.


17
Operators
An operator acts on one or more operands when
comparing,assigning,concatenating,calculating and
performing logical operations.

Example: calculating the difference between two variables x
and y and store the result in z
z=X-Y

Here the assignment operator(=)to assign the difference
between x and y,which was found by using the subtraction
operator(-).


18
Operators
Different types of Operators:
The assignment(=)operator is used for assigning the value to the
variable.

The arithmetic operators are all used to calculate a numeric value.
Ex: +,-,Mod,*,/,

The concatenation operator (&) are used to concatenate expressions.

The comparison operators are used for comparing variables and
expressions. Ex:<>,<,>,<=,>=,Is

The Logical operators are used for performing logical operations on
expressions. Ex:Not,And,Or,Xor,Eqv,Imp.

The bitwise operators are used for comparing binary values bit-by bit.
19
Operators
Rules for + operators:
If both variables have the string subtype, then Vbscript will concatenate them.
Ex: Dim StrFirst=Hello
Dim StrSecond=Me
StrResult= StrFirst + StrSecond
The output of StrResult = HelloMe

If both variables have any of the numeric subtypes then VBscript will add them.
Ex: Dim FirstNumber = 20
Dim SecondNumber =10
Result= FirstNumber + SecondNumber
The output of Result = 30

If one of the variables has a numeric subtype and the other has the String
subtype, then Vbscript will attempt to add them. If the variable with the string
subtype does not contain the number, then a Type Mismatch error will occur.
Ex: Dim FirstNumber = 20
Dim Strfirst=Hello
Result= FirstNumber + Strfirst
The output of Result =Type Mismatch error

20
21
Detailed Explanation on MsgBox and InputBox
22
MsgBox Definition
The MsgBox function displays a message in a dialog box, waits for the user to click a button,
and returns an Integer indicating which button the user clicked
Syntax
MsgBox(prompt[, buttons] [, title] [, helpfile, context])

The MsgBox function syntax has these parts:

MsgBox
23
The buttons argument

The first group of values (05) describes the number and type of buttons displayed in the dialog
box.
The second group (16, 32, 48, 64) describes the icon style.
24
The Third Group (0, 256, 512, 768) determines which button is the default.
You can test which button the user clicked by comparing the return value of the Msgbox function with one of
these values:
25
Example 1 :

MsgBox "Hello there!"

MsgBox "The Last Name field must not be blank.", vbExclamation, "Last Name

MsgBox "The Last Name field must not be blank.", vbExclamation + vbOKOnly,"Last Name

MsgBox "A bad database error has occurred.", vbCritical,
_"UpdateCustomerTable

Example 2 :

Dim Test

Test = MsgBox ("Are you sure you want to quit?", vbYesNo + vbQuestion,"Quit")

If test = vbYes then
MsgBox "Yes Pressed"
Else
MsgBox "No Pressed"
End If
26
Example 3 :

Option Explicit

Dim intResponse

intResponse = MsgBox ("Are you sure you want to delete all of the rows in the Customer
table?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete")

If intResponse = vbYes Then
Msgbox "Delete Rows.."
Else
Msgbox "No.."
End If
27
Input Box Definition
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns
a string containing the contents of the text box
Syntax
stringvariable = InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])

The arguments to the InputBox function are described below:
Imp Note :If the user clicks OK or presses ENTER , the InputBox function returns whatever is
in the text box. If the user clicks Cancel, the function returns a zero-length string ("").
InputBox
28
Option Explicit

Dim StrInput

StrInput = InputBox(Please Enter your Name :")

msgbox StrInput
Example
VB Scripting Functions
Functions
1. abs ( number ) : returns the absolute value of the number.
dim h
h = abs ( number )
msgbox h

2. asc ( string ) : returns the ansi character code corresponding to the
first letter in a string.
dim h
h = asc ( A) returns 65.
msgbox h

29
VB Scripting Functions
3. chr ( character code ) returns the character associated with the
ANSI Character code.
dim h
h = chr ( 65 ) returns A
msgbox h


4. cos ( angle ) returns the cosine of an angle.
dim h
h = cos ( 45 )
msgbox h


30
VB scripting functions
5. Inputbox Displays a prompt in a dialog box , waits for the user to
input to text or click a button and returns the contents of the text box.
dim h
Inputbox ( enter the name)
msgbox h

6. Round Functions returns a number rounded to a specified number
of decimal places.
dim h,pi
pi = 3.14159
h = round ( pi, 2)
31
Vb Scripting Functions
7. Sqrt returns the square root of a number.
dim h
h = sqr ( 4 )
msgbox h.


8. Time returns the current system time.
dim mytime
mytime = time returns the current system time.


32
33
Q & A
34
End of session 1

Das könnte Ihnen auch gefallen