Chapter 18

Using Visual Basic Script on the Intranet


CONTENTS


This chapter continues the theme of Part IV of the book, "Advanced Intranet Publishing," in which we are discussing advanced functions you can build that will capitalize on your Intranet. Visual Basic Script, also called VBScript, or VBS, is a tool that you can put to good use even if you don't consider yourself a programmer. As you will soon see, VBScript is not very advanced after all, and it integrates well with HTML. If you can learn basic HTML, you can learn VBScript and put it to good use building more creative, and more useful, Web pages.

VBScript functions as an extension to HTML. It is a subset of the commercial Visual Basic language. As I write this chapter, VBScript is only available in the beta release of Microsoft Internet Explorer 3.0. As a beta release, the browser is still somewhat shaky, but the final release should be stable and available by the time you read this.

This chapter will explore the benefits of using VBScript. If you are wondering why you want to bother with it at all, I will explain what you would be missing if you decided not to take advantage of it. I'll then discuss the details of the language, including how it integrates with HTML forms and ActiveX controls.

I apologize in advance for the shortness of this chapter. VBScript is a very rich language and there is so much that could be covered; it really deserves a whole book dedicated to the subject. Unfortunately, my publishing deadline is looming and only a quick overview of the language is possible at this time.

The Benefits of VBScript

Soon after hearing about the Java programming language (invented by SunSoft), most programmers quickly began to realize the striking possibilities that it presents for client/server application development on the Web. By December 1995, the Internet buzz about Java was becoming so heavy that some analysts began predicting the eventual demise of Microsoft because it was estimated Microsoft couldn't possibly compete with something so revolutionary.

Microsoft answered by saying that it would license Java in future versions of its own browser, Internet Explorer. But Microsoft also countered Java by creating (or you might say, renaming) an alternative language. Microsoft designed VBScript, and we should include the overall ActiveX strategy in this discussion, to combat the Internet rage over Java. In my opinion, Microsoft has an idea that is going to be very successful. Let me tell you why I think so.

First, VBScript is easy to learn. Many people first learned programming using BASIC. VBScript is a small subset of Visual Basic-a language that already boasts about three million worldwide users.

Additionally, VBScript is an excellent language with which to take advantage of hundreds of existing OLE controls (as the vendors of those controls move them to the ActiveX model). What is the basis for this statement, you ask? Let's start with the premise that Visual Basic itself is a very successful product, and that VB is already being widely used to develop client/server applications using OLE controls (and their precursor, VBXs). As easy as Visual Basic is to learn, it still isn't something that just anyone off the street can do. It takes time to learn client/server techniques, and it takes time to craft a GUI and get it working in the VB development environment (even as fast as the interpreter is). Having the ability to build certain classes of network applications in a Web page designer is certainly going to make that process easier.

VBScript is lightweight and portable to other operating system platforms (at least that is what Microsoft claims they have achieved). Assuming for the moment that this statement is true, consider the implications. It means that programmers will have a much easier time creating client/server applications that run on numerous platforms. There will be no further need for expensive efforts to port a program that is tailored for one platform over to entirely different platforms. If VBScript is available in Web browsers on different operating systems, Microsoft and the browser vendors will have done your porting for you.

Today, VBScript, paired with HTML, is somewhat like a fourth-generation language. Although quite different from languages such as Delphi and PowerBuilder, VBScript and HTML are certainly head-and-shoulders above third-generation languages like C and C++. Currently, HTML authoring tools and editors are considered to be in their infancy. As better development tools become available for both HTML and VBScript, the ground rules of client/server application development are going to be radically altered for the better. I predict that HTML and VBS could eventually come to be known as fifth-generation programming languages due to the impact they will have on the pace of application development. Client/server application development is clearly going to become much more accessible and powerful than ever before.

