You are on page 1of 46

VBScript Introduction

What You Should Already Know


Before you continue you should have a basic understanding of the following:

HTML / XHTML

What is VBScript?

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 VBScript is only supported by Microsoft's browsers (Internet Explorer)

How Does it Work?


When a VBScript is inserted into an HTML document, Internet Explorer browser will read the HTML and interpret the VBScript. The VBScript can be executed immediately, or at a later event. VBScript only works in Microsoft browsers (Internet Explorer).

The HTML <script> tag is used to insert a VBScript into an HTML page.

Put a VBScript into an HTML Page


The example below shows how to use VBSript to write text on a web page:

Example (IE Only)


<html> <body> <script type="text/vbscript"> document.write("Hello World!") </script> </body> </html>

Example (IE Only)


<html> <body> <script type="text/vbscript"> document.write("<h1>Hello World!</h1>") </script> </body> </html>

Example Explained
To insert a VBScript into an HTML page, we use the <script> tag. Inside the <script> tag we use the type attribute to define the scripting language. So, the <script type="text/vbscript"> and </script> tells where the VBScript starts and ends: <html> <body> <script type="text/vbscript"> ... </script> </body> </html> The document.write command is a standard VBScript command for writing output to a page. By entering the document.write command between the <script> and </script> tags, the browser will recognize it as a VBScript command and execute the code line. In this case the browser will write Hello World! to the page: <html> <body> <script type="text/vbscript"> document.write("Hello World!") </script> </body> </html>

How to Handle Simple Browsers


Browsers that do not support scripting, will display VBScript as page content. To prevent them from doing this, the HTML comment tag should be used to "hide" the VBScript. Just add an HTML comment tag <!-- before the first VBScript statement, and a --> (end of comment) after the last VBScript statement, like this: <html> <body> <script type="text/vbscript"> <!-document.write("Hello World!") --> </script> </body> </html>

Where to Put the VBScript


VBScripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, or at a later event, such as when a user clicks a button. When this is the case we put the script inside a function or a sub procedure, you will learn about procedures in a later chapter.

Scripts in <head>
Put your functions and sub procedures in the head section, this way they are all in one place, and they do not interfere with page content.

Example (IE Only)


<html> <head> <script type="text/vbscript"> function myFunction() alert("Hello World!") end function </script> </head> <body onload="myFunction()"> </body> </html>
Try it yourself

Scripts in <body>
If you don't want your script to be placed inside a function, and especially if your script should write page content, it should be placed in the body section.

Example (IE Only)


<html> <head> </head> <body> <script type="text/vbscript"> document.write("This message is written by VBScript") </script> </body> </html>
Try it yourself

Scripts in <head> and <body>


You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section.

Example (IE Only)


<html> <head> <script type="text/vbscript"> function myFunction() alert("Hello World!") end function </script> </head> <body> <button onclick="myFunction()">Click me</button> <script type="text/vbscript"> document.write("This message is written by VBScript") </script> </body> </html>
Try it yourself

Using an External VBScript


If you want to run the same VBScript on several pages, without having to write the same script on every page, you can write a VBScript in an external file. Save the external VBScript file with a .vbs file extension. Note: The external script cannot contain the <script> tag! To use the external script, point to the .vbs file in the "src" attribute of the <script> tag:

Example
<html> <head> <script type="text/vbscript" src="ex.vbs"></script> </head> <body> </body> </html>

Do You Remember Algebra from School?


Do you remember algebra from school? x=5, y=6, z=x+y Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above to calculate the value of z to be 11? These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).

VBScript Variables
As with algebra, VBScript variables are used to hold values or expressions. A variable can have a short name, like x, or a more descriptive name, like carname. Rules for VBScript variable names:

Must begin with a letter Cannot contain a period (.) Cannot exceed 255 characters

In VBScript, all variables are of type variant, that can store different types of data.

Declaring (Creating) VBScript Variables


