VB Scripting
Short Description
Download VB Scripting...
Description
1. Intr Introd oduc ucti tion on VBScript stands for Visual Basic Script, a scripting language developed by Microsoft to be used with Microsoft products, mainly Internet Explorer.
VBScript Data types: VBScript has only one data type called a Variant. A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used. Because Variant is the only data type in VBScript, it is also the data type returned by all functions in VBScript. At its simplest, a Variant can contain either numeric or string information. A Variant behaves as a number when you use it in a numeric context and as a string when you use it in a string context. That is, if you are working with data that looks like numbers, VBScript assumes that it is numbers and does what is most appropriate for numbers. Similarly, if you're working with data that can only be string data, VBScript treats it as string data. You can always make numbers behave as strings by enclosing them in quotation marks (" " ).
a. Vari Varian antt Sub Subty type pes: s: The following table shows the subtypes of data that a Variant can contain. Subtype
Description
Empty Empty
Varia Variant nt is is unini uninitia tializ lized. ed. Value Value is is 0 for for nume numeri ric c varia variable bles s or a zerozerolength string ("") for string variables.
Null ull
Vari ariant inten ntenti tion onal allly con conta tain ins s no valid alid dat data.
Bool oolean ean
Conta ontain ins s eith either er True True or False or False..
Byte
Contains integer in th the range ange 0 to to 255 255.
Inte Intege gerr
Cont Contai ains ns int integ eger er in in the the rang range e -32, -32,76 768 8 to 32, 32,76 767. 7.
Currency Currency -922,337,2 -922,337,203,68 03,685,477 5,477.5808 .5808 to 922,33 922,337,203, 7,203,685,4 685,477.580 77.5807. 7. Long Long
Cont Contai ains ns inte integer ger in the the ran range ge -2,1 -2,147 47,4 ,483 83,6 ,648 48 to 2,14 2,147, 7,483 483,6 ,647. 47.
Single Single
Contai Contains ns a sing singlele-pre precis cision ion,, floa floatin ting-p g-poin ointt number number in the range range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
Double Double
Contai Contains ns a doub doublele-pre precis cision ion,, float floating ing-po -point int number number in in the range range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
Date (Time (Time))
VB Scripting
Contains Contains a number number that that represen represents ts a date date between between January January 1, 100 to December 31, 9999.
1
Stri String ng
Cont Contai ains ns a var varia iabl blee-le leng ngth th str strin ing g that that can can be be up to to appro approxi xima mate tely ly 2 billion characters in length.
Object
Contains an an ob object.
Error
Contains an an er error nu number.
You can use conversion functions to convert data from one subtype to another. In addition, the VarType function returns information about how your data is stored within a Variant.
Variable A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change during the time your script is running.
b. Declaring Variables: You declare variables explicitly in your script using the Dim statement. The Public statement, and the Private statement. For example: Dim DegreesFahrenheit You declare multiple variables by separating each variable name with a comma. For example: Dim Top, Bottom, Left, Right
c. Naming Restrictions: Variable names follow the standard rules for naming anything in VBScript. A variable name: • • • •
Must begin with an alphabetic character. Cannot contain an embedded period. Must not exceed 255 characters. Must be unique in the scope in which it is declared.
d. Assigning Values to Variables: Values are assigned to variables creating an expression as follows: The variable is on the left side of the expression and the value you want to assign to the variable is on the right. For example:
VB Scripting
2
Stri String ng
Cont Contai ains ns a var varia iabl blee-le leng ngth th str strin ing g that that can can be be up to to appro approxi xima mate tely ly 2 billion characters in length.
Object
Contains an an ob object.
Error
Contains an an er error nu number.
You can use conversion functions to convert data from one subtype to another. In addition, the VarType function returns information about how your data is stored within a Variant.
Variable A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change during the time your script is running.
b. Declaring Variables: You declare variables explicitly in your script using the Dim statement. The Public statement, and the Private statement. For example: Dim DegreesFahrenheit You declare multiple variables by separating each variable name with a comma. For example: Dim Top, Bottom, Left, Right
c. Naming Restrictions: Variable names follow the standard rules for naming anything in VBScript. A variable name: • • • •
Must begin with an alphabetic character. Cannot contain an embedded period. Must not exceed 255 characters. Must be unique in the scope in which it is declared.
d. Assigning Values to Variables: Values are assigned to variables creating an expression as follows: The variable is on the left side of the expression and the value you want to assign to the variable is on the right. For example:
VB Scripting
2
B = 200
e. Array Variables: Sometimes you want to assign more than one value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. The declaration of an array variable uses parentheses ( ) following the variable name. In the following example, an array containing containing 3 elements is declared: Dim names(2) The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a fixed-size array. You assign data to each of the elements of the array like this: names(0)="Tove" names(1)="Jani" names(2)="Stale" Similarly, the data can be retrieved from any element using the index of the particular array element you want. Like this: mother=names(0) You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows and 7 columns: dim table(4, 6)
2. CONS CONSTA TANT NTS: S: A constant is a meaningful name that takes the place of a number or string and never changes. VBScript defines a number of intrinsic constants a. Creat Creatin ing g Cons Consta tant nts s You create user-defined constants in VBScript using the Const statement; you can create string or numeric constants with meaningful names and assign them literal values. For example:
VB Scripting
3
Const MyString = "This is my string." Const MyAge = 49 Note that the string literal is enclosed in quotation marks (" "). Quotation marks are the most obvious way to differentiate string values from numeric values. You represent Date literals and time literals by enclosing them in number signs (#). For example: Const CutoffDate = #6-1-97# Here are the various categories of constants provided in VBScript and a brief description of each: Color Constants Date and Time Constants Date Format Constants Miscellaneous Constants MsgBox Constants String Constants Tristate Constants VarType Constants •
Color Constants Defines eight basic colors that can be used in scripting.
EXAMPLES Constant vbBlack VbRed •
Value &h00 &hFF
Description Black Red
Date and Time Constants Defines date and time constants used by various date and time functions.
EXAMPLES Constant vbSunday vbMonday •
Value Description 1 Sunday 2 Monday
Date Format Constants Defines constants used to format dates and times.
EXAMPLES Constant VbGeneralDate
VB Scripting
Value 0
Description Display a date and/or time. For real numbers, display a date and time. If there is no fractional part, display only a date. If there is no integer part, display time only. Date and time display is determined by your system settings.
4
VbLongDate
•
1
Display a date using the long date format specified in your computer's regional settings.
Miscellaneous Constants Defines constants that don't conveniently fit into any other category.
EXAMPLES Constant vbObjectError
•
Value -2147221504
Description User-defined error numbers should be greater than this value, for example, Err.Raise Number = vbObjectError + 1000
MsgBox Constants Defines constants used in the MsgBox function to describe button visibility, labeling, behavior, and return values.
EXAMPLES Constant vbOKOnly vbOKCancel vbAbortRetryIgnore •
Value 0 1 2
Description Display OK button only. Display OK and Cancel buttons. Display Abort, Retry, and Ignore buttons.
String Constants Defines a variety of non-printable characters used in string manipulation.
EXAMPLES Constant
Value
Description
vbCr
Chr(13)
Carriage return.
VbCrLf
Chr(13) & Chr(10)
Carriage return–linefeed combination.
vbFormFeed
Chr(12)
Form feed; not useful in Microsoft Windows.
•
Tristate Constants Defines constants used with functions that format numbers.
EXAMPLES Constant vbUseDefault vbTrue vbFalse
•
Value Description -2 Use default from computer's regional settings. -1 True 0 False
VarType Constants Defines the various Variant subtypes.
EXAMPLES
VB Scripting
5
Constant vbEmpty vbNull vbInteger
Value Description 0 Uninitialized (default) 1 Contains no valid data 2 Integer subtype
3. ERRORS: There are two types of error 1. 2.
VBScript Run-time Errors VBScript Syntax Errors
A) VB SCRIPT RUN-TIME ERRORS: VBScript run-time errors are errors that result when your VBScript script attempts to perform an action that the system cannot execute. VBScript run-time errors occur while your script is being executed; when variable expressions are being evaluated, and memory is being dynamic allocated. Error Number
Description
429
ActiveX component can't create object
507
An exception occurred
449
Argument not optional
17
Can't perform requested operation
430
Class doesn't support Automation
506
Class not defined
11
Division by zero
b) VB SCRIPT SYNTAX ERRORS: VBScript syntax errors are errors that result when the structure of one of your VBScript statements violates one or more of the grammatical rules of the VBScript scripting language. VBScript syntax errors occur during the program compilation stage, before the program has begun to be executed. Error Number 1053 1005 1021 1047 1014 1015 1010 1012
VB Scripting
Description Class initialize or terminate do not have arguments Expected '(' Expected 'Case' Expected 'Class' Expected 'End' Expected 'Function' Expected identifier Expected 'If'
6
1019 1020
Expected 'Loop' Expected 'Next'
4. Operators: Operators are used to "do operations" or manipulate variables and values.VBScript's many operators can be separated into four semi-distinct categories: • • • •
Math Operators Comparison Operators Logic Operators String Concatenation Operator
1. VBScript Operators: Math When you want to perform addition, subtraction, multiplication, and other mathematical operations on numbers and variables use the operators listed below. Operator
English
Example
Result
+
Add
8+7
15
-
Subtract
11-10
1
*
Multiply
7*8
56
/
Divide
8/2
4
^
Exponent
2^4
16
Mod
Modulus
15 Mod 10
5
2. VBScript Operators: Comparison When you want to compare two numbers to see which is bigger, if they're equal, or some other type of relationship use the comparison operators listed below. Common uses of comparison operators are within conditional statements like an If Statement or the condition check in a While Loop. Operator
English
Example
Result
=
Equal To
10 =1 4
False
>
Greater Than
10 > 14
False
<
Less Than
10 < 14
True
>=
Greater Than Or Equal To
10 >= 14
False
View more...
Comments