Introduction to VBScript learning materials


Microsoft Visual Basic Scripting Edition is the latest member of the visual basic family of programming languages. It applies flexible scripts to a wider range of fields, including web client scripts in Microsoft Internet Explorer and web server scripts in Microsoft Internet information server.

Easy to learn and use

If you already know visual basic or visual basic for applications, you will soon become familiar with VBScript. Even if you have not learned visual basic, as long as you learn VBScript, you can use all visual basic languages for programming. Although you can learn VBScript from several web pages in this tutorial, this tutorial does not tell you how to program. To learn programming, read step by step published by Microsoft Press.

  ActiveX Script

VBScript using ActiveX ™ Script talks to the host application. With ActiveX script, browsers and other host applications no longer need special integration code for each script part. ActiveX script enables the host to compile scripts, obtain and invoke entry points, and manage namespaces available to developers. Through ActiveX script, language manufacturers can establish a standard script runtime language. Microsoft will provide runtime support for VBScript. Microsoft is working with multiple internet groups to define ActiveX script standards to make script engines interchangeable. ActiveX scripts are available in Microsoft ® Internet Explorer and Microsoft ® Internet information server.

VBScript in other applications and browsers

As a developer, you can use VBScript source to realize the program in your product for free. Microsoft for 32-bit windows ® API, 16 bit Windows API and Macintosh ® Provide binary implementation program of VBScript. VBScript is integrated with the world wide web browser. VBScript and ActiveX script can also be used as normal script languages in other applications.

What is VBScript data type?
VBScript has only one data type, called variant. Variant is a special data type. It can contain different types of information according to the way it is used. Because variant is the only data type in VBScript, it is also the data type of the return value of all functions in VBScript.

The simplest variant can contain numeric or string information. Variant is processed as a number when used in a digital context and as a string when used in a string context. That is, if you use data that looks like a number, VBScript assumes it is a number and processes it in a manner suitable for numbers. Similarly, if the data used can only be strings, VBScript will process as strings. Of course, you can also include a number in quotation marks (“”) to make it a string.

Variant subtype

In addition to simple numbers or strings, variant can further distinguish the specific meaning of numerical information. For example, use numeric information to represent a date or time. When such data is used with other date or time data, the result is always expressed as date or time. Of course, there are many kinds of numerical information, from Boolean value to floating-point number. The types of numeric information contained in a variant are called subtypes. In most cases, you can put the required data into the variant, and the variant will operate in the way that is best suited to the data it contains.

The following table shows the data subtypes that variant contains:

Subtype description
Empty uninitialized variant. For numeric variables, the value is 0; For string variables, the value is a zero length string (“”).

Null a variant that does not contain any valid data.

Boolean contains true or false.

Byte contains an integer between 0 and 255.

Integer contains an integer between – 32768 and 32767.

Currency -922337203685477.5808 to 922337203685477.5807.

Long contains an integer between – 2147483648 and 2147483647.

Single contains single precision floating-point numbers, with negative numbers ranging from -3.402823e38 to -1.401298e-45 and positive numbers ranging from 1.401298e-45 to 3.402823e38.

Double contains double precision floating-point numbers, with negative numbers ranging from -1.79769313486232e308 to -4.94065645841247e-324 and positive numbers ranging from 4.94065645841247e-324 to 1.79769313486232e308.

Date (time) contains a number representing the date, ranging from January 1, A.D. 100 to December 31, A.D. 9999.

String contains variable length strings with a maximum length of 2 billion characters.

Object contains objects.

Error contains the error number.

You can use conversion functions to convert subtypes of data. Alternatively, you can use the vartype function to return the variant subtype of the data.

What are variables?

Variables are easy-to-use placeholders that reference computer memory addresses that store program information that can be changed when a script runs. For example, you can create a variable called clickcount to store the number of times a user clicks an object on a web page. You don’t need to know the address of the variable in the computer memory to use the variable. You can view or change the value of the variable as long as you reference the variable through the variable name. In VBScript, there is only one basic data type, namely variant, so the data type of all variables is variant.

Declare variable

One way to declare variables is to explicitly declare variables in scripts using dim statements, public statements, and private statements. For example:

Dim DegreesFahrenheit
When declaring multiple variables, use commas to separate variables. For example:

Dim Top, Bottom, Left, Right

Another way is to implicitly declare variables by using variable names directly in the script. This is usually not a good habit because it can sometimes lead to unexpected results when running scripts due to misspelled variable names. Therefore, it is best to use the option explicit statement to explicitly declare all variables as the first statement of the script.

Naming rules

Variable naming must follow the standard naming rules of VBScript. Variable naming must follow:

The first character must be a letter.
Cannot contain embedded periods.
The length cannot exceed 255 characters.
Must be unique within the declared scope.
Scope and survival of variables
The scope of a variable is determined by where it is declared. If a variable is declared in a procedure, only the code in the procedure can access or change the value of the variable. At this time, the variable has a local scope and is called a procedure level variable. If a variable is declared outside the procedure, it can be recognized by all procedures in the script. It is called a script level variable and has a script level scope.

The time the variable exists is called survival. Script level variables live from the moment they are declared until the end of the script run. For process level variables, their survival period is only the running time of the process. After the process is completed, the variables disappear. Local variables are ideal temporary storage space when executing procedures. Local variables with the same name can be used in different procedures because each local variable is only recognized by the procedure that declares it.

Assign values to variables

Create an expression in the following form to assign a value to the variable: the variable is on the left of the expression and the value to be assigned is on the right of the expression. For example:

B = 200

Scalar and array variables

In most cases, you only need to assign a value to the declared variable. Variables that contain only one value are called scalar variables. Sometimes it is more convenient to assign multiple related values to a variable, so you can create a variable containing a series of values, called an array variable. Array variables and scalar variables are declared in the same way. The only difference is that when declaring array variables, the variable name is followed by parentheses (). The following example declares a one-dimensional array containing 11 elements:

  Dim A(10)

Although the number shown in parentheses is 10, since all arrays in VBScript are based on 0, this array actually contains 11 elements. In a 0-based array, the number of array elements is always the number shown in parentheses plus 1. Such arrays are called fixed size arrays.

An index is used in an array to assign a value to each element of the array. From 0 to 10, assign data to the elements of the array, as follows:

A(0) = 256
A(1) = 324
A(2) = 100
. . .
A(10) = 55

Similarly, the index can be used to retrieve the data of the required array elements. For example:

. . .
SomeVariable = A(8)
. . .

Arrays are not limited to one dimension. The maximum dimension of an array can be 60 (although most people cannot understand a dimension greater than 3 or 4). When you declare a multidimensional array, the size of each array is separated by a comma. In the following example, the mytable variable is a two-dimensional array with 6 rows and 11 columns:

Dim MyTable(5, 10)

In a two-dimensional array, the first number in parentheses represents the number of rows and the second number represents the number of columns.

You can also declare a dynamic array, that is, an array whose size changes when you run a script. Use a dim statement or a ReDim statement for the initial declaration of the array. However, for dynamic arrays, there are no numbers in parentheses. For example:

  Dim MyArray()
  ReDim AnotherArray()

To use dynamic arrays, you must then use ReDim to determine the dimension and the size of each dimension. In the following example, ReDim sets the initial size of the dynamic array to 25, while the subsequent ReDim statement resizes the array to 30, while using the preserve keyword to preserve the contents of the array when resizing.

  ReDim MyArray(25)
. . .
ReDim Preserve MyArray(30)

There is no limit to the number of times to resize the dynamic array, but it should be noted that adjusting the size of the array will lose the data of the deleted elements.