Creating variables in VBScript is most often referred to as "declaring" variables. You can declare VBScript variables with the Dim, Public or the Private statement. Like this: Dim x Dim carname Now you have created two variables. The name of the variables are "x" and "carname". You can also declare variables by using its name in a script. Like this: carname="Volvo" Now you have also created a variable. The name of the variable is "carname". However, this method is not a good practice, because you can misspell the variable name later in your script, and that can cause strange results when your script is running. If you misspell for example the "carname" variable to "carnime", the script will automatically create a new variable called "carnime". To prevent your script from doing this, you can use the Option Explicit statement. This statement forces you to declare all your variables with the dim, public or private statement.

Put the Option Explicit statement on the top of your script. Like this: Option Explicit Dim carname carname=some value

Assigning Values to Variables


You assign a value to a variable like this: carname="Volvo" x=10 The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "carname" has the value of "Volvo", and the variable "x" has the value of "10".

Lifetime of Variables
How long a variable exists is its lifetime. When you declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared. If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.

VBScript Array Variables


An array variable is used to store multiple values in a single variable. In the following example, an array 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) Asign data to a two-dimensional array:

Example (IE Only)


<html> <body> <script type="text/vbscript"> Dim x(2,2) x(0,0)="Volvo" x(0,1)="BMW" x(0,2)="Ford" x(1,0)="Apple" x(1,1)="Orange" x(1,2)="Banana" x(2,0)="Coke" x(2,1)="Pepsi" x(2,2)="Sprite" for i=0 to 2 document.write("<p>") for j=0 to 2 document.write(x(i,j) & "<br />") next document.write("</p>") next </script> </body> </html>

VBScript has two kinds procedures:

Sub procedure Function procedure

VBScript Sub Procedures


A Sub procedure:

is a series of statements, enclosed by the Sub and End Sub statements can perform actions, but does not return a value can take arguments without arguments, it must include an empty set of parentheses ()

Sub mysub() some statements End Sub or Sub mysub(argument1,argument2) some statements End Sub

Example (IE Only)


Sub mysub() alert("Hello World") End Sub
Try it yourself

VBScript Function Procedures


A Function procedure:

is a series of statements, enclosed by the Function and End Function statements can perform actions and can return a value can take arguments that are passed to it by a calling procedure without arguments, must include an empty set of parentheses () returns a value by assigning a value to its name

Function myfunction() some statements myfunction=some value End Function or Function myfunction(argument1,argument2) some statements myfunction=some value End Function

Example (IE Only)


function myfunction() myfunction=Date() end function
Try it yourself

How to Call a Procedure


There are different ways to call a procedure. You can call it from within another procedure, on an event, or call it within a script.

Example (IE Only)


Call a procedure when the user clicks on a button: <body> <button onclick="myfunction()">Click me</button> </body>
Try it yourself

Procedures can be used to get a variable value: carname=findname() Here you call a Function called "findname", the Function returns a value that will be stored in the variable "carname". Function procedures can calculate the sum of two arguments:

Example (IE Only)


Function myfunction(a,b) myfunction=a+b End Function document.write(myfunction(5,9))
Try it yourself

The function "myfunction" will return the sum of argument "a" and argument "b". In this case 14. When you call a procedure you can use the Call statement, like this: Call MyProc(argument) Or, you can omit the Call statement, like this:

MyProc argument

Conditional Statements
Conditional statements are used to perform different actions for different decisions. In VBScript we have four conditional statements:

If statement - executes a set of code when a condition is true If...Then...Else statement - select one of two sets of lines to execute If...Then...ElseIf statement - select one of many sets of lines to execute Select Case statement - select one of many sets of lines to execute

If...Then...Else
Use the If...Then...Else statement if you want to

execute some code if a condition is true select one of two blocks of code to execute

If you want to execute only one statement when a condition is true, you can write the code on one line: If i=10 Then alert("Hello") There is no ..Else.. in this syntax. You just tell the code to perform one action if a condition is true (in this case If i=10). If you want to execute more than one statement when a condition is true, you must put each statement on separate lines, and end the statement with the keyword "End If": If i=10 Then alert("Hello") i = i+1 End If There is no ..Else.. in the example above either. You just tell the code to perform multiple actions if the condition is true. If you want to execute a statement if a condition is true and execute another statement if the condition is not true, you must add the "Else" keyword:

Example (IE Only)


