Professional Documents
Culture Documents
www.gcreddy.com
VB Script
www.gcreddy.com
1.0 Introduction
VBScript is a scripting language. A scripting language is a lightweight programming language. VBScript is a light version of Microsoft's programming language Visual Basic.
When a VBScript is inserted into a HTML document, the Internet browser will read the HTML and interpret the VBScript. The VBScript can be executed immediately, or at a later event. Microsoft Visual Basic Scripting Edition brings active scripting to a wide variety of environments, including Web client scripting in Microsoft Internet Explorer and Web server scripting in Microsoft Internet Information Service. 1.1 Windows Script Host (WSH) It is a Windows administration tool. WSH creates an environment for hosting scripts. That is, when a script arrives at your computer, WSH plays the part of the host it makes objects and services available for the script and provides a set of guidelines within which the script is executed. Among other things, Windows Script Host manages security and invokes the appropriate script engine Windows Script Host is built into Microsoft Windows 98, 2000, and Millennium Editions and higher versions. A Windows script is a text file. We can create a script with any text editor as long as we save our script with a WSH-compatible script extension (.js, vbs, or .wsf). The most commonly available text editor is already installed on our computer Notepad. We can also use your favorite HTML editor, VbsEdit, Microsoft Visual C++, or Visual InterDev. 1.2 Creating a script with Notepad 1. Start Notepad.
2. Write your script. For example purposes, type Msgbox "Hello VB Script" 3. Save this text file with a .vbs extension (instead of the default .txt extension). For
example, Hello.vbs 4. Navigate to the file you just saved, and double-click it. 5. Windows Script Host invokes the VB Script engine and runs your script. In the example, a message box is displayed with the message "Hello VB Script" 1.3 Hosting Environments and Script Engines Scripts are often embedded in Web pages, either in an HTML page (on the client side) or in an ASP page (on the server side). In the case of a script embedded in an HTML page, the engine component that interprets and runs the script code is loaded by the Web browser, such as Internet Explorer. In the case of a script embedded in an ASP page, the engine that interprets and runs the script code is built into Internet Information Services (IIS).
www.gcreddy.com
Windows Script Host executes scripts that exist outside an HTML or ASP page and that stand on their own as text files. 1.4 Available Script Engines Generally, we write scripts in either Microsoft JScript or VBScript, the two script engines that ship with Microsoft Windows 98, 2000 and Millennium Editions. We can use other script engines, such as Perl, REXX, and Python, with Windows Script Host. A stand-alone script written in JScript has the .js extension; a stand-alone script written in VBScript has the .vbs extension. These extensions are registered with Windows. When we run one of these types of files, Windows starts Windows Script Host, which invokes the associated script engine to interpret and run the file.
www.gcreddy.com
2.0 Comments
The comment argument is the text of any comment we want to include. 2.0 Purpose of comments: o o We can use comments for making the script understandable. We can use comments for making one or more statements disable from execution.
2.1 Syntax Rem comment (After the Rem keyword, a space is required before comment.) Or Apostrophe (') symbol before the comment 2.2 Comment/Uncomment a block of statements
o Select block of statement and use short cut key Ctrl + M (for comment) o Select comment block and use short cut key Ctrl + Shift + M (for uncomment)
2.3 Example
www.gcreddy.com
3.4 Scope of Variables o o o A variable's scope is determined by where you declare it. When we declare a variable within a procedure, only code within that procedure can access or change the value of that variable If we declare a variable outside a procedure, we make it recognizable to all the
www.gcreddy.com
procedures in our script. This is a script-level variable, and it has script-level scope. 3.5 Life Time of Variables o o o 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.
3.6 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: B = 200 City = Hyderabad 3.7 Scalar Variables and Array Variables A variable containing a single value is a scalar variable. A variable containing a series of values, is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses () following the variable name. Example: Dim A(3) Although the number shown in the parentheses is 3, all arrays in VBScript are zero-based, so this array actually contains 4 elements. We assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 10, data can be assigned to the elements of an array as follows: A(0) A(1) A(2) A(3) = = = = 256 324 100 55
Similarly, the data can be retrieved from any element using an index into the particular array element you want. For example: SomeVariable = A(4) Arrays aren't limited to a single dimension. You can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions. Arrays aren't limited to a single dimension. We can have as many as 60 dimensions,
www.gcreddy.com
although most people can't comprehend more than three or four dimensions. In the following example, the MyTable variable is a two-dimensional array consisting of 6 rows and 11 columns: Dim MyTable(5, 10) In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns. 3.8 Dynamic Arrays We can also declare an array whose size changes during the time our script is running. This is called a dynamic array. The array is initially declared within a procedure using either the Dim statement or using the ReDim statement. However, for a dynamic array, no size or number of dimensions is placed inside the parentheses. For example: Dim MyArray() ReDim AnotherArray() To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension. In the following example, ReDim sets the initial size of the dynamic array to 25. A subsequent ReDim statement resizes the array to 30, but uses the Preserve keyword to preserve the contents of the array as the resizing takes place. ReDim MyArray(25) ReDim Preserve MyArray(30) There is no limit to the number of times we can resize a dynamic array, although if we make an array smaller, we lose the data in the eliminated elements.
www.gcreddy.com
Subtype Empty Null Boolean Byte Integer Currency Long Single Double
Description Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables. Variant intentionally contains no valid data. Contains either True or False. Contains integer in the range 0 to 255. Contains integer in the range -32,768 to 32,767. -922,337,203,685,477.5808 to 922,337,203,685,477.5807. Contains integer in the range -2,147,483,648 to 2,147,483,647. Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values. Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values. Contains a number that represents a date between January 1, 100 to December 31, 9999. Contains a variable-length string that can be up to approximately 2 billion characters in length. Contains an object. Contains an error number.
We 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.
www.gcreddy.com
For QTP information & Scripts examples visit: 4.4 Concatenation Operators Operator 1) Addition Operator (+)
www.gcreddy.com
Description
Sums two numbers. If Then 1) Both expressions are numeric Add. 2) Both expressions are strings Concatenate. 3) One expression is numeric and the other is a string Add.
4.5 Logical Operators Operator 1) Not 2) And 3) Or 4) Xor 5) Eqv 6) Imp Description Performs logical negation on an expression. Performs a logical conjunction on two expressions. Performs a logical disjunction on two expressions. Performs a logical exclusion on two expressions. Performs a logical equivalence on two expressions. Performs a logical implication on two expressions. Syntax result= Not expression result= expression1 expression2 result= expression1 expression2 result= expression1 expression2 result= expression1 expression2 And Or Xor Eqv
6) Input/Output Operations
6.1 InputBox Function Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box. Example: Dim Input Input = InputBox("Enter your name") MsgBox ("You entered: " & Input) 6.2 MsgBox Function
10
www.gcreddy.com
Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked. Example: Dim MyVar MyVar = MsgBox ("Hello World!", 65, "MsgBox Example") ' MyVar contains either 1 or 2, depending on which button is clicked.
7) VB Script Constants
A constant is a meaningful name that takes the place of a number or string and never changes. 7.1 Creating Constants 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 Declares constants for use in place of literal values. Example: Const MyString = "This is my string." Const MyAge = 49 Const CutoffDate = #6-1-97# Note that String literal is enclosed in quotation marks (" "). Represent Date literals and time literals 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= Hyderabad, x= 27
11
www.gcreddy.com
8) Conditional Statements
We can control the flow of our script with conditional statements and looping statements. Using conditional statements, we can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript: 1) IfThenElse Statement 2) Select Case Statement 8.1 Making Decisions Using If...Then...Else The If...Then...Else statement is used to evaluate whether a condition is True or False and, depending on the result, to specify one or more statements to run. Usually the condition is an expression that uses a comparison operator to compare one value or variable with another. If...Then...Else statements can be nested to as many levels as you need. 8.1.1 Running a Statements if a Condition is True (single statement) To run only one statement when a condition is True, use the single-line syntax for the If...Then...Else statement. Dim myDate myDate = #2/13/98# If myDate < Now Then myDate = Now 8.1.2 Running Statements if a Condition is True (multiple statements) To run more than one line of code, we must use the multiple-line (or block) syntax. This syntax includes the End If statement. Dim x x= 20 If x>10 Then msgbox "Hello G.C.Reddy" msgbox "x value is: "&x msgbox "Bye Bye" End If 8.1.3 Running Certain Statements if a Condition is True and Running Others if a Condition is False We can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False.
12
For QTP information & Scripts examples visit: Example: Dim x x= Inputbox (" Enter a value") If x>100 Then Msgbox "Hello G.C.Reddy" Msgbox "X is a Big Number" Msgbox "X value is: "&X Else Msgbox "GCR" Msgbox "X is a Small Number" Msgbox "X value is: "&X End If 8.1.4 Deciding Between Several Alternatives
www.gcreddy.com
A variation on the If...Then...Else statement allows us to choose from several alternatives. Adding ElseIf clauses expands the functionality of the If...Then...Else statement so we can control program flow based on different possibilities. Example: Dim x x= Inputbox (" Enter a value") If x>0 and x<=100 Then Msgbox "Hello G.C.Reddy" Msgbox "X is a Small Number" Msgbox "X value is "&x Else IF x>100 and x<=500 Then Msgbox "Hello GCR" Msgbox "X is a Medium Number" Else IF x>500 and x<=1000 Then Msgbox "Hello Chandra Mohan Reddy" Msgbox "X is a Large Number" Else Msgbox "Hello Sir" Msgbox "X is a Grand Number" End If End If End If 8.1.5 Executing a certain block of statements when two / more conditions are True (Nested If...) Example: Dim State, Region State=Inputbox ("Enter a State") Region=Inputbox ("Enter a Region") If state= "AP" Then
13
For QTP information & Scripts examples visit: If Region= "Telangana" Then msgbox "Hello G.C.Reddy" msgbox "Dist count is 10" Else if Region= "Rayalasema" Then msgbox "Hello GCR" msgbox "Dist count is 4"
www.gcreddy.com
Else If Region= "Costal" Then msgbox "Hello Chandra mohan Reddy" msgbox "Dist count is 9" End End End End If If If If
8.2 Making Decisions with Select Case The Select Case structure provides an alternative to If...Then...ElseIf for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If...Then...Else statement, but it makes code more efficient and readable. Example: Option explicit Dim x,y, Operation, Result x= Inputbox (" Enter x value") y= Inputbox ("Enter y value") Operation= Inputbox ("Enter an Operation") Select Case Operation Case "add" Result= cdbl (x)+cdbl (y) Msgbox "Hello G.C.Reddy" Msgbox "Addition of x,y values is "&Result Result= x-y Msgbox "Hello G.C.Reddy" Msgbox "Substraction of x,y values is "&Result Case "mul" Result= x*y Msgbox "Hello G.C.Reddy" Msgbox "Multiplication of x,y values is "&Result Result= x/y Msgbox "Hello G.C.Reddy" Msgbox "Division of x,y values is "&Result Case "mod"
Case "sub"
Case "div"
14
www.gcreddy.com
Result= x mod y Msgbox "Hello G.C.Reddy" Msgbox "Mod of x,y values is "&Result Case "expo" Result= x^y Msgbox "Hello G.C.Reddy" Msgbox"Exponentation of x,y values is "&Result Msgbox "Hello G.C.Reddy" msgbox "Wrong Operation" End Select 8.3 Other Examples 8.3.1 Write a program for finding out whether the given year is a leap year or not? Dim xyear xyear=inputbox ("Enter Year") If xyear mod 4=0 Then msgbox "This is a Leap year" Else msgbox "This is NOT" End If 8.3.2 Write a program for finding out whether the given number is, Even number or Odd number? Dim num num=inputbox ("Enter a number") If num mod 2=0 Then msgbox "This is a Even Number" Else msgbox "This is a Odd Number" End If 8.3.3 Read two numbers and display the sum? Dim num1,num2, sum num1=inputbox ("Enter num1") num2=inputbox ("Enter num2") sum= Cdbl (num1) + Cdbl (num2) 'if we want add two strings conversion require msgbox ("Sum is " &sum) 8.3.4 Read P,T,R values and Calculate the Simple Interest? Dim p,t, r, si p=inputbox ("Enter Principle") t=inputbox ("Enter Time")
Case Else
15
www.gcreddy.com
r=inputbox ("Enter Rate of Interest") si= (p*t*r)/100 ' p= principle amount, t=time in years, r= rate of interest msgbox ("Simple Interest is " &si) 8.3.5 Read Four digit number, calculate & display the sum of the number or display Error message if the number is not a four digit number? Dim num, sum num=inputbox ("Enter a Four digit number") If Len(num) = 4 Then sum=0 sum=sum+num mod 10 num=num/10 num= left (num, 3) sum=sum+num mod 10 num=num/10 num= left (num, 2) sum=sum+num mod 10 num=num/10 num= left (num, 1) sum=sum+num mod 10 msgbox ("Sum is " &sum) else msgbox "Number, you entered is not a 4 digit number" End If 8.3.6 Read any Four digit number and display the number in reverse order? Dim num,rev num= inputbox("Enter a number") If len(num)=4 Then rev=rev*10 + num mod 10 num=num/10 num= left(num,3) rev=rev*10 + num mod 10 num=num/10 num= left(num,2) rev=rev*10 + num mod 10 num=num/10 num= left(num,1) rev=rev*10 + num mod 10 msgbox "Reverse Order of the number is "&rev Else End If 8.3.7 Read 4 subjects marks; calculate the Total marks and grade? (a) If average marks Greater than or equal to 75, grade is Distinction b) If average marks Greater than or equal to 60 and less than 75 , then grade is First msgbox "Number, you entered is not a 4 digit number"
16
www.gcreddy.com
c) If average marks Greater than or equal to 50 and less than 60 , then grade is Second d) If average marks Greater than or equal to 40 and less than 50 , then grade is Third e) Minimum marks 35 for any subject, otherwise 'no grade fail') Dim e,m,p,c, tot e=inputbox ("Enter english Marks") m=inputbox ("Enter maths Marks") p=inputbox ("Enter physics Marks") c=inputbox ("Enter chemistry Marks") tot= cdbl(e) + cdbl(m) + cdbl(p) + cdbl(c) msgbox tot If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=300 Then msgbox "Grade is Distinction" else If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=240 and tot<300 Then msgbox "Grade is First" else If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=200 and tot<240 Then msgbox "Grade is Second" else If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=160 and tot<200 Then msgbox "Grade is Third" else msgbox "No Grade, Fail" End End End End If If If If
8.3.8 Display Odd numbers up to n? Dim num,n n=Inputbox ("Enter a Vaule") For num= 1 to n step 2 msgbox num Next 8.3.9 Display Even numbers up to n? Dim num,n n=Inputbox ("Enter a Vaule") For num= 2 to n step 2 msgbox num Next
17
www.gcreddy.com
8.3.10 display natural numers up to n and write in a text file? Dim num, n, fso, myfile n= inputbox ("Enter any Value") num=1 For num= 1 to n step 1 Set fso= createobject ("scripting.filesystemobject") set myfile=fso.opentextfile ("E:\gcr.txt", 8, true) myfile.writeline num myfile.close Next 8.11 Display Natural numbers in reverse order up to n? Dim num,n n=Inputbox ("Enter a Vaule") For num=n to 1 step -1 msgbox num Next 8.12 Display Natural numbers sum up to n? (using For...Next Loop) Dim num, n, sum n= inputbox ("Enter a Value") sum=0 For num= 1 to n step 1 sum= sum+num Next msgbox sum 8.13 Display Natural numbers sum up to n? (using While...Wend Loop) Dim num, n, sum n= inputbox ("Enter a Value") While num <=cdbl (n) sum= sum+num num=num+1 Wend msgbox sum 8.14 Display Natural numbers sum up to n? (using Do...Until...Loop) Dim num, n, sum n= inputbox ("Enter a Value") sum=0 num=1 Do sum= sum+num num=num+1 Loop Until num =cdbl (n+1) msgbox sum 8.15 Write a Function for Natural Numbers sum up to n?
18
For QTP information & Scripts examples visit: Function NNumCou (n) Dim num, sum sum=0 For num= 1 to n step 1 sum= sum+num Next msgbox sum End Function
www.gcreddy.com
8.16 Verify weather the entered 10 digit value is a numeric value or not? Dim a,x,y,z,num num=Inputbox ("Enter a Phone Number") d1= left (num,1) d10=Right (num,1) d2=mid (num, 2, len d3=mid (num, 3, len d4=mid (num, 4, len d5=mid (num, 5, len d6=mid (num, 6, len d7=mid (num, 7, len d8=mid (num, 8, len d9=mid (num, 9, len
If isnumeric (d1) = "True" and isnumeric (d2) = "True" and isnumeric (d3) = "True" and isnumeric (d4) = "True"and isnumeric (d5) = "True"and isnumeric (d6) = "True"and isnumeric (d7) = "True"and isnumeric (d8) = "True"and isnumeric (d9) = "True"and isnumeric (d10) = "True" Then msgbox "It is a Numeric Value" else Msgbox "It is NOT Numeric" End If 8.17 Verify weather the entered value is a 10 digit value or not and Numeric value or not? (using multiple if conditions) Dim a,x,y,z,num num=Inputbox ("Enter a Phone Number") d1= left (num,1) d10=Right (num,1) d2=mid (num, 2, len d3=mid (num, 3, len d4=mid (num, 4, len d5=mid (num, 5, len d6=mid (num, 6, len d7=mid (num, 7, len d8=mid (num, 8, len d9=mid (num, 9, len
19
www.gcreddy.com
If isnumeric (d1) = "True" and isnumeric (d2) = "True" and isnumeric (d3) = "True" and isnumeric (d4) = "True"and isnumeric (d5) = "True"and isnumeric (d6) = "True"and isnumeric (d7) = "True"and isnumeric (d8) = "True"and isnumeric (d9) = "True"and isnumeric (d10) = "True" Then msgbox "It is a Numeric Value" End If End If If len (num) <> 10 Then Msgbox "It is NOT valid Number " End If
20
www.gcreddy.com
The following looping statements are available in VBScript: Do...Loop: Loops while or until a condition is True. While...Wend: Loops while a condition is True. For...Next: Uses a counter to run statements a specified number of times. For Each...Next: Repeats a group of statements for each item in a collection or each element of an array.
9.1 Using Do Loops We can use Do...Loop statements to run a block of statements an indefinite number of times. The statements are repeated either while a condition is True or until a condition becomes True. 9.1.1 Repeating Statements While a Condition is True Repeats a block of statements while a condition is True or until a condition becomes True a) Do While condition Statements --------------------Loop Or, we can use this below syntax: Example: Dim x Do While x<5 x=x+1 Msgbox "Hello G.C.Reddy" Msgbox "Hello QTP" Loop b) Do Statements
21
For QTP information & Scripts examples visit: --------------------Loop While condition Example: Dim x x=1 Do Msgbox "Hello G.C.Reddy" Msgbox "Hello QTP" x=x+1 Loop While x<5
www.gcreddy.com
9.1.2 Repeating a Statement Until a Condition Becomes True c) Do Until condition Statements --------------------Loop Or, we can use this below syntax: Example: Dim x Do Until x=5 x=x+1 Msgbox "G.C.Reddy" Msgbox "Hello QTP" Loop Or, we can use this below syntax: d) Do Statements --------------------Loop Until condition Or, we can use this below syntax: Example: Dim x x=1 Do Msgbox Hello G.C.Reddy Msgbox "Hello QTP" x=x+1 Loop Until x=5 9.2 While...Wend Statement Executes a series of statements as long as a given condition is True. Syntax:
22
For QTP information & Scripts examples visit: While condition Statements --------------------Wend Example: Dim x x=0 While x<5 x=x+1 msgbox "Hello G.C.Reddy" msgbox "Hello QTP" Wend 9.3 For...Next Statement
www.gcreddy.com
Repeats a group of statements a specified number of times. Syntax: For counter = start to end [Step step] statements Next Example: Dim x For x= 1 to 5 step 1 Msgbox "Hello G.C.Reddy" Next 9.4 For Each...Next Statement Repeats a group of statements for each element in an array or collection. Syntax: For Each item In array Statements Next Example: Dim a,b,x (3) a=20 b=30 x(0)= "Addition is "& a+b x(1)="Substraction is " & a-b x(2)= "Multiplication is " & a*b x(3)= "Division is " & a/b For Each element In x msgbox element
23
www.gcreddy.com
24
www.gcreddy.com
11.2 Function Procedures A Function procedure is a series of VBScript statements enclosed by the Function and End Function statements. A Function procedure is similar to a Sub procedure, but can also return a value. A Function procedure can take arguments (constants, variables, or expressions that are passed to it by a calling procedure). If a Function procedure has no arguments, its Function statement must include an empty set of parentheses. A Function returns a value by assigning a value to its name in one or more statements of the procedure. The return type of a Function is always a Variant. Syntax: Function Procedure name () Statements --------------------End Function Or Function Procedure name (argument1, argument2) Statements --------------------End Function Example: 1 Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function Example: 2 Function cal(a,b,c) cal = (a+b+c)
25
For QTP information & Scripts examples visit: End Function 11.3 Getting Data into and out of Procedures o o o o
www.gcreddy.com
Each piece of data is passed into our procedures using an argument. Arguments serve as placeholders for the data we want to pass into our procedure. We can name our arguments any valid variable name. When we create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure. Any arguments are placed inside these parentheses, separated by commas.
11.4 Using Sub and Function Procedures in Code A Function in our code must always be used on the right side of a variable assignment or in an expression. For example: Temp = Celsius(fDegrees) -OrMsgBox "The Celsius temperature is " & Celsius(fDegrees) & " degrees." To call a Sub procedure from another procedure, type the name of the procedure along with values for any required arguments, each separated by a comma. The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses. The following example shows two calls to the MyProc procedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing. Call MyProc(firstarg, secondarg) MyProc firstarg, secondarg Notice that the parentheses are omitted in the call when the Call statement isn't used.
26
www.gcreddy.com
2) Array Function Returns a Variant containing an array. Array(arglist) Example: Dim A A = Array(10,20,30) B = A(2) ' B is now 30.
Note A variable that is not declared as an array can still contain an array. Although a Variant variable containing an array is conceptually different from an array variable containing Variant elements, the array elements are accessed in the same way.
3) CBool Function Returns an expression that has been converted to a Variant of subtype Boolean.
27
For QTP information & Scripts examples visit: CBool(expression) The expression argument is any valid expression. Remarks
www.gcreddy.com
If expression is zero, False is returned; otherwise, True is returned. If expression can't be interpreted as a numeric value, a run-time error occurs. The following example uses the CBool function to convert an expression to a Boolean. If the expression evaluates to a nonzero value, CBool returns True; otherwise, it returns False. Dim A, B, Check A = 5: B = 5 ' Initialize variables. ' Check contains True.
4) CByte Function Returns an expression that has been converted to a Variant of subtype Byte. CByte(expression) The expression argument is any valid expression. Example of use Dim MyDouble, MyByte MyDouble = 125.5678 MyByte = CByte(MyDouble) ' MyDouble is a Double. ' MyByte contains 126.
5) CCur Function Returns an expression that has been converted to a Variant of subtype Currency. CCur(expression) The expression argument is any valid expression. The following example uses the CCur function to convert an expression to a Currency: Dim MyDouble, MyCurr
28
For QTP information & Scripts examples visit: MyDouble = 543.214588 MyCurr = CCur(MyDouble * 2) Currency (1086.4292). 6) CDate Function
www.gcreddy.com
Returns an expression that has been converted to a Variant of subtype Date. CDate(date) The date argument is any valid date expression. Example: MyDate = "October 19, 1962" MyShortDate = CDate(MyDate) MyTime = "4:35:47 PM" ' Define date. ' Convert to Date data type.
MyShortTime = CDate(MyTime)
7) CDbl Function Returns an expression that has been converted to a Variant of subtype Double. CDbl(expression) The expression argument is any valid expression. This example uses the CDbl function to convert an expression to a Double. Dim MyCurr, MyDouble MyCurr = CCur(234.456784) ' MyCurr is a Currency (234.4567). ' Convert result to a Double (19.2254576).
8) Cos Function Returns the cosine of an angle. Cos(number) The number argument can be any valid numeric expression that expresses an angle in radians. The following example uses the Cos function to return the cosine of an angle:
29
For QTP information & Scripts examples visit: Dim MyAngle, MySecant MyAngle = 1.3 ' Define angle in radians. ' Calculate secant.
www.gcreddy.com
Returns the current system date. Date The following example uses the Date function to return the current system date: Dim MyDate MyDate = Date ' MyDate contains the current system date.
30
www.gcreddy.com
31
www.gcreddy.com
We can add comments to our statements using an apostrophe ('), either at the beginning of a separate line, or at the end of a statement. It is recommended that we add comments wherever possible, to make our scripts easier to understand and maintain. 21.7 Spaces: We can add extra blank spaces to our script to improve clarity. These spaces are ignored by VBScript.
13) Errors
We have two types Errors in VB Script; they are VBScript Run-time Errors and VBScript Syntax Errors 13.1 VBScript Run-time Errors VBScript run-time errors are errors that result when our VBScript script attempts to perform an action that the system cannot execute. VBScript run-time errors occur while our script is being executed; when variable expressions are being evaluated, and memory is being dynamic allocated. Error Number 429 507 449 17 430 506 11 48 5020 5019 432 92 5008 51 505 481 5 5021 94 448 432 447 445 438 451 504 503 502 424 91 7 Description ActiveX component can't create object An exception occurred Argument not optional Can't perform requested operation Class doesn't support Automation Class not defined Division by zero Error in loading DLL Expected ')' in regular expression Expected ']' in regular expression File name or class name not found during Automation operation For loop not initialized Illegal assignment Internal error Invalid or unqualified reference Invalid picture Invalid procedure call or argument Invalid range in character set Invalid use of Null Named argument not found File name or class name not found during Automation operation Object doesn't support current locale setting Object doesn't support this action Object doesn't support this property or method Object not a collection Object not safe for creating Object not safe for initializing Object not safe for scripting Object required Object variable not set Out of Memory
32
For QTP information & Scripts examples visit: 28 14 6 35 9 5017 462 10 13 5018 500 458 450
www.gcreddy.com
Out of stack space Out of string space Overflow Sub or function not defined Subscript out of range Syntax error in regular expression The remote server machine does not exist or is unavailable This array is fixed or temporarily locked Type mismatch Unexpected quantifier Variable is undefined Variable uses an Automation type not supported in VBScript Wrong number of arguments or invalid property assignment
33
For QTP information & Scripts examples visit: 13.2 VBScript Syntax Errors
www.gcreddy.com
VBScript syntax errors are errors that result when the structure of one of our 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.
34
For QTP information & Scripts examples visit: Error Number 1052 1044 1053 1058 1057 1005 1006 1011 1021 1047 1025 1014 1023 1015 1010 1012 1046 1026 1049 1045 1019 1020 1050 1022 1024 1016 1017 1013 1018 1027 1028 1029 1030 1014 1039 1040 1013 1037 1038 1048 1042 1041 1051 1001 1054 1002 1055 1015
www.gcreddy.com
Description
Cannot have multiple default property/method in a Class Cannot use parentheses when calling a Sub Class initialize or terminate do not have arguments 'Default' specification can only be on Property Get 'Default' specification must also specify 'Public' Expected '(' Expected ')' Expected '=' Expected 'Case' Expected 'Class' Expected end of statement Expected 'End' Expected expression Expected 'Function' Expected identifier Expected 'If' Expected 'In' Expected integer constant Expected Let or Set or Get in property declaration Expected literal constant
Expected 'Loop' Expected 'Next' Expected 'Property' Expected 'Select' Expected statement Expected 'Sub' Expected 'Then' Expected 'To' Expected 'Wend' Expected 'While' or 'Until'
Expected 'While,' 'Until,' or end of statement Expected 'With' Identifier too long Invalid character Invalid 'exit' statement Invalid 'for' loop control variable Invalid number Invalid use of 'Me' keyword 'loop' without 'do' Must be defined inside a Class Must be first statement on the line Name redefined Number of arguments must be consistent across properties specification Out of Memory Property Set or Let must have at least one argument Syntax error Unexpected 'Next' Unterminated string constant
35
www.gcreddy.com
36