Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

Related items

un-Gratuitous Gradients

The Amazing ActiveX - Part 1

Browser Redirection using VBScript

Rattling Keys and Chasing Mice With VBScript

Introduction to Visual Basic Scripting (VBScript)

You are here: irt.org | Articles | VBScript | Introduction to Visual Basic Scripting (VBScript) [ previous next ]

Published on: Friday 18th September 1998 By: Lawrence Elliot

Introduction

VBScript is a subset of the Visual Basic Programming language. The result of the slimming down process is a very small language that is easy to use. You may be interest to know that there is also available Visual Basic for Applications (VBA) for use with Microsoft Word, Excel, Access etc.

JavaScript can do all that VBScript can do and far more. So why use VBScript instead of JavaScript? Because VBScript is much easier to learn than JavaScript.

For instance, here is the VBScript code to request the users name and display it in a message box on your web page and print your name in large letters:

<HTML>
<HEAD>
<TITLE> "VBScript Demo"</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!--
' Your Script goes here.
YourName = InputBox("What is your name?")
MsgBox "YourName"
' or you can display YourName like this:
Document.Write" <H1>"
Document.Write YourName
Document.Write" <\/H1>"
- ->
</SCRIPT>
</HEAD>
</BODY>
</HTML>

There is one big drawback to using VBScript and that is that it isn't supported by non-Internet Explorer browsers.

Much of the power of VBScript comes from its ability to control the thousands of ActiveX controls that are available. It is a fairly straight forward exercise to create your own ActiveX controls using ActiveX Control Pad which is free from Microsoft. For the people who still use Notepad to write their HTML files, you really need to use Control Pad. For instance, you may call a text box a "text box" but to every machine in the world its name is really: 8BD21D10-EC42-11CE-9E0D-00AA006002F3

The reason behind this great long number is that if the person displaying your web page speaks a different language, the persons computer displays everything in the other language. Don't be put off by the long CLASSID number, you don't have to remember it or even type it in, Control Pad does it all for you.

VBScript Syntax