<html> <body> <head> <script type="text/vbscript"> Function greeting() i=hour(time) If i < 10 Then document.write("Good morning!") Else document.write("Have a nice day!") End If End Function </script> </head> <body onload="greeting()"> </body> </html>
Try it yourself

In the example above, the first block of code will be executed if the condition is true, and the other block will be executed otherwise (if i is greater than 10).

If...Then...ElseIf
You can use the If...Then...ElseIf statement if you want to select one of many blocks of code to execute:

Example (IE Only)


<html> <body> <head> <script type="text/vbscript"> Function greeting() i=hour(time) If i = 10 Then document.write("Just started...!") ElseIf i = 11 then document.write("Hungry!") ElseIf i = 12 then document.write("Ah, lunch-time!") ElseIf i = 16 then document.write("Time to go home!") Else document.write("Unknown") End If End Function </script> </head> <body onload="greeting()"> </body> </html>
Try it yourself

Select Case
You can also use the "Select Case" statement if you want to select one of many blocks of code to execute:

Example (IE Only)


<html> <body> <script type="text/vbscript"> d=weekday(date) Select Case d Case 1 document.write("Sleepy Sunday") Case 2 document.write("Monday again!") Case 3 document.write("Just Tuesday!") Case 4 document.write("Wednesday!") Case 5 document.write("Thursday...") Case 6 document.write("Finally Friday!") Case else document.write("Super Saturday!!!!") End Select </script> </body> </html>
Try it yourself

This is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each Case in the structure. If there is a match, the block of code associated with that Case is executed.

Looping Statements
Looping statements are used to run the same block of code a specified number of times. In VBScript we have four looping statements:

For...Next statement - runs code a specified number of times For Each...Next statement - runs code for each item in a collection or each element of an array Do...Loop statement - loops while or until a condition is true While...Wend statement - Do not use it - use the Do...Loop statement instead

For...Next Loop
Use the For...Next statement to run a block of code a specified number of times. The For statement specifies the counter variable (i), and its start and end values. The Next statement increases the counter variable (i) by one.

Example
<html> <body> <script type="text/vbscript"> For i = 0 To 5 document.write("The number is " & i & "<br />") Next </script> </body> </html>
Try it yourself

The Step Keyword


With the Step keyword, you can increase or decrease the counter variable by the value you specify. In the example below, the counter variable (i) is INCREASED by two, each time the loop repeats. For i=2 To 10 Step 2 some code Next To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value. In the example below, the counter variable (i) is DECREASED by two, each time the loop repeats. For i=10 To 2 Step -2 some code Next

Exit a For...Next
You can exit a For...Next statement with the Exit For keyword. For i=1 To 10 If i=5 Then Exit For some code Next

For Each...Next Loop


A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array.

Example
<html> <body> <script type="text/vbscript"> Dim cars(2) cars(0)="Volvo" cars(1)="Saab" cars(2)="BMW" For Each x In cars document.write(x & "<br />") Next </script> </body> </html>
Try it yourself

Do...Loop
If you don't know how many repetitions you want, use a Do...Loop statement. The Do...Loop statement repeats a block of code while a condition is true, or until a condition becomes true.

Repeat Code While a Condition is True


You use the While keyword to check a condition in a Do...Loop statement. Do While i>10 some code Loop If i equals 9, the code inside the loop above will never be executed. Do some code Loop While i>10 The code inside this loop will be executed at least one time, even if i is less than 10.

Repeat Code Until a Condition Becomes True


You use the Until keyword to check a condition in a Do...Loop statement. Do Until i=10 some code Loop If i equals 10, the code inside the loop will never be executed.

Do some code Loop Until i=10 The code inside this loop will be executed at least one time, even if i is equal to 10.

Exit a Do...Loop
You can exit a Do...Loop statement with the Exit Do keyword. Do Until i=10 i=i-1 If i<10 Then Exit Do Loop The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10.

VBScript Examples
Write text using VB Script. BASICS: <html> <body> <script type="text/vbscript"> document.write("Hello World!") </script> </body> </html> Answer: Hello World!

Format Text with html tags: <html> <body> <script type="text/vbscript"> document.write("<h1>Hello World!</h1>")

</script> </body> </html> Answer:

