Software, Systems, and Solutions

"We do not run your business. We make your business run better."

Skip Repetitive Navigational Links

VB Script

  • Microsoft Visual Basic Scripting Edition, is a subset of the  Visual Basic family of programming language.
  • It 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 Server.

Adding VBScript Code to an HTML Page

  • VBScript can be embedded in HTML pages either to create  dynamic pages (client side scripting) and CGI-based applications, you may take advantage of Microsoft's Active Server Pages (ASP) technology. 
  • To embed VBScript in a web page for "client-side" scripting you would enclose the code within the opening and closing script tags: <script language="VBScript"> and </script>
  • To embed VBScript in a web page for "serever-side" scripting you enclose the commands within an opening <% symbol and a closing %> symbol. This is known as a SCRIPT element and is used when creating Active Server Pages.
    • ASPs are based on VBScript, Microsoft's development language. To create and ASP you need to create a HTML file with script elements and the extension ".asp". 
    • ASP files are processed by the server prior to sending the page to the browser, which makes it very easy to create dynamic pages. 

Note:  While VBScript can be used for both client-side scripting and server-side scripting, for client-side scripting JavaScript is more universally supported. Nevertheless, using VBScript for server-side scripting, as described here, is very powerful and compatible with any browser.

VB Script Variables

  • 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. 
  • You only have to refer to a variable by name to see its value or to change its value. In VBScript, variables are always of one fundamental data type, Variant.


  • A Variant is a special kind of data type that can contain different kinds of information, depending on how it's used. 
  • At its simplest, a Variant can contain either numeric or string information. 
    • If you're working with data that looks like numbers, VBScript assumes that it is numbers and does the thing that is most appropriate for numbers. 
    • If you're working with data that can only be string data, VBScript treats it as string data. 

Variant Subtypes

  • Beyond the simple numeric or string classifications, a Variant can make further distinctions about the specific nature of numeric information. (It  knows what it is). 
  • 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.
  • It is recommended you use the same naming conventions objects, variables, and procedures as in Visual Basic.  That is each variable of a certain type is preceded by 3 letters describing its Variant Subtype.
  • The following table shows the subtypes of data that a Variant can contain and the 3 letter naming convention for each sub type.
Subtype Naming
Empty Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables.
Null Variant intentionally contains no valid data.
Boolean bln Contains either True or False.
Byte byt Contains integer in the range 0 to 255.
Integer int Contains integer in the range -32,768 to 32,767.
Currency cur -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Long lng Contains integer in the range -2,147,483,648 to 2,147,483,647.
Single sng Contains a single-precision, floating-point numbers 
Double dbl Contains a double-precision, floating-point numbers.
Date (Time) dtm Contains a date between 1/1/ 100 & 12/31/ 9999.
String str Contains a variable-length string up to 2 billion characters long.
Object obj Contains an object.
Error err Contains an error number.

Declaring Variables

  • You declare variables using the Dim statement, the Public statement, and the Private statement:
 Dim intDegreesFahrenheit
  • You declare multiple variables by separating each variable name with a comma:
 Dim intTop, intBottom, intLeft, intRight
  • You can also declare a variable implicitly by simply using its name in your script. That's not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. 
    • For that reason, the Option Explicit statement is available to require explicit declaration of all variables. 
    • As with regular Visual Basic The Option Explicit statement should be the first statement in your script.

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.

Scope and Lifetime of Variables

  • A variable's scope is determined by where you declare it. 
    • When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local scope and is called a procedure-level variable. 
    • If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This is a script-level variable, and it has script-level scope.
  • How long a variable exists is its lifetime. 
    • The lifetime of a script-level variable extends from the time it's declared until the time the script is finished running. 
    • At procedure level, a variable exists only as long as you are in the procedure. When the procedure exits, the variable is destroyed. 
    • Local variables are ideal as temporary storage space when a procedure is executing. You can have local variables of the same name in several different procedures because each is recognized only by the procedure in which it is declared.

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:
 intB = 200

Scalar Variables and Array Variables

  • A variable containing a single value is a scalar variable & A variable that can contain a series of values is called an array variable. 
  • Array variables are declared using parentheses ( ) following the variable name. 
    • Below, an array containing 11 elements is declared:
Dim intA(10)
  • Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based. This array actually contains 11 elements (0 to 10). 

Array Assignment

  • You assign data to each of the elements of the array using the array index. Begin at zero and end at 10:
 intA(0) = 256
 intA(1) = 324
 . . .
 intA(10) = 55
  • Similarly, the data can be retrieved from any element using the index of the array element you want. For example:
 intSomeVariable = intA(8)

Dynamic Arrays

  • You can also declare an array whose size changes during the time your script is running. This is called a dynamic array
  • The array is initially declared using either Dim or ReDim. However, for a dynamic array, no size or number of dimensions is placed inside the parentheses:
 Dim intMyArray()
 ReDim intAnotherArray()
  • To use a dynamic array, you must subsequently use ReDim to specify the number of dimensions and the size of each dimension.  The Preserve keyword is used with  ReDim to preserve the contents of the array as the resizing takes place.
 ReDim intMyArray(25)
 . . . 
 ReDim Preserve intMyArray(30)
  • There is no limit to the number of times you can resize a dynamic array, but if you make an array smaller than it was, you lose the data in the eliminated elements.


  • A constant is a meaningful name that takes the place of a number or string and never changes. 
  • You create user-defined constants in VBScript using the Const statement. Using the Const statement, you can create string or numeric constants with meaningful names and assign them literal values. For example:
Const conMyString = "This is my string."
Const conMyAge = 49
  • The string literal is enclosed in quotation marks (" "). Date literals and time literals are represented by enclosing them in number signs (#). For example:
Const conCutoffDate = #6-1-97#

VB Operators

  • The standard VB Script Arithmetic, Comparison and Logical Operators are listed int he table below.
  • When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. 
Arithmetic Comparison Logical
Description Symbol Description Symbol Description Symbol
Exponentiation ^ Equality = Logical negation Not
Unary negation - Inequality <> Logical conjunction And
Multiplication, Division *, / Less than < Logical disjunction Or
Modulus Mod Greater than > Logical exclusion Xor
Addition, Subtraction +, - Less than or equal to <= Logical equivalence Eqv
String concatenation & Greater than or equal to >= Logical implication Imp
  • The string concatenation (&) operator is not an arithmetic operator, but in precedence it does fall after all arithmetic operators and before all comparison operators. 

Controlling Program Execution

  • You can control the flow of your script with conditional statements and looping statements. Using conditional statements, you can write VBScript code that makes decisions and repeats actions. 

Selection Statements

  • The following selection statements are available in VBScript:
    • If...Then...Else statement
    • Select Case statement


  • 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: 
Sub AlertUser(value)
 If value = 0 Then
   A.ForeColor = vbRed 
   A.Font.Bold = True
 End If
End Sub
Sub AlertUser(value)
 If value = 0 Then
   A.ForeColor = vbRed
   A.Font.Bold = True
   A.ForeColor = vbBlue
   A.Font.Bold = False
 End If
End Sub


  • The If...Then...ElseIf...Else statement allows you to choose from several alternatives:
Sub ReportValue(value)
   If value = 0 Then
      MsgBox "American Express"
   ElseIf value = 1 Then
      MsgBox "Visa"
   ElseIf value = 2 then
      Msgbox "Master Card"
      Msgbox "Value out of range!"
End If

Select Case

  • The Select Case structure works with a single test expression that is evaluated once. The result of the expression is then compared with the values for each Case in the structure. If there is a match, the block of statements associated with that Case is executed:
Sub ReportValue(value)
   Select Case value
   Case 0
      MsgBox "American Express"
   Case 1
      Msgbox "Visa"
   Case 2
      Msgbox "Master Card"
   Case Else
      Msgbox "Value out of range!"
End Select

VB Repetition Structures

  • Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is False; others repeat statements until a condition is True. There are also loops that repeat statements a specific number of times.
  • Some looping statements available in VBScript are:
    • Do...Loop: Loops While or Until a condition is True.
    • For...Next: Uses a counter to run statements a specified number of times.

Do Loops

  • Do...Loop statements run an indefinite number of times. 
    • Use the While keyword to check a condition in a Do...Loop statement. The loop runs While a condition is True
    • Use the Until keyword to check a condition in a Do...Loop statement. When condition is False, the looping occurs.
Sub ChkFirstWhile()
   Dim intN
   intN = 20
   Do While intN > 10 
     intNum = intN - 1
End Sub
Sub ChkLastUntil()
   Dim intN
   intN = 1
     intN = intN + 1
   Loop Until intN = 10
End Sub


  • For...Next statements run a block of statements a specific number of times: 
Sub DoMyProc50Times() 
  Dim intx, intT
  For intx = 1 To 50
    intT = intT + intx  
End Sub
Sub TwosTotal()
  Dim j, intT
  For j = 2 To 10 Step 2
    intT = intT + j
  MsgBox "Total=" & intT
 End Sub

Sub and Function Procedures

  • In VBScript there are two kinds of procedures; 
    • The Sub procedure which returns nothing, and 
    • The Function procedure which can return a value.

Sub Procedures

  • A Sub procedure is a series of VBScript statements, enclosed by Sub and End Sub statements, that perform actions but don't return a value. 
  • A Sub procedure can take arguments (constants, variables, or expressions that are passed by a calling procedure). 
  • If a Sub procedure has no arguments, its Sub statement must include an empty set of parentheses ().
Sub ConvertTemp() // Sub takes no arguments
   temp = InputBox("Enter a temp in F", 1)
   MsgBox "Temperature=" & Celsius(temp) & " C."
End Sub  // calls function Celsius (below)

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.
Function Celsius(fDeg)fDeg is the Argument Celsius = (fDeg - 32) * 5 / 9 End Function

Using Sub and Function Procedures in Code

  • A Function in your code must always be used on the right side of a variable assignment or in an expression:
Temp = Celsius(fDeg)
MsgBox "Temperature=" & Celsius(fDeg) & " C."
  • To call a Sub procedure from another procedure, you can just 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.
Call MySub(firstarg, secondarg) MySub firstarg, secondarg

or Call ConvertTemp() ConvertTemp

  • Notice that the parentheses are omitted in the call when the Call statement isn't used.

VBScript with Form Elements

  • Like JavaScript, VBScript can be used to read data from form elements or write data to form elements.
  • To access a textbox in a form use the following when you are referencing a textbox with the name txtName in the first form int he document - You can not use the form name to reference the form like you did in JavaScript:


  • To convert the String retrieved from the textbox to a number you need to use:
    • CSng for Singles, CDbl for Doubles, CInt  for Integers, CLng for Longs (etc.)
Sales:    Bonus:    
<Script Language="VBScript">
Sub calcInput()
  Dim intSales
  intSales = CInt(Document.Forms(0).txtSales.Value)
  if intSales > 500 Then
    Document.Forms(0).txtBonus.Value = "$100.00" 
    Document.Forms(0).txtBonus.Value = "No Bonus" 
  End If 
End Sub 
txtSales" VALUE=""