Introduction to Web Design - 14. VBScripts
14.1 Basic Differences between VBScript and JavaScript | 14.2 Arithmetic, Comparison and Logical Operators | 14.3 VBScript Control Structures | 14.4 Iteration | 14.5 VBScript Math, Variant, String and Formatting Functions | 14.6 VBScript Dialog Boxes | 14.7 Defining Your Own VBScript Functions
  1. Preface
  2. Markup Languages – A Definition and Some History
  3. Beginning HTML
  4. HTML Lists
  5. HTML Tables
  6. HTML - Color, Fonts and Special Characters
  7. HTML Links
  8. HTML Images
  9. HTML Frames
  10. Cascading Style Sheets
  11. MicroSoft PhotoDraw
  12. JavaScript
  13. HTML Forms and Form Handling
  14. VBScript
  15. MicroSoft FrontPage
  16. Active Server Pages
  17. Java Applets
  18. XML Meaning and More
  19. Macromedia Flash 5.0
  20. References
VBScript (Visual Basic Script) is another scripting language that can be used in the WWW. However, VBScript is based on MicroSoft Visual Basic and only MicroSoft Internet Explorer has a VBScript interpreter. Therefore, VBScript will not be practical for many WWW purposes. It can be used in intranets if everyone is restricted to using Internet Explorer to access the intranet. It can also be used with Microsoft Servers to create Active Server Pages which use server-side scripts that create content that is sent to the client’s browser. In that case, the client’s browser is not restricted to Internet Explorer, as long as the content being sent is consistent for any WWW browser. Putting the actual script code inside HTML comments is considered good practice with VBScript as it is with JavaScript, to prevent the code from being displayed in browsers that do not have VBScript interpreters.

This section will present a quick overview of client-side scripting with VBScript. Since the basics of programming and scripting were covered in the section on JavaScript, only the basics of VBScript will be covered and some of the differences between JavaScript and VBScript will be mentioned. The reader should refer to the end of this document and the Reference section for more complete references on VBScript.

14.1 Some Basic Syntax Differences Between VBScript and JavaScript

Unlike JavaScript, VBScript is a case-insensitive language. That means that it views uppercase and lowercase letters to be the same. For example, it would see “num”, “Num” and NUM” to be the same item whereas JavaScript would view them as three separate things.

JavaScript indicates the end of a statement with a semicolon, ‘;’. However, in VBScript, there is no character to indicate the end of a statement. The end of a line is considered the end of a statement. If a statement is too long to fit on one line, the line continuation character, ‘_’, the underscore character must be placed on the line to be continued.

In both languages, variables do not need to be declared before being used. However, it is considered good programming practice to declare variables and to include comments that indicate the purpose of the variables. In VBScript, the keyword ‘Dim’ is used to declare variables instead of the word ‘var’ in JavaScript. In VBScript, if the first line in a script or function is “Option Explicit”, then variables must be declared before use.

All variables in VBScript are of type variant. This data type can store different data types such as strings, integers, floating point numbers, boolean, etc.

As in JavaScript, arrays in VBScript always start with the index value zero. However, declaring an array in VBScript is done by giving the highest index value needed rather than the size. The declaration

Dim a(5)


in VBScript declares an array of six elements numbered from zero to 5.

14.2 Arithmetic, Comparison and Logical Operators

VBScript’s arithmetic operators for addition, subtraction and multiplication are the same as JavaScript’s. However, VBScript uses two division operators: a forward slash for floating point division and a backslash for integer division.

VBScript’s comparison operators for less than, greater than, less than or equal to and greater than or equal to are the same as JavaScript’s. But the equal comparison operator in VBScript is a single equal sign, ‘=’, and the not equal comparison operator is ‘<>’.

The logical operators in VBScript are:
  • And (logical AND)
  • Or (logical OR)
  • Not (logical NOT)
  • Imp (logical implication)
  • Xor (exclusive OR)
  • Eqv (logical equivalence)

14.3 VBScript Control Structures

Since VBScript does not use end-of-statement characters and curly braces to indicate “groups” of statements, the control structures in VBScript end with one or more keywords such as “End If”, “Wend” (for While End), “End Select”, etc.
14.3.1 The If Statement
Here are two examples of VBScript’s if statement:

(1) 
If s = x Then
   sum = sum + s
End If

(2)
If s = 0 Then
   sum = 0
Else 
   sum = sum + s
End If


14.3.2 The Select Case Statement
The Select Case statement is VBScript’s version of the JavaScript switch statement. The case statement in VBScript does not require the use of a “break” statement. Any variant type can be used in the case comparisons. The default case is implemented with a “Case Else” case statement. An example of a VBScript Select Case statement is:

Select Case choice
   Case 1
      Call MsgBox(“You chose Door #1.”)
   Case 2
      Call MsgBox(“You chose Door #2.”)
   Case 3
      Call MsgBox(“You chose Door #3.”)
   Case Else
      Call MsgBox(“You chose to keep the $200.”)
End Select


14.4 Iteration

There are several means of performing iteration in VBScript. VBScript has two means of iteration that are similar to the while loop in JavaScript: The While/Wend and the Do While/Loop.

The VBScript For/Next structure has a significantly different structure than the JavaScript for loop. Most importantly, the condition in the VBScript For/Next loop does not change once the loop begins. As an example, consider the following:

x = 6
For y = 1 To x Step 1
   x = x - 1
Next


The first line of the For loop above initializes y to 1 and increments y by 1 at each repetition of the loop. The condition “To x” means that the loop will continue while y is less than or equal to x. The thing to be careful about with the VBScript For loop is that the condition is fixed once the loop begins. That is, the condition is set at the beginning of the loop, in this case from 1 to 6, and even though x is changing during the execution of the loop, the condition of the loop does not change. VBScript has two iteration structures that do not have duplicates in JavaScript: The Do Until/Loop and the Do/Loop Until structures.

14.5 VBScript Math, Variant, String and Formatting Functions

VBScript furnishes a number of functions for common math functions such as absolute value abs(x), sine, cosine, logarithms, square root, etc.

VBScript also furnishes a number of functions to determine the data type is currently stored in a variable. These functions include IsArray, IsDate, IsEmpty, and IsNumeric.

VBScript provides a wider variety of string manipulation functions than does JavaScript. Functions are available for searching a string, the length of a string, comparing strings, trimming strings from the right or left, concatenating strings, replacing substrings, and more.

VBScript also furnishes a number of formatting functions for output. These include functions for currency, dates and percents.

14.6 VBScript Dialog Boxes

The VBScript equivalent of the JavaScript alert box is the message box. To create a message box, you need to call the MsgBox() function. The MsgBox() function takes 3 arguments or parameters. The first is a string that will be displayed in the box. The second is optional and allows the button on the box to be customized to another string. The third is a string which will be the title on the box. For example, the call

Call MsgBox(“The input was not a number”, , “Error”)


will create a dialog box with the first string displayed inside the box, “Error” in the titlebar and an “OK” button for the user to dispatch the message box. Note that even though the call above does not include the second, optional parameter, a blank space separated from the other parameters by a comma is required.

The VBScript equivalent of the JavaScript prompt box is the input box. To create an input box, you need to call the InputBox() function. The InputBox() function takes 5 arguments or parameters. The first is a string that will be displayed in the box. The second is a string that will appear on the title bar of the box. The third is optional and provides an initial value for the input area. The fourth and fifth parameters are x,y screen coordinates which determine where on the screen the box will appear. The unit of the coordinates is twips. (1440 twips is one inch). For example, the call

firstname = InputBox(“Enter your first name”, “Input”, , 700, 700)


will place the input box approximately one half inch down and one half inch to the right of the top left corner of the screen. Note that this function call does not include the word “Call”. Note also that this function returns the value entered by the user and this value is assigned to the variable “firstname”.

14.7 Defining Your Own Functions in VBScript - An Example

VBScript has two means of grouping statements that perform a function together. One is to define a subroutine and the other is to define a function. The main difference is that functions return a value and subroutines do not. Both functions and subroutines can take parameters. Functions return values by assigning a value to the name of the function.

The HTML file below contains a VBScript that defines a function called “WhichLarger” and a subroutine called “GetNums_OnClick”. Note that the function has assignment statements to the name of the function. Note that when the function is called in the subroutine GetNums_OnClick, the value returned by the function is assigned to a variable called “larger”.

The subroutine “GetNums_OnClick” is called when the button called “GetNums” in the form is clicked. Thus, the click event calls the subroutine.

<html>
<head>
<title>A Simple VBScript</title>
<script type = "text/vbscript">
<!--
 Option Explicit
 Dim intTotal, num1, num2

 Function WhichLarger(x, y)

  If(x > y) Then
    WhichLarger = x
  Else
    WhichLarger = y
  End If

 End Function


 Sub GetNums_OnClick()
   Dim larger, sum

   num1 = InputBox("Enter an integer", "Input Box", , 700, 700)
   num2 = InputBox("Enter an integer", "Input Box", , 3200, 3200)

   larger = WhichLarger(num1, num2) 
   sum = CInt(num1) + CInt(num2)

   Call MsgBox("The sum of the two numbers is " & sum & ". " &_
       larger & " was the larger of the two numbers.")
 End Sub
-->
</script>
</head>

<body>
Click the button to see what happens.
      
<form>
<input name = "GetNums" type = "button" 
             value = "Click Here" />
</form>
</body>
</html>

References

  1. Anderson-Freed, S. (2002) Weaving a Website: Programming in HTML, Javascript, Perl and Java. Prentice-Hall:NJ. ISBN 0-13-028220-0
  2. Dietel, H. M., Dietel, P. J. & Neito, T. R. (2001) Internet & World Wide Web: How to Program. 2nd Edition. Prentice Hall, NJ.

Cynthia J. Martincic
cynthia.martincic@email.stvincent.edu
CIS Department
Saint Vincent College
Latrobe, PA 15650