Hello World!
Function in script section : <html> <head> <script type="text/vbscript"> function myFunction() alert("Hello World!") end function </script> </head> <body onload="myFunction()"> <p>We usually use the head section for functions (to be sure that the functions are loaded before they are called).</p> </body> </html> Answer: We usually use the head section for functions (to be sure that the functions are loaded before they are called). A Script in body section: <html> <body>

<script type="text/vbscript">

document.write("This message is written by VBScript") </script>

</body> </html> Answer: This message is written by VBScript VARIABLES: Create a Variable: <html> <body>

<script type="text/vbscript"> Dim firstname firstname="Hege" document.write(firstname) document.write("<br />") firstname="Tove" document.write(firstname) </script>

<p>The script above declares a variable, assigns a value to it, and displays the value. Then, it changes the value, and displays the value again.</p>

</body> </html> Answer: Hege Tove The script above declares a variable, assigns a value to it, and displays the value. Then, it changes the value, and displays the value again. Inserting variable value in text: <html> <body>

<script type="text/vbscript"> Dim name name="Jan Egil" document.write("My name is: " & name) </script>

</body> </html>

Answer: My name is: Jan Egil Create an array: <html>

<body>

<script type="text/vbscript"> Dim famname(5) famname(0)="Jan Egil" famname(1)="Tove" famname(2)="Hege" famname(3)="Stale" famname(4)="Kai Jim" famname(5)="Borge" For i=0 To 5 document.write(famname(i) & "<br />") Next </script>

</body> </html> Answer: Jan Egil Tove Hege Stale Kai Jim Borge PROCEDURES: Sub Procedure:

<html>

<head> <script type="text/vbscript"> Sub mySub() msgbox("This is a Sub procedure") End Sub </script> </head>

<body> <script type="text/vbscript"> Call mySub() </script> <p>A Sub procedure does not return a result.</p> </body> </html>

Answer: A Sub procedure does not return a result. And A Alert message also thrown Fuction Procedure: <html>

<head> <script type="text/vbscript"> Function myFunction() myFunction = "BLUE" End Function </script> </head> <body> <script type="text/vbscript"> document.write("My favorite color is " & myFunction()) </script> <p>A Function procedure can return a result.</p> </body> </html> Answer: My favorite color is BLUE A Function procedure can return a result

Conditional Statements

If.. then..else statement <html> <head> <script type="text/vbscript">

Function greeting() i=hour(time) If i < 10 Then document.write("Good morning!") Else document.write("Have a nice day!") End If End Function </script> </head> <body onload="greeting()"> </body> </html> Answer: Have a nice day!

If..then..elseif statement <html>

<head>

<script type="text/vbscript"> Function greeting() i=hour(time) If i = 10 Then document.write("Just started...!") ElseIf i = 11 Then document.write("Hungry!") ElseIf i = 12 Then document.write("Ah, lunch-time!") ElseIf i = 16 Then document.write("Time to go home!") Else document.write("Unknown") End If End Function </script> </head>

<body onload="greeting()"> </body>

</html> Answer:

Unknown Select case Statement: <html> <body> <script type="text/vbscript"> d=weekday(Date) Select Case d Case 1 document.write("Sleepy Sunday") Case 2 document.write("Monday again!") Case 3 document.write("Just Tuesday!") Case 4 document.write("Wednesday!") Case 5 document.write("Thursday...") Case 6 document.write("Finally Friday!") Case Else document.write("Super Saturday!!!!") End Select </script>

<p>This example demonstrates the "Select Case" statement.<br /> You will receive a different greeting based on what day it is.<br /> Note that Sunday=1, Monday=2, Tuesday=3, etc.</p> </body> </html> Answer: Just Tuesday! This example demonstrates the "Select Case" statement. You will receive a different greeting based on what day it is. Note that Sunday=1, Monday=2, Tuesday=3, etc. Random link: <html> <body> <script type="text/vbscript"> randomize() r=rnd() If r>0.5 Then document.write("<a href='http://www.w3schools.com'>Learn Web Development!</a>") Else document.write("<a href='http://www.refsnesdata.no'>Visit Refsnes Data!</a>") End If </script> <p> This example demonstrates a link, when you click on the link it will take you to W3Schools.com OR to