VBScriptJavaScript
Arrays Declaration (e.g., Dim, Static)
Lbound, Ubound
ReDim, Erase
new Array()
new Object()
Assignment =
=
Comments REM
Single Quote (')
//
/* comment */
Control Flow Do ....Loop
For ...Next, For Each ...Next
While ...Wend
If ... Then ...Else
do {
    statements
} while (condition)

for (expression; condition; [expression]) {
    statements
}

for (variable in object) {
    statements
}

while (condition) {
    statements
}

if (condition) {
    statements1
}
else {
    statements2
}

switch (expression){
    case label : 
        statement;
        break;
    case label : 
        statement;
       break;
    ...
    default : statement;
}
Error Trapping On Error
Err Object
window.onError = handler-func
window.onError(message,url,line)
Literals Empty
Nothing
Null
True, False
User Defined Literals(e.g., 123.456, "test")
NaN
null
true, false
User Defined Literals(e.g., 123.456, "test")
Operators Arithmetic +, -, *, /, \, ^, Mod, Negation ( -)
Strings concatenation (&)
Comparison =, <, >, <>, <=, >=, Is
Logical Not, And, Or, Xor, Eqv, Imp
Arithmetic +, ++, -, --, *, / %
String + +=
Logical && || !
Bitwise & ^ | ~ << >> >>>
Assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
Comparison == |= > >= < <=
Special ?: , delete new this typeof void
Options Option Explicit
N/A
Declaring Procedures Functions
Sub
function function-name(arg1, arg2, ... argN)
Calling Procedures: Call
function function-name(arg1, arg2, ... argN) { statements; }
Exiting Procedures Exit Function
Exit Sub
Parameters for Procedures ByVal, ByRef
Procedure-level Variables Dim
Static
var variable-name
Module level Variables Private
dim
var variable-name

When one uses Option Explicit it forces the user to declare all variables. If one tries to use a variable without declaring it an error is generated. If one doesn't use Option Explicit one is free to use variables without declaring them, however, this is bad programming practice as it is easy to misspell a variable and produce faulty code.

VBScript Operations

VBScriptJavaScript
Divide & return an integer value\ N/A
Exponentiation ^ N/A
Modulas MOD %
String Concatenation + or & +
Left Shift N/A <<
Right Shift N/A >>
Bitwise AND AND &
Bitwise OR OR |
Bitwise XOR XOR ^
Bitwise complement NOT ~
Not equal to <> !=
Evaluation AND AND &&
Evaluation OR OR ||
Evaluation XOR XOR N/A

VBScript Functions

A Function is a way of dividing up a program into reusable chunks. A Function is usually passed a list of variables, performs action upon these variables and returns a value.

Function Name(arguments)
    code body of function
End Function

Similarly Subs in VBScript:

Sub
    code body of function
End sub

Working with Strings

Trim, Ltrim, Rtrim:

MyString = "   This is a Test  "
NewString = Trim(MyString)     ' NewString = "This is a Test"
NewString = Ltrim(MyString)    ' NewString = "This is a test   "
NewString = Rtrim(MyString)    ' NewString = "   This is a test"

Len:

MyString =  "Test this string"
l = Len(MyString) ' l = 16

Mid:

NewString = Mid(MyString,1,1)  ' NewString = "T"
NewString = Mid(MyString,6,4)  ' NewString = "this"

Left:

MyString =  "Test this string"
NewString = Left(MyString,3)   ' NewString = "Tes"

Right:

MyString =  "Test this string"
NewString = Right(MyString,4)  ' NewString = "ring"

For example to count the number of spaces in MyString:

<HTML>
<HEAD>
<TITLE> "VBScript Demo"</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!--

Dim MyString, SpaceCount, Length, Position
MyString = "This is my test string"
SpaceCount = 0
Length = Len(MyString)

For Position = 1 to Length
If Mid(MyString, Position, 1) = Chr(32) Then ' Chr(32) equals the space character.
SpaceCount = SpaceCount + 1
End If
Next 
MsgBox SpaceCount

'SpaceCount would equal 4
-->
</SCRIPT>
</HEAD>
</BODY>
</HTML>

InStr and InStrRev. Syntax is: Instr([start,] OriginalString, SubString{, Compare])

Start defines the start of the search and is optional, Compare lets you defining whether the search should be case sensitive and is also optional.

MyString = "This is a test"
Position = InStr(MyString, "s") ' Position = 4
Position = InStrRev(MyString, "s") ' Position = 13
Position = InStr(5, MyString, "s") ' Position = 7

Arrays

Fixed Arrays

A one-dimensional array is a way to group items so that you can refer to each item individually by means of its Index:

Dim MyButton(4)

This means that you have five elements in an array, MyButton(0) to MyButton(4).

Dynamic arrays

Dim MyButtons()
NumberOfButtons = InputBox("How many Buttons")
ReDim MyButtons(NumberOfButtons)

Format
MsgBox FormatDatTime(Now, vbGeneralDate) ' 15/6/98 5:15:20 PM

vbLongDate Monday, June 15, 1998
vbShortDate 15/6/98
vbLongTime 5:15;20 PM 
vbShortTime 17:15 (24-hour clock) 

ActiveX

Much of the power of VBScript comes from its ability to control the thousands of ActiveX controls that are available.

ActiveX Control Pad

ActiveX Control Pad available free from Microsoft combines a simple HTML editor with some very powerful features for working with ActiveX Controls. The key is that ActiveX Control Pad will let you do the following: Specify the type of ActiveX control, such as command button or text box that you want to insert.

ActiveX Control Pad comes with a tool named Script Wizard. Script Wizard will generate code automatically when you give it directions, however, it only generates the code for the controls used. If you need additional operability you will have to add the code by hand.

Visual Basic 5 Control Creation Edition (VB5CCE)

Visual Basic 5 Control Creation Edition is another way in which you can create your own ActiveX controls and again it is available from Microsoft and again its free. VB5CCE can create full blown ActiveX controls as 32-bit OCX's.

VBScript FAQ

What is VBScript?

VBScript is the name given to Microsoft's Visual Basic Scripting Edition. It is a subset of the Visual Basic for Applications (VBA) language although it has a few features that haven't yet been introduced into VBA. It can also be use to program Windows95 and Windows NT by using the Microsoft Windows Scripting Host.

How do I get VBScript?

VBScript is part of Internet Explorer version 4 or later for the Windows platform. This means that millions of people have VBScript and don't even know it!

Where can I find more information on VBScript?

Have a look at VBScript at Microsoft. This site has a list of non-Microsoft sites that you might find useful. It also contains information about VBScript news groups.

How does VBScript compare to JavaScript?

JavaScript is a language designed by Netscape Communications for programming Web pages. Thus VBScript and JavaScript are designed to do the same job. In my opinion JavaScript has a much steeper learning curve than VBScript since JavaScript uses syntax similar to the cryptic C programming language.

Books on VBScript?

There are several books on the subject of VBScript available. Have a look at http://computer-manuals.co.uk. I can recommend "Learning Visual Basic Scripting Edition Now" from Microsoft Press ISBN 1-57231-347-1.

Related items

un-Gratuitous Gradients

The Amazing ActiveX - Part 1

Browser Redirection using VBScript

Rattling Keys and Chasing Mice With VBScript

©2018 Martin Webb