Already, it seems obvious that Java is also turning traditional client/server programming on its head. Java has changed the rules of client/server development by making it so easy to develop true Internet-ready applications. Further, Java has wide industry backing, and SunSoft hopes to make it an open standard. Interestingly enough, Microsoft also hopes to make VBScript an open standard. It is giving away the object code, licensing the source code, and helping other browser vendors to port VBS to non-Windows platforms. Microsoft cites dozens of companies that have already thrown their support behind VBS and ActiveX. At the end of the day, it seems most likely that both Java and VBScript/ActiveX will succeed.

How to Learn More About VBScript and ActiveX
Here are several Web pages that you will want to visit to supplement the material in this chapter and the previous chapter. If you feel like you're hitting a stumbling block when you begin exploring VBScript in your own Intranet HTML pages, these resources should be able to lead you to the answer to any conceivable question:
  • Internet Explorer 3.0 Home Page-This site is an excellent branch point for everything about ActiveX.
    http://www.microsoft.com/ie/
  • ActiveX Control Gallery-This page contains several ready-to-run ActiveX controls that you can embed into Internet Explorer 3.0 HTML pages. You can download them from Microsoft for free!
    http://www.microsoft.com/appdev/controls/default.htm
  • Visual Basic Script Home Page-This is the home page for everything you need to know about VBScript. It includes a link to an excellent VBS tutorial.
    http://www.microsoft.com/vbscript/
  • Download the ActiveX SDK-If you are thinking about inventing your own ActiveX controls, or if you just want to learn more about the inner-workings of OLE DocObjects, you'll need this Software Development Kit.
    http://www.microsoft.com/intdev/sdk/
  • W3C Working Draft on HTML 3 Scripting-Although this paper has a focus on JavaScript, it shows the techniques for adding scripting logic and event handlers to HTML.
    http://www.w3.org/pub/WWW/TR/WD-script.html

Integrating VBScript with HTML Forms

Although VBScript is frequently discussed as the vehicle to manage ActiveX controls embedded in HTML pages, VBS can also be used without ActiveX controls. I'll present some VBS and ActiveX examples in a subsequent section, but for now, this section looks at the basics of VBS.

First of all, VBScript code goes inside ASCII HTML files. The VBScript code appears in the<HEAD> … </HEAD> section of the HTML file. Further, VBS code is nested within HTML comments within the <SCRIPT> … </SCRIPT> tags inside the <HEAD> section. If you think that sounds as if the code is being buried or obfuscated, you are probably right. But this is all necessary in order to preserve backward compatibility with existing Web browsers.

If you want to embed comments within your VBScript code, you can use the REM statement, which applies to a single line of code. Remember, HTML comments are in between <!-- and-->; that's different than VBScript comments.

The next thing to learn about VBS is that it can be tied to HTML forms. You won't have much need for VBScript if you don't have a <FORM> … </FORM> section in your HTML file. Figure 18.1 shows a very simple VBS HTML file running in Internet Explorer 3.0 (beta). Notice that the HTML page contains a trivial form with one command button. When the button is pressed, a custom message box appears. This result is quite different from the usual result of HTML forms and CGI programming in which pressing the button causes the form data to be sent to the Web server, essentially uninterpreted by the Web browser.

Figure 18.1: A sample Visual Basic script shown in IE 3.0 (beta version).

Listing 18.1 presents the VBS code for the simple HTML page shown in Figure 18.1. This file appears on the CD-ROM as vbscript1.htm.


Listing 18.1. The smallest Visual Basic Script page.

<HTML>
<HEAD>
<TITLE>VBScript Test</TITLE>
<SCRIPT LANGUAGE="VBS">
<!--
Sub Button1_OnClick
        MsgBox "Hello World!"
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H1>VBScript Test</H1>
<HR>
<FORM>
<INPUT NAME="Button1" TYPE="BUTTON"
       VALUE="Press here to see the message">
</FORM>
</BODY>
</HTML>

VBScript Variables and Operators

The only Visual Basic data type used in VBScript is the variant. The reason Microsoft chose to do this is that it significantly reduces the size of the VBScript language, making it faster to download. However, there is no major drawback to this decision because variants are capable of holding all the other basic data types, such as integers, strings, and floating point numbers (single and double precision).