RefsnesData.no. There is a 50% chance for each of them. </p> </body> </html> Answer: This example demonstrates a link, when you click on the link it will take you to W3Schools.com OR to RefsnesData.no. There is a 50% chance for each of them.
Looping

For.. next loop <html> <body> <script type="text/vbscript"> For i = 0 To 5 document.write("The number is " & i & "<br />") Next </script> </body> </html> Answer: The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 The number is 5 Looping through html headers:

<html> <body> <script type="text/vbscript"> For i=1 To 6 document.write("<h" & i & ">This is header " & i & "</h" & i & ">") Next </script> </body> </html> Answer:

This is header 1
This is header 2
This is header 3

This is header 4
This is header 5
This is header 6

For .. each loop: <html> <body> <script type="text/vbscript"> Dim cars(2) cars(0)="Volvo" cars(1)="Saab"

cars(2)="BMW" For Each x In cars document.write(x & "<br />") Next </script> </body> </html> Answer: Volvo Saab BMW Do..While loop: <html> <body> <script type="text/vbscript"> i=0 Do While i < 10 document.write(i & "<br />") i=i+1 Loop </script> </body> </html> Answer: 0 1 2 3 4 5 6 7 8 9
Date and Time Functions

Display date and time: <html>

<body>

<script type="text/vbscript"> document.write("Today's date is " & Date()) document.write("<br />") document.write("The time is " & Time()) </script>

</body> </html> Answer: Today's date is 8/2/2011 The time is 11:50:55 PM Display the days: <html> <body>

<p>VBScripts' function <b>WeekdayName</b> is used to get a weekday:</p> <script type="text/vbscript"> document.write("<p>") document.write(WeekDayName(1)) document.write("<br />") document.write(WeekDayName(2)) document.write("</p><p>")

document.write("Get the abbreviated name of a weekday:") document.write("<br />") document.write(WeekDayName(1,True)) document.write("<br />") document.write(WeekDayName(2,True)) document.write("</p><p>")

document.write("Get the current weekday:") document.write("<br />") document.write(WeekdayName(weekday(Date))) document.write("<br />") document.write(WeekdayName(weekday(Date), True)) document.write("</p>") </script>

</body> </html> Answer: VBScripts' function WeekdayName is used to get a weekday: Sunday Monday Get the abbreviated name of a weekday: Sun Mon

Get the current weekday: Tuesday Tue Display the months: <html> <body>

<p>VBScripts' function <b>MonthName</b> is used to get a month:</p> <script type="text/vbscript"> document.write("<p>") document.write(MonthName(1)) document.write("<br />") document.write(MonthName(2)) document.write("</p><p>")

document.write("Here is how you get the abbreviated name of a month:") document.write("<br />") document.write(MonthName(1,True)) document.write("<br />") document.write(MonthName(2,True)) document.write("</p><p>")

document.write("Here is how you get the current month:") document.write("<br />")

document.write(MonthName(Month(Date))) document.write("<br />") document.write(MonthName(Month(Date),True)) document.write("</p>") </script>

</body> </html> Answer: VBScripts' function MonthName is used to get a month: January February Here is how you get the abbreviated name of a month: Jan Feb Here is how you get the current month: August Aug

Display the current month and date: html> <body>

<script type="text/vbscript">

document.write("Today's day is " & WeekdayName(Weekday(Date))) document.write("<br />") document.write("The month is " & MonthName(Month(Date))) </script>

</body> </html> Answer: Today's day is Tuesday The month is August


Other Built-in Functions Uppercase or lower case

<html> <body> <script type="text/vbscript"> txt="Have a nice day!" document.write(UCase(txt)) document.write("<br />") document.write(LCase(txt)) </script> </body> </html> Answer: HAVE A NICE DAY! have a nice day!

Remove Leading or trailing spaces from string; <html> <body>

<script type="text/vbscript"> fname=" Bill " document.write("Hello" & Trim(fname) & "Gates<br />") document.write("Hello" & RTrim(fname) & "Gates<br />") document.write("Hello" & LTrim(fname) & "Gates<br />") </script>

