VBScript Primer

Note: VBScript is deprecated effective October 2023. In future releases of Windows, VBScript will be available as a feature on demand before its removal from the operating system. See Resources for deprecated features for more information.

Microsoft Visual Basic for Scripting (VBScript) is a subset of Visual Basic created for client-side Internet scripting, although it is now used in many Microsoft Windows applications. The VBScript syntax is similar to that of Visual Basic and is relatively simple. The following sections outline some aspects of VBScript that you may find useful as a starter or refresher.

A helpful resource for learning VBScript is available online at w3schools.com.

Properties

Properties are attributes or values of an object that can be read and/or set. As long as the value returned by the property is not an object, reading and setting property values generally requires only a simple assignment statement. Object assignment lines must be prefaced by the Set keyword.

The following line of code reads the value of "Facility1_TankLevel" and stores it in the variable lngLevel.

lngLevel = Facility1_TankLevel.Value

The following line of code sets the value of "Facility1_TankLevel":

Facility1_TankLevel.Value = 100

Not all properties can be written. If you attempt to set a property that is read-only, an error code will be generated.

Methods

Methods include functions and subroutines that are available for an object. A function returns a value, whereas a subroutine does not. Both functions and subroutines can take one or more arguments (or none), depending on how the method is defined. Methods are called by prefacing the method name with the object name, followed by a period.

The following line of code sets the strRefSite variable to the value returned by the MyPoint object’s GetAttribute method. The method takes one argument:  the attribute to return.

strRefSite = MyPoint.GetAttribute(ReferenceSite)

The following line of code calls the Connect method of the VhsClient object. In VBScript, the parentheses are optional when no arguments are required.

VhsClient.Connect()

When calling a function, it is necessary to include parentheses around the function arguments. On the other hand, the arguments of subroutines must not be enclosed in parentheses, unless passing a single variable. Some methods return objects, and must be used with the Set statement.

Set vhsPointIter = CreateObject("CxVhsLib.PointIterator")

The examples in the following sections will explain how to create your own methods for HyperPoint and CygNet Studio objects.

Events

Events handlers are methods that are called by the script engine in response to an event. For example, when a CygNet Studio button is pressed, its EventClick custom event code is run. In the HSS, a HyperPoint’s OnInitializeEx event handler is run when the script engine initializes the point.

Variables and Data Types

In VBScript, all variables are variants, although there are many subtypes that the variant type can support. These include:

Value Variant Subtype Constant Description
0 Empty vbEmpty Uninitialized
1 Null vbNull No valid data
2 Integer vbInteger Signed integer stored in 2 bytes
3 Long vbLong Signed integer stored in 4 bytes
4 Single vbSingle Single precision floating data point
5 Double vbDouble Double precision floating data point
6 Currency vbCurrency Special numeric format for storing monetary values
7 Date vbDate Date or time value
8 String vbString String data of variable length
9 OLE Automation Object VbObject Contains a reference to an object, typically an OLE automation object.
10 Error vbError Used to store an error number
11 Boolean vbBoolean Logical data that is either True or False
12 Array of Variant vbVariant Variant array
13 Data Access Object vbDataObject Generic object
17 Byte vbByte Unsigned integer stored in 1 byte
8192 Array vbArray Array

To determine the subtype, use the VarType or TypeName functions. VarType returns an integer that represents the subtype. TypeName returns the subtype name.

VarType(variable) or TypeName(variable)

Before manipulating data, it is sometimes necessary to validate the variant’s subtype. For example, if you are going to perform a calculation, make sure that the subtype is a numeric.

If Not IsNumeric(Point1.Value) Then Exit Sub

If necessary, convert the data into the appropriate subtype. The table below lists the VBScript conversion functions. For example, adding two point values may require that the values be of the double subtype, requiring the following line of code.

SumPoint.Value = CDbl(Point1.Value) + CDbl(Point2.Value)

 

Conversion Type Description

CBool

Converts variablename to Boolean data subtype. If variablename is 0 or "0" CBool returns False; otherwise it returns True (-1).

CByte

Converts variablename to a Byte data subtype. Variable name can contain any numeric or string data that is capable of conversion into a number that is greater than or equal to 0 and less than or equal to 255. If variablename is out of range, VBScript displays on Overflow error message. If variablename is a floating-point number, it is rounded to the nearest integer before being converted to byte data.

CDate

Converts variablename to a Date/Time data subtype. This function accepts numeric or string data that appears to be a date, converting it to the correct format for the machine. The date is returned in the format specified by the local information on the client computer. On a machine set to the American date format MM/DD/YY, if you enter the British date format dd/mm/yy in a text box and then use this function on the contents of the text box, the text will be converted to the American format.

CCur

Converts variablename to a Currency data subtype. This function accepts any numeric or string data that can be expressed as a currency value. The function recognizes the decimal and thousands separators based on local information on the client computer.

CDbl

Converts variablename to a Double data subtype. This function accepts any numeric data within the limits of the Double data subtype or any string data that can be converted to a number within the range of the Double data subtype.

Cint

Converts variablename to an Integer data subtype. This function accepts any numeric data within the limits of the Integer data subtype or any string data that can be converted to a number within the range of the Integer data subtype.

CLng

Converts variablename to a Long data subtype. This function accepts any numeric data within the limits of the Long data subtype or any string data that can be converted to a number within the range of the Long data subtype.

CSng

Converts variablename to a Single data subtype. This function accepts any numeric data within the limits of the Single data subtype or any string data that can be converted to a number within the range of the Single data subtype.

CStr

Converts variablename to a String data subtype. This function accepts any kind of data.

Refer to online documentation at MSDN for more information about the VBScript language.

Back to top