In Visual Basic, a literal is a named constant value, either predefined (True, False, Null) or user-defined (such as MAX = 100).

VBScript also supports arrays of variables and collections of variables, just as in Visual Basic. Arrays can be indexed by integer values (using variants of course). Collections are analogous to an associative linked-list. By associative, I mean that you can give an item a name (key) when you insert it into the collection using the Add method. A collection is like a linked-list because the size of the collection grows dynamically as you add and remove items.

The following table lists the VBScript operators that can be applied to variables, literals, and constants in expressions.

Tables 18.1 and 18.2 list the various VBScript operators and their meanings.

Table 18.1. The VBScript arithmetic operators.

Operator
Meaning
+
Addition
-
Subtraction and Negation
*
Multiplication
/
Division
\
Integer Division
^
Exponentiation
Mod
Remainder Division
&
String Concatenation

Table 18.2. The VBScript comparison operators.

Operator
Meaning
=
Equality
<>
Non-equality
<
Less than
>
Greater than
<=
Less than or equal
>=
Greater than or equal
Is
Object comparison
&
String Concatenation

In addition, VBScript supports the same logical operators as Visual Basic: Not, And, Or, Xor, Eqv (a bitwise version of And), and Imp (performs logical implication).

Assignment statements can be made to variants with either = or Let. The Set statement is used to assign object variables, such as ActiveX controls.

The Err object is built into VBScript. It allows you to track and handle run-time errors. It has several properties which you can test or set.

VBScript Flow of Control Statements

VBScript supports all the types of control flow statements that you need to create rich programs without resorting to GOTO (no, it doesn't even support GOTO). The following bullet list shows the major control flow statements in VBScript.

In addition, VBScript supports both functions and procedures as subroutines. As you saw in Listing 18.1, a procedure is defined with the Sub keyword. Functions, which return a value, are introduced with the Function keyword.

Using ActiveX Controls with VBScript

Before you try to do any serious VBScript and ActiveX programming, make sure you check out the Microsoft ActiveX Control Pad editor. Currently, the beta version is available for free download from Microsoft at this URL: http://www.microsoft.com/intdev/author/cpad/.

The reason that you want to get your hands on this program is that inserting ActiveX controls into a Web page involves using very long OLE OBJECT identifiers as specified by the control vendor. The ActiveX Control Pad includes a tool to help you insert ActiveX controls onto your page without having to mess with the numbers.

It also includes a Layout Wizard for positioning the controls on the page and to assist you in writing the code for the event handlers. Figure 18.2 shows an example of the ActiveX Control Pad in action, so to speak.

Figure 18.2: The ActiveX Control Pad can create layouts and HTML documents.

Although the ActiveX Control Pad program was rather quickly developed as a solution to the problem of many HTML programmers of using NotePad to write code, there are many more capabilities present in this program than I have discussed here. Now that Internet Explorer 3.0 is out, and ActiveX is gaining momentum, there will be a rush of other software development companies also building ActiveX controls and HTML editors that support them.

Summary

This chapter examined the evolution of the Visual Basic language into a client/server Web programming language. VBScript is currently available only in Microsoft Internet Explorer 3.0. But it is being freely licensed to other Web browser vendors, and Microsoft has demonstrated VBScript running in Netscape Navigator 2.0 (and greater) through the ncompass Labs plug-in technology.

VBScript can be tied to traditional HTML forms with or without embedded ActiveX controls. When ActiveX controls are embedded in an HTML page, VBS can be used to process the form data very extensively before the data is finally submitted to the Web server.

The next two chapters move to the server-side of advanced Intranet publishing. I begin the discussion of CGI and ISAPI by exploring the fundamentals of HTML form processing in Chapter 19, "Getting the Most Out of HTML with CGI." Then Chapter 20, "Building a CGI Database System," will return to the subject of Visual Basic and databases by presenting the full source code to a sample Web application you can build and customize.