</body> </html> Answer: HelloBillGates Hello BillGates HelloBill Gates Reverse a string: <html> <body>

<script type="text/vbscript"> sometext = "Hello Everyone!" document.write(StrReverse(sometext))

</script>

</body> </html> Answer!enoyrevE olleH

Round a number: <html> <body>

<script type="text/vbscript"> i = 48.66776677 j = 48.3333333 document.write(Round(i)) document.write("<br />") document.write(Round(j)) </script>

</body> </html> Answer: 49 48

Return a random number: <html> <body>

<script type="text/vbscript"> Randomize() document.write(Rnd()) </script>

</body> </html> Answer: 0.6506006

Return a random number(0-99): <html> <body>

<script type="text/vbscript"> Randomize()

randomNumber=Int(100 * Rnd()) document.write("A random number: <b>" & randomNumber & "</b>") </script>

</body> </html> Answer: A random number: 30 Return a specified number of characters from the left or right side of a string: <html> <body>

<script type="text/vbscript"> sometext="Welcome to our Web Site!!" document.write(Left(sometext,5)) document.write("<br />") document.write(Right(sometext,5)) </script>

</body> </html> Answer:

Welco ite!! Replace some characters in a string <html> <body>

<script type="text/vbscript"> sometext="Welcome to this Web!!" document.write(Replace(sometext, "Web", "Page")) </script>

</body> </html> Answer: Welcome to this Page!!

Return a specified number of characters from a string <html> <body>

<script type="text/vbscript">

sometext="Welcome to our Web Site!!" document.write(Mid(sometext, 9, 2)) </script>

</body> </html> Answer: To

Date/Time Functions
Function CDate Date DateAdd DateDiff DatePart DateSerial DateValue Day FormatDateTime Hour IsDate Minute Month MonthName Now Second Time Timer TimeSerial Description Converts a valid date and time expression to the variant of subtype Date Returns the current system date Returns a date to which a specified time interval has been added Returns the number of intervals between two dates Returns the specified part of a given date Returns the date for a specified year, month, and day Returns a date Returns a number that represents the day of the month (between 1 and 31, inclusive) Returns an expression formatted as a date or time Returns a number that represents the hour of the day (between 0 and 23, inclusive) Returns a Boolean value that indicates if the evaluated expression can be converted to a date Returns a number that represents the minute of the hour (between 0 and 59, inclusive) Returns a number that represents the month of the year (between 1 and 12, inclusive) Returns the name of a specified month Returns the current system date and time Returns a number that represents the second of the minute (between 0 and 59, inclusive) Returns the current system time Returns the number of seconds since 12:00 AM Returns the time for a specific hour, minute, and second

TimeValue Weekday WeekdayName Year

Returns a time Returns a number that represents the day of the week (between 1 and 7, inclusive) Returns the weekday name of a specified day of the week Returns a number that represents the year

Conversion Functions
Function Asc CBool CByte CCur CDate CDbl Chr CInt CLng CSng CStr Hex Oct Description Converts the first letter in a string to ANSI code Converts an expression to a variant of subtype Boolean Converts an expression to a variant of subtype Byte Converts an expression to a variant of subtype Currency Converts a valid date and time expression to the variant of subtype Date Converts an expression to a variant of subtype Double Converts the specified ANSI code to a character Converts an expression to a variant of subtype Integer Converts an expression to a variant of subtype Long Converts an expression to a variant of subtype Single Converts an expression to a variant of subtype String Returns the hexadecimal value of a specified number Returns the octal value of a specified number

Top

Format Functions
Function FormatCurrency FormatDateTime FormatNumber FormatPercent Description Returns an expression formatted as a currency value Returns an expression formatted as a date or time Returns an expression formatted as a number Returns an expression formatted as a percentage

Top

Math Functions
Function Abs Atn Cos Exp Hex Int Description Returns the absolute value of a specified number Returns the arctangent of a specified number Returns the cosine of a specified number (angle) Returns e raised to a power Returns the hexadecimal value of a specified number Returns the integer part of a specified number

Top

Fix Log Oct Rnd Sgn Sin Sqr Tan

Returns the integer part of a specified number Returns the natural logarithm of a specified number Returns the octal value of a specified number Returns a random number less than 1 but greater or equal to 0 Returns an integer that indicates the sign of a specified number Returns the sine of a specified number (angle) Returns the square root of a specified number Returns the tangent of a specified number (angle)

Array Functions
Function Array Filter IsArray Join LBound Split UBound Description Returns a variant containing an array Returns a zero-based array that contains a subset of a string array based on a filter criteria Returns a Boolean value that indicates whether a specified variable is an array Returns a string that consists of a number of substrings in an array

Top

Returns the smallest subscript for the indicated dimension of an array Returns a zero-based, one-dimensional array that contains a specified number of substrings Returns the largest subscript for the indicated dimension of an array

String Functions
Function InStr InStrRev LCase Left Len LTrim RTrim Trim Mid Replace Right Description

Top

Returns the position of the first occurrence of one string within another. The search begins at the first character of the string Returns the position of the first occurrence of one string within another. The search begins at the last character of the string Converts a specified string to lowercase Returns a specified number of characters from the left side of a string Returns the number of characters in a string Removes spaces on the left side of a string Removes spaces on the right side of a string Removes spaces on both the left and the right side of a string Returns a specified number of characters from a string Replaces a specified part of a string with another string a specified number of times Returns a specified number of characters from the right side of a

string Space StrComp String StrReverse UCase Returns a string that consists of a specified number of spaces Compares two strings and returns a value that represents the result of the comparison Returns a string that contains a repeating character of a specified length Reverses a string Converts a specified string to uppercase

Other Functions
Function CreateObject Eval GetLocale GetObject GetRef InputBox IsEmpty IsNull IsNumeric IsObject LoadPicture MsgBox RGB Round ScriptEngine ScriptEngineBuildVersion ScriptEngineMajorVersion ScriptEngineMinorVersion SetLocale TypeName VarType Description Creates an object of a specified type Evaluates an expression and returns the result Returns the current locale ID Returns a reference to an automation object from a file

Top

Allows you to connect a VBScript procedure to a DHTML event on your pages Displays a dialog box, where the user can write some input and/or click on a button, and returns the contents Returns a Boolean value that indicates whether a specified variable has been initialized or not Returns a Boolean value that indicates whether a specified expression contains no valid data (Null) Returns a Boolean value that indicates whether a specified expression can be evaluated as a number Returns a Boolean value that indicates whether the specified expression is an automation object Returns a picture object. Available only on 32-bit platforms Displays a message box, waits for the user to click a button, and returns a value that indicates which button the user clicked Returns a number that represents an RGB color value Rounds a number Returns the scripting language in use Returns the build version number of the scripting engine in use Returns the major version number of the scripting engine in use Returns the minor version number of the scripting engine in use Sets the locale ID and returns the previous locale ID Returns the subtype of a specified variable Returns a value that indicates the subtype of a specified variable

VBScript Keywords
Keyword Empty Description Used to indicate an uninitialized variable value. A variable value is uninitialized when it is first created and no value is assigned to it, or when a variable value is explicitly set to empty. Example: Dim x 'the variable x is uninitialized! x="ff" 'the variable x is NOT uninitialized anymore x=Empty 'the variable x is uninitialized! Note: This is not the same as Null!! IsEmpty Used to test if a variable is uninitialized. Example: If (IsEmpty(x)) 'is x uninitialized? Nothing Used to indicate an uninitialized object value, or to disassociate an object variable from an object to release system resources. Example: Set myObject=Nothing Is Nothing Used to test if a value is an initialized object. Example: If (myObject Is Nothing) 'is it unset? Note: If you compare a value to Nothing, you will not get the right result! Example: If (myObject = Nothing) 'always false! Null Used to indicate that a variable contains no valid data. One way to think of Null is that someone has explicitly set the value to "invalid", unlike Empty where the value is "not set". Note: This is not the same as Empty or Nothing!! Example: x=Null 'x contains no valid data IsNull Used to test if a value contains invalid data. Example: if (IsNull(x)) 'is x invalid? True False Used to indicate a Boolean condition that is correct (True has a value of -1) Used to indicate a Boolean condition that is not correct (False has a value of 0)

You might also like