Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Teach Yourself VBScript in 21 Days

Day 2 -- The Essence of VBScript

DAY 2

The Essence of VBScript


CONTENTS




You've probably seen and heard a lot of excitement surrounding Microsoft's new Internet development platform and suite of Internet tools. This new technology is indeed very exciting to almost everyone in the computer industry today. When any new technology emerges, it takes time for the technology to "settle down." The creators of the technology first fill the airwaves with hype and send out beta copies of what they're working on. Finally, a solid release of the product emerges, and builders can actually start making full use of the technology that has been discussed for so long. After that, it takes a certain amount of time for the intended users of that technology to adjust to how it works and how to use it.

If you've heard a lot about VBScript and what it can do but you've been bombarded with so much new information that it's hard to absorb it all, you're not alone. Today's lesson was written for you! It will clearly and methodically explain to you the essence of VBScript-that is, what it's all about. Rather than drop a load of technical bombs that only a rocket scientist can understand, I explain in plain English why VBScript is exciting and what it can do for you. Here is a list of some of the questions I will answer today:

You will also find several examples of Web pages that use VBScript so that you can see just how powerful and revolutionary VBScript really is. Think of today as a friendly introduction to the world of Internet programming with VBScript. You won't get hit with a lot of techno-jargon in this lesson, but you will see just what you can do with this new and exciting language.

What Is VBScript?



When the World Wide Web first became popular, HTML was the only language programmers could use to create Web pages. They soon learned that HTML was quite limited in what it could do. It presented the user with a "page" of information, but the Web page and the user had a limited amount of interaction; it was like reading the front page of a newspaper on a computer monitor. Now most computer users, whether they use Windows, Macintosh, UNIX, or a combination of the three, are accustomed to graphical applications that provide interaction. They're used to clicking buttons, entering values into text boxes, and choosing from menus. The only way to get useful work done with a computer is to interact with it. The first generation of Web pages provided information to the users, but the users could not interact with the Web the way they could with their word processors. The interaction available to them required that they send the data to the server, where all the "smarts" were provided. The results were then sent back to the Web page. This interaction required a great deal of extra time, effort, and overhead, and the interface presented to the user was very constrained compared to the applications they were accustomed to using.

Fortunately, the builders of the Internet and the World Wide Web could see these limitations. They soon realized that if the user was denied the capability to interact with the Web page, it would become little more than a collection of information, much like a library of books. Although that collection is very useful, users demand more from their computers than what they could get elsewhere. The capabilities of HTML began to grow and become more powerful. Soon, designers began to realize that they needed more than just HTML to make the Internet accessible and useful to the masses. Corporations who wanted to develop enterprise solutions or make money off the Internet also began to put pressure on designers to give them something more.

These demands have resulted in a continued improvement of HTML, the emergence of browsers such as Internet Explorer that tap into the power of HTML, and the advent of scripting languages such as VBScript. To understand what a scripting language is, think of HTML as an airport runway. You can get where you need to go on the ground, but you have an entire sky to travel through above you. Scripting languages are like the airplanes that make it possible for you to lift off the ground. They extend the capabilities of the Web much like an airplane extends the capabilities of man so he can travel through the sky. A script lets the page become an active, dynamic piece of software rather than a static piece of content.

A scripting language is a type of programming language used to provide control in another host environment. It is interpreted rather than compiled. This means that a program built with a scripting language must be run in the environment that contains the scripting language's interpreter and cannot be run as a stand-alone application.

HTML can't interpret a scripting language itself, but it knows enough to call the interpreter of the scripting language to carry out the interpretation. This enables you to go above and beyond HTML, using any browser-supported scripting language you want to extend the Web page. With scripting languages such as VBScript, the limitations of HTML disappear; the opportunities are now limited only by the scripting language itself! Although other scripting languages are bound to emerge, two widely used scripting languages in existence today are JavaScript and VBScript. Both of these languages can be embedded in a Web page, and if the browser supports them, they provide the path to smart, active programs that are part of that page.

With HTML, you can place controls such as buttons and text boxes on a Web page. Without a scripting language such as VBScript, any actions the user takes on the controls of a Web page must be sent back to the Web page server. They cannot be handled at the user's computer. Furthermore, the amount of control and flexibility available without a scripting language is very limited. With VBScript, you not only can link up to controls on the Web page, but you can also write code to respond to what the user does with those controls. If, for example, a Web page contains a command button, you can write VBScript code that gets executed immediately when the user clicks that button. An example of such a code segment might look like the following:

Sub Button_OnClick
   ' The message below will be displayed when the user clicks on the button
   Msgbox "This button was clicked"
End Sub

Button is the name of the button, and OnClick is the event that the user causes to occur when he or she clicks the button. You can supply code, such as the message statement shown here, to be executed every time the user clicks the button. The MsgBox statement, which will be covered in more detail on Day 14, "Working with Documents and User Interface Functions," simply presents a message box to the user. This example is simple, but it shows how VBScript code can give a page the capability to respond immediately to user actions. Rather than present the user with a lifeless Web page, VBScript breathes life into Web pages, making them dynamic, responsive, intelligent, and interactive.

What Can VBScript Do?

VBScript lets the user interact with a Web page rather than simply view it. There are many possible scenarios for this interaction. For instance, this capability to interact makes it possible for Web pages to ask questions and respond to how the user answers them. VBScript can then take input from the user and check the data to make sure it is valid or meets certain criteria. Then, it can put an Internet server to work either by actually storing the data or causing some action to take place on the server based on the information given. For example, VBScript could respond to a user's request for an airline reservation. It could read in the data, check to make sure all fields have been filled out and that the phone number and zip code are in a valid format, inform the user of the estimated price, and then notify the server of the reservation. All these tasks could be carried out by the code in the Web page that was downloaded across the Internet as it sits on the user's client pc. The server in turn would make sure there is an open slot for the customer for when he wants a ticket and then book the flight and get tickets out in the mail to the customer.

Interaction can also be helpful in advertising services or products to a user. Through an interactive survey, you can target exactly what the potential customer is looking for. Imagine, for example, a series of Web pages that ask you all kinds of questions about your dream car, along with what you want to spend, and then give you a series of vehicles that match your criteria. Rather than dealing with a pushy salesperson, you can take your time on the Internet and carefully research the facts. When you go to the dealer, you can be specific and avoid having to wrestle with a salesperson.

In this type of example, VBScript can play an important role in many ways, including validating data, pricing, providing impressive multimedia feedback, and initiating data storage. You can use VBScript to sequence the questions based on responses. For example, if a user indicates he wants a van, VBScript can generate an input box asking him how many seats he wants. Throughout the data entry process, VBScript can make sure the user enters a valid order, address, and method of payment, and it can even present him with a pie chart of how much of the cost goes toward the base price and how much goes toward extras. The generation of the bar graph can be accompanied by the sound of a trumpet fanfare. The possibilities are endless.

VBScript can also perform calculations on data, such as computing the cost of an item after taking into account the sales tax. Often, calculations on a Web page are useful in providing the user a way of figuring out what he wants to do, or perhaps giving the user some sort of result he is seeking. In this way, your Web page enables the user to walk away with more than a mere presentation of fixed information. You could, for example, allow the user to choose what luxury items he wants on a car and, as the luxury items are selected, keep adjusting the overall cost. The user could spend as much time as he wants and choose as many combinations as he likes until the perfect combination of features versus price is calculated. How often can you do that at a car dealership?

By utilizing other technologies such as CGI, VBScript code can even initiate order placement for that item in the computer of the company that is selling the item. If the script determines all criteria for a valid order are met, it can place the order. Otherwise, it can generate an error message. Using script logic, it could even place the order on a different server, depending on which type of car was requested, and provide an estimated time frame for delivery based on a rules-of-thumb calculation for that type of order. VBScript can carry out virtually anything you can think of that a traditional application could carry out. Even in areas where VBScript can't directly cause some action such as writing to the server database or playing a sound file, it can achieve these results indirectly by making use of CGI scripts or sound controls, for example. VBScript becomes the application behind the Web page that the user views and interacts with.

Another important aspect of this programming model is that you can also use the intrinsic HTML form controls and Microsoft's ActiveX controls with VBScript to give Web pages an attractive look and feel. Intrinsic HTML form controls, which are discussed in Days 8, "Intrinsic HTML Form Controls," and 9, "More Intrinsic HTML Form Controls," provide the Web page developer with a standard set of controls similar to those used in the Windows environment. ActiveX controls, which are discussed in Days 10, "An Introduction to Objects and ActiveX Controls," and 11, "More ActiveX Controls," consist of useful controls such as graphs and charts, labels that can be rotated 360 degrees, a timer control that enables you to time events on Web pages, a pre-load control that lets you load bitmaps and other time-consuming parts of a Web page before it gets displayed, and so on. These controls further enhance Web pages to give them a professional, polished look. They also provide pages with smarter interactive response because the control characteristics can be controlled dynamically by a VBScript program. For example, your code can generate a new graph based on the user's input on a page.

Designers can now place ActiveX custom controls directly on Web pages in the Windows environment. OCXs, the forerunner of ActiveX controls, have made languages such as Visual Basic incredibly powerful because programmers can take an OCX that performs some task for them, such as displaying a calendar on the screen, and "glue" that component into the application. They don't have to create code to put a calendar on the screen; the OCX already does that for them. Likewise, programmers can now put an ActiveX control on a Web page and access the control through VBScript. The goal is to make Web pages capable of what a regular Windows application can do. There are some restrictions on what can be achieved due to security (addressed in Day 21, "Security, Stability, and Distributed Source Control Issues"), but the technology is rapidly moving in that direction. The Internet might someday be the "platform" that all our applications rest upon and work within.

In addition to using ActiveX controls, the current beta version of VBScript can also tie other applications into a Web page through OLE automation technology. For example, with the appropriate object declarations, you could tie an Excel spreadsheet into your Web page so that when the user clicks on the spreadsheet, Microsoft Excel runs and loads the spreadsheet for you to edit. Now, not only can you work within a Web page, but you can bring other applications into the Web page that can be activated at the click of a mouse button. This capability to tie other applications to a Web page lets you show virtually anything on a Web page; the only requirement is that the applications you want to link to a page support the OLE automation standard.

With VBScript and the right controls, you can even create 3-D animation effects, making your Web page come alive with moving objects in response to certain events. You can use animation to make cars careen across the screen, butterflies fly across your Web page, or arrows move and point to where you want the user to interact with the Web page. Although this might seem quite esoteric, animation does help attract the users' attention and makes them feel like they're working with a living, breathing entity. They're more likely to explore your Web page and stay tuned in to it when a lot of neat animated effects grab their attention.

The component incorporation capabilities of VBScript introduce some special considerations and trade-offs in page design. A Web page that includes VBScript code that interacts with Excel and Microsoft Word, as well as the currently Windows-specific ActiveX controls, is not one that can be fully distributed over the Internet with all the support software. It also might not run easily on any platform and operating system that could use it. On the other hand, such a page would be very powerful for Internet or intranet users who fit the target user profile. At the other end of the spectrum, a page that incorporates VBScript to carry out a series of calculations can be a perfect Internet citizen, fully downloadable over the Internet and running on a variety of platforms and operating systems. In this respect, the flavor and tone of your VBScript Web pages depends largely on how you want to leverage it. This is an important theme that we discuss throughout the remaining days' lessons.

Note
Microsoft may support ActiveX controls on other environments such as the PowerMac in the future, but they are currently supported only in the Windows environment.

In order to gain a more complete understanding of how VBScript works with browsers, controls, and objects, consider the simple model shown in Figure 2.1.

Figure 2.1 : A simple diagram of the VBScript-host model.

In this model, the innermost box is where the core language of the scripting language resides (in this case VBScript). Other languages such as JavaScript could be placed in this box as well. This core language cannot be run alone-it must be provided with a host that supports it. This leads to the second, larger box in the figure.

The second, larger box represents the host program that runs the scripting language. At the time of this printing, VBScript could be used with Microsoft Internet Explorer 3.0, which in this case would be the host. While the host may vary, the core VBScript language stays the same. In other words, the VBScript language is the same regardless of the host, whether it be Internet Explorer or any other product. The host may contain a set of internal objects and controls, such as the case with Internet Explorer 3.0. For example, you can access Internet Explorer's document object from VBScript, which will be discussed further on Day 14. You can also access Internet Explorer's intrinsic HTML controls through VBScript (this is discussed on Day 8). Any scripting language, such as JavaScript, has the same type and level of access to these browser objects and controls as does VBScript.

The third box represents components external to the browser but available in the Windows environment, such as ActiveX controls, OLE controls, and objects such as OLE automation servers. VBScript can access these controls and objects through the browser, as illustrated by Figure 2.1. In order for VBScript to access a control at this level, it must be linked through the host. Each link of the chain must be complete before VBScript can access any objects or controls throughout the chain. Since the browser is a secure environment, the user will be presented with appropriate warnings before a control is used by a script if default browser options are in place.

It's important to remember that any scripting language operates using this model. JavaScript, another powerful scripting language, would occupy the same position in the figure as VBScript does. This means that any scripting language has access to the browser's controls and objects as well as the system's controls and objects if supported by the browser.

The variety of controls and technologies that surround the World Wide Web is likely to increase dramatically over the next several years. Although it can be quite dizzying to keep up with the changes and new controls on the market, it's an exciting time to be a part of the Internet revolution. Now that you have a glimpse of the power of VBScript and the technologies that surround it, take a look at the examples in the next section. They will help you see some of what VBScript is capable of!

Some Examples of VBScript in Action

To help you appreciate and understand the power that VBScript gives you, I provide three simple examples in this lesson. Each example is on the CD-ROM that accompanies this book. You might want to load them into your browser and take a look at them for yourself. All of the examples presented here are very simple, but they help give you a taste of what lies ahead for you in building your own Web pages.

Note
You can run samples in this book over the Internet as well as from the CD-ROM provided with the book. Refer to Appendix B, "Information Resources," for details on where to find the samples on the World Wide Web.

Example #1: The DiscoTec Web Page

Imagine, for the moment, that you're the proud owner of a music store, and business is booming. You're always interested in more business, and you see tremendous potential in the Internet to help you sell your product. In fact, you want to do more than simply advertise; you want your customers to be able to figure the cost of the music they want to order over the Internet! This first example shows what you can do to make that possible. Take a look at the Web page shown in Figure 2.2, where you see a "costing form" page that lets the user calculate the cost of an order of music.

Figure 2.2 : The Disco Tec Web page.

This page appears to the user after you have presented him or her with one or more pages showing the selection of music you offer. You could even get fancy and show a picture of each album cover and, when the user clicks on the cover, play a sample of the music contained on the album. You will see more about the concepts of using controls for such purposes on Days 10 and 11.

As you can see, the user enters his home state along with the number of cassettes or compact discs he wants. Then he clicks the What's the Cost? button. When the user clicks this button, the Web browser initiates VBScript code that calculates the cost for shipping and handling, adds in the sales tax, and presents the user with the total cost.

Note
This Web page, named discotec.htm, can be found on the CD-ROM that accompanies this book.

Listing 2.1 shows the Web page source code, which includes the VBScript code that gets executed when the user clicks the What's the Cost? button.


Listing 2.1. The HTML source code for the DiscoTec Web page.
<HTML>

<HEAD>
<TITLE>The DiscoTec Example</TITLE>
</HEAD>

<BODY>

<H1>
<A HREF="http://www.mcp.com"><IMG  ALIGN=BOTTOM
SRC="../shared/jpg/samsnet.jpg" BORDER=2></A>
<EM>The DiscoTec Music Company</EM></H1>
<HR>

<H2>What's It Gonna Cost Me?</H2>
<P>To determine the cost of an album, enter the order number of the album
you're interested in followed by the quantity desired. Cassettes cost
$15.00 each, and compact disks cost $25.00 each. Residents of California must
pay an additional sales tax of 6%.

<PRE>
     The State you live in: <INPUT NAME="txtState" SIZE=2>
       Number of Cassettes: <INPUT NAME="txtCassettes" SIZE=5>
   Number of Compact Disks: <INPUT NAME="txtDisks" SIZE=5>

</PRE>

<CENTER><B><INPUT TYPE=BUTTON VALUE="What's the cost?" NAME="cmdCost">
</B></CENTER>

<PRE>
    Cost                    $<INPUT NAME="txtCost" SIZE=10>
    Sales Tax (6% CA only)  $<INPUT NAME="txtTax" SIZE=10>
    Shipping (12% of cost)  $<INPUT NAME="txtShipping" SIZE=10>
<BR>
    Total Cost              $<INPUT NAME="txtTotal" SIZE=10>
</PRE>

<HR>

<center>
From <em>Teach Yourself VBScript in 21 Days</em> by
<A HREF="../shared/info/keith.htm">Keith Brophy</A> and
<A HREF="../shared/info/tim.htm">Tim Koets</A><br>
Return to <a href="..\default.htm">Content Overview</A><br>
Copyright 1996 by SamsNet<br>
</center>

<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit

   Sub cmdCost_OnClick()

      Dim State
      Dim Cassettes
      Dim Disks
      Dim Cost
      Dim Tax
      Dim Shipping
      Dim Total

      Cassettes = txtCassettes.Value
      Disks = txtDisks.Value
      State = txtState.Value

      If Cassettes = "" Then
         Cassettes = 0
      Else
         Cassettes = CInt(Cassettes)
      End If

      If Disks = "" Then
         Disks = 0
      Else
         Disks = CInt(Disks)
      End If

      If Cassettes + Disks = 0 Then
         MsgBox "You must enter at least one cassette or disk."
         Exit Sub
     End If

     Cost = 15.00 * Cassettes + 25.00 * Disks

     If State = "CA" Then
        Tax = Cost * 0.06
     Else
        Tax = 0
     End If

     Shipping = Cost * 0.12

     Total = Cost + Tax + Shipping

     txtCost.Value = Cost
     txtTax.Value = Tax
     txtShipping.Value = Shipping
     txtTotal.Value = Total

   End Sub

-->
</SCRIPT>

</BODY>
</HTML>

If the code doesn't make any sense to you, don't worry-it doesn't have to yet. You can see, however, that a fairly small amount of code is needed to do the calculations for the total cost of the items, and it's all accomplished on the user's computer. Without a scripting language, this kind of application would be impossible on the user's computer; the data would have to be transmitted back to the server, calculated there, and sent back. That would take considerably more time because of all the delay in getting the information back and forth, not to mention the overhead required to carry out the transaction.

One of the powerful benefits of VBScript is its capability to ensure the validity of the data the user enters. Suppose, for example, that the user enters a negative number for the quantity of an item. Obviously, this is an invalid amount. Fortunately, VBScript can validate the data the user enters. If, for example, the user doesn't enter a quantity either for cassettes or CDs, the message shown in Figure 2.3 appears when the user tries to calculate the cost.

Figure 2.3 : What hapens when the user forgets to enter the quantity.

This example shows you how you can make a Web page intelligent with VBScript. In addition to presenting a Web page that shows simple text and graphics, you can also truly interact with the Web page. Not only can you retrieve and process data entered by the user, but you can also perform checks to make sure the information he enters is valid, and if it isn't valid, you can take the steps necessary to make sure he enters the information correctly.

Example #2: The Pace Pal

This next example of VBScript is very useful to a runner, jogger, or walker. You can use the Web page in this example to calculate your pace from a running, jogging, or walking workout. All the user has to do is enter the distance traveled and the time it took to travel that distance. Then, VBScript code calculates the pace and displays it back to the user. Figure 2.4 shows the Web page.

Figure 2.4 : The Pace-Pal Web page.

Figure 2.4 shows a text box where the user enters the distance traveled. He must choose the units of travel as either miles or kilometers. Notice the two special controls to the right of the text box. You use these special controls, called radio buttons, to limit the user to selecting one of the choices. VBScript can interact fully with controls such as these. You are not merely limited to text boxes and buttons as in the first example. Radio buttons, along with other important controls, will be discussed on Day 8.

Having entered the distance, the user then enters the time in the designated text box. Once the data is entered correctly, the user clicks the Display Pace button, which starts the calculation of the runner's pace. The result is displayed in miles and kilometers as shown on the page. This example once again shows you the benefits of VBScript. The pace calculation can be done entirely on the user's computer with no Internet involvement. Once the page is loaded into the browser, VBScript does the rest. You could, in fact, even disconnect from the Internet and use this page offline.

Note
This Web page, named pacepal.htm, is also on the CD-ROM that comes with the book.

This example shows you how you can design Web pages to assist the user in some way. In a sense, a Web page can become an application delivered over the Internet. Before the Web page phenomenon grew so popular, you would probably write such an application using C++ or Visual Basic. Then, you might distribute the application as shareware. With the World Wide Web and VBScript, however, anyone who uses the Internet has instant access to your application, and it appears very attractively on a Web page. If you want to share your application with friends across the country, it's much easier for you to point them all to your Web page rather than send each of them a copy of it.

Example #3: The Bird Feeder

The last example today is a simple Web page that provides assistance to the user and helps advertise a product at the same time. This Web page is targeted to your average cockatiel owner. It determines just how much bird seed you need to feed your cockatiel each day over the next month. By the way, this example is purely fictitious, so please don't base your own cockatiel's diet on the results! Figure 2.5 shows the Web page.

Figure 2.5 : The Bird Feeder Web page.

This simple Web page asks the user for the age, sex, activity level, and weight of your cockatiel. Notice that when you enter the sex of the bird, you can only choose Male or Female through the use of the special radio button controls. When you enter the activity level of the bird, you can choose from a variety of selections that are contained in a special control called a list box. The list box gives you the ability to select one of several choices, but you cannot enter a new choice like you can in a text box. On Day 8, you will learn more about list box controls and how to use them.

When you click the Calculate button, VBScript performs some simple calculations to determine the amount of bird seed required. The formula used to calculate this amount is in no way scientific and should not be considered valid if you want to use this on your own bird. It's just a fictitious example to show you what you can do with VBScript. Still, it shows you the potential of providing this service to the public. If you were selling bird seed, this Web page might be just enough of a convenience for the user to click the handy little Order Seed button at the bottom of the Web page!

Note
This Web page, named birdfeed.htm, is also on the CD-ROM that comes with the book.

These examples show only a fraction of what you can do with VBScript. All these examples involve taking input from the user, processing that input, and providing the results back to the user on the Web page. As you progress through the book, you will see how to use ActiveX controls, OLE, Java applets, CGI, animation, sound, and other useful features to make Web pages even more powerful. If you've used the Internet for a while, you can begin to appreciate just how useful VBScript really is. We've come a long way from the static text-and-graphic Web pages, and the journey has only begun.

Learning VBScript

VBScript is much easier to learn than programming languages such as C/C++ and other scripting languages such as JavaScript. Derived from the Visual Basic language, VBScript should not be difficult for anyone who has done computer programming before. If you have done no programming whatsoever, it might take you a bit more time to come up to speed, but rest assured-you will. The most effective way to learn VBScript is to read this book and practice creating your own Web pages with VBScript. Reading a book is never enough unless it is accompanied with experience and practice. Fortunately, this book doesn't just tell you how to use VBScript; it shows you how and gives you exercises to try on your own. You can count on the fact that you will get knowledge and experience as a result of working through this book.

You need to know how to do two things before you begin to write VBScript code. First, you need to know how to use a browser and navigate through the World Wide Web. Second, you should be somewhat familiar with the Microsoft Windows environment and how to navigate your way through a Windows application, or have equivalent experience in another graphical user interface (GUI) environment. If you want to use VBScript to design Web pages, chances are you already have a browser and are quite capable of using it. You are probably very familiar with GUI applications as well. If so, the only remaining obstacle is learning how to program with VBScript. The more programming you have done in the past, the better you'll pick up on VBScript, especially if you've written programs using Visual Basic or Visual Basic for Applications. If you've never written a computer program, don't worry: You don't need a programming background to use this book.

To start working with VBScript, you need several things: a browser that supports VBScript, the VBScript run-time interpreter, access to required controls, and an editor or other tool to help you assemble Web pages or edit HTML documents. The first step is to obtain a browser that includes VBScript run-time support if you do not already have one. Microsoft's Internet Explorer 3.0 was the first publicly available browser that provided support for VBScript. This browser is available free of charge from Microsoft. At the time this was written, you could obtain Internet Explorer 3.0 from http://www.microsoft.com/ie.

The VBScript run-time interpreter will usually be included with any browser that supports VBScript. This is the case with Internet Explorer 3.0. If you obtain the browser and install it, you have everything you need to run VBScript, including the run-time interpreter. No separate installation is required to provide run-time capabilities. You don't need to obtain any other pieces. However, you might want to refer to the location http://www.microsoft.com/vbscript if you want a general description of run-time capabilities beyond that provided in this book. The run-time interpreter for VBScript is license free, just like Internet Explorer. Even if other browsers incorporate the VBScript run-time interpreter, Microsoft does not charge a licensing fee to the end user or the company that produces the browser. Therefore, it is a safe bet that the VBScript interpreter will be widely distributed with most browsers in the future.

If you've obtained Internet Explorer 3.0, you're nearly all set to run VBScript programs. You also will need ActiveX controls from Microsoft for many of the samples in this book. Pages can be set up with control definitions to automatically download required controls from across the Internet. For best results we recommend downloading the entire ActiveX control collection from Microsoft before proceeding with the samples in the book. This also allows you to work with the samples from the book's CD-ROM without being connected to the Internet. The requirements page on the CD-ROM will download the controls the samples require. Be sure to check the book update sites, www.mcp.com and www.DoubleBlaze.com, to ensure you're using the latest version of the requirements page. At the time of this printing, you could also go to www.microsoft.com/intdev and do a search on ActiveX controls to find information about ActiveX controls.

Once you get set up, you can directly load the book VBScript pages from the CD-ROM or visit the Microsoft samples over the Internet (at www.microsoft.com/vbscript), or from any other site for that matter, and the VBScript capabilities will work right in front of you. If you're merely an end user of the Web, that's all you need to enjoy the power of VBScript programs. If you are a VBScript programmer-in other words, you want to edit VBScript programs or create your own from scratch-then you need one more tool, a Web page editing tool. Such a tool should not only let you generate Web pages, but it should also enable you to embed VBScript code into those pages. Fortunately, most tools provide this capability. As long as you can enter text insertion mode and type text directly into your page as you create it, you can insert VBScript statements. You can use many different tools to accomplish this. They range from the Windows Notepad, a simple text editor, to more sophisticated tools such as Internet Assistant, a utility that incorporates itself into Microsoft Word and enables you to quickly and easily build Web pages.

Another available tool for use in Web page design with VBScript is Microsoft's ActiveX Control Pad. This tool is an HTML text editor that allows you to insert HTML and ActiveX controls automatically into your HTML documents with the help of a wizard. Figure 2.6 shows the ActiveX Control Pad in action.

Figure 2.6 : Microsoft's ActiveX Control Pad.

The Control Pad has a great deal of additional features that aid in editing Web pages and incorporating ActiveX contols, VBScript, and JavaScript into them. You will learn more about ActiveX controls in general starting on Day 10.

Another useful capability from Microsoft is the Layout Control forms interface. This, together with Internet Explorer's incorporation of 2-D layout for HTML, makes it possible to position elements and controls anywhere on a page in a form-like fashion much like in Visual Basic. The layout takes place in a separate file that the layout control incorporates into your main page when that page is loaded. This approach is discussed in more detail on Day 18, "Advanced User Interface and Browser Object Techniques."

Security and VBScript

One of the many questions people have when learning about VBScript is, "How secure is it?" VBScript was designed as a subset of the Visual Basic language. When you look at the language and compare it to VBScript, you essentially see a stripped-down version of Visual Basic. The designers took any part of Visual Basic that could cause VBScript to be unsafe and unsecure and eliminated it. The end result of their work is a language that is safe and "lighter" than its parent, Visual Basic.

When people think about security and the Internet, their concerns are often valid. The Internet has just recently become popular to the masses, and companies are starting to think of ways to make money on the Internet. This brings to mind images of users entering credit card numbers, making banking and shopping transactions, and paying for other services. Obviously, before such activities can take place, the Internet must be secure. Otherwise, people simply won't want to take the chance of their credit card numbers being intercepted or some secure password being used to access their accounts. The World Wide Web and Internet consortia are working very hard to establish security mechanisms for the Internet. Rest assured; this will be an area of intense interest in the short term until security issues can be firmly resolved.

Those who use VBScript are also likely to be concerned about safety. They want to make sure that VBScript does not open the door for a devious Web page to do damage to their computer systems in any way. The most common type of computer damage most users fear is viruses that are transmitted to computers when files are downloaded from an Internet server and modified on the user's computer. Other possible damage includes a Web page that, for some reason, causes the loss of data on the client's computer system or otherwise causes the computer to crash. Nothing is more frustrating to the user than to have five applications open and lose all the data because of a computer crash. Although multitasking systems such as Windows 95, Windows NT, and UNIX make that less likely to occur than the Windows operating system did, it is still an unfortunate possibility in some cases.

VBScript prevents these and other potential security and safety problems by eliminating the cause of such problems entirely. First of all, it is not possible to read and write files or databases in the normal fashion in VBScript. This might seem like quite a limitation, and it is indeed limiting, but this stops up a very large security leak. Damage could come to a user's computer through a Web page that opens and modifies a file or perhaps deletes a file on the user's computer. As a result, it is not possible for VBScript to modify files on the user's computer using conventional methods.

The second area of safety is making sure VBScript will not cause the computer to crash. If there is an important exchange of information happening in a Web page, the user would certainly not want the computer to crash due to an ill-formed script. It's a safe assumption that the designers of VBScript made this less likely by avoiding any code that could potentially cause a crash. It's impossible to state outright that a VBScript program cannot crash because absolutes in the world of computers are rare. It certainly is possible, however, to say that VBScript is very safe and robust and not likely to crash in and of itself.

The design of VBScript properly reflects the goal to prevent security leaks and ensure safety with VBScript, but the story does not end there. VBScript works together with ActiveX controls, intrinsic HTML controls, OCXs, and OLE objects within a Web page. VBScript has no control over what goes on when the code that composes such controls executes. All VBScript "sees" when working with a control or object is the interface that control provides. It is conceivable that a control could, for example, modify a file. VBScript doesn't modify the file-the control does. Although VBScript can't be faulted for causing damage to a user's system, it cannot be responsible for any controls or OLE objects it works with. This is true of security issues as well as stability.

If a control were written with some sort of a glitch or bug inside of it, it could cause a computer to crash or, at the very least, cause the Web page to not function properly. Again, the problem does not lie in VBScript itself but in the control it interfaces with. A Web page is only as stable as its least stable component. If a buggy control is included on a Web page, the potential of that Web page to become unstable is equal to the stability of that control. Therefore, it is very important to choose controls that will work as bug free as possible with any browser or any platform the Web page is run on.

Microsoft's Internet Explorer 3.0 is a good example of the current state of browser security. Internet Explorer contains a feature called Safe Content that makes sure that no unknown programs or components can be downloaded to the user's computer by a Web page without the user's consent. See Figure 2.7 for Internet Explorer 3.0 beta's security dialog for programs you receive within Web pages from the Internet.

Figure 2.7 : The Microsoft Internet Explorer 3.0 (beta) Program Security dialog.

As you can see, you have the option of choosing Expert, which warns you about any security problems before a Web page is displayed; Normal, which automatically prevents security problems by not displaying content that causes security violations; and None, which displays all contents without worrying about security violations. With the current beta version of Internet Explorer 3.0, you should use either Expert or None when working with VBScript Web pages that contain ActiveX controls. This Expert setting will inform you whenever an object is required by the Web page or if a scripting language changes the properties of an existing control on a Web page. These safeguards are an example of part of an evolving process of making Web browsers more secure in an environment of rapidly growing objects, controls, and scripting languages.

You may wish to set this setting to None while using the samples in this book on the
CD-ROM. These samples only make use of Microsoft ActiveX controls. Since your browser can run these locally from the CD-ROM, you don't even have to be on the Internet to progress through the samples and can be sure that you are working in a safe computing environment. Once you go out on the real Internet, any page can cause a control to be downloaded, and all bets are off! It is recommended that you use Expert when visiting pages you are not well acquainted with. Microsoft has provided a framework for certifying and identifying trusted controls so you can only choose to work with known commodity controls. This is discussed more fully on Day 21.

Industry approaches are continuing to rapidly develop in the arena of Web security, particularly in the area of component distribution and security. These issues will be addressed later in considerably more detail on Day 21. For now, it is important to understand that security considerations have shaped the capabilities inherent in the VBScript language.

VBScript Versus Visual Basic and Visual Basic for Applications

Many programmers who are interested in VBScript have used either Visual Basic or Visual Basic for Applications. If you are included in that group, you most likely know that VBScript is a subset of Visual Basic. You've probably also heard from Microsoft that if you already know Visual Basic or Visual Basic for Applications, you already know VBScript. Yes, well-sort of. You know what you could do, but you might not be sure what you cannot do.

One of the first striking differences between VBScript and Visual Basic is that Visual Basic has a design-time environment. When you run Visual Basic, you get an attractive editing environment where you can craft forms and write code using an interactive shell. When you work with VBScript, on the other hand, you have no such environment. VBScript code "lives" within an HTML document, which is a plain text file. At the time of this printing, neither Microsoft nor any other large commercial vendor has a design environment that lets users create VBScript code like Visual Basic does. This is due, in part, to the way in which VBScript works. Visual Basic code creates Windows applications that operate in and of themselves. On the other hand, VBScript code works inside of HTML documents and runs along with HTML. Even though VBScript is an interpreted language as is its parent, you must create VBScript code manually. Some tools are already emerging to overcome this limitation. Microsoft's ActiveX Control Pad, for example, helps overcome the tedious work of inserting controls and editing HTML code by providing a more sophisticated editing tool than Notepad. This tool is currently available for free and can be located by doing a search at www.microsoft.com/intdev. The tool provides for automatic insertion of control object definitions, and it provides a layout editor. The layout editor lets you lay out pages interactively, much like Visual Basic 4.0, and stores the results in a separate file for the layout control to integrate on your page. The ActiveX Control Pad even provides a Script wizard that lets you define scripts through an interactive high-level interface rather than by entering source code statements. Since the focus in this book is on helping you understand all the details of VBScript, however, we steer clear of the higher-level tools, which can shield you from some of the underlying details. We want you to learn those details! Therefore, the samples here are presented at the source code level and can be entered with a text editor such as Notepad. After you gain a well-grounded knowledge of VBScript, you might choose to progress to some of the higher-level tools.

The other primary difference between VBScript and Visual Basic aside from development environments is the language itself. As mentioned previously, many of the commands, keywords, and data types that are supported in Visual Basic are not supported in VBScript. Rather than provide a list of specifics in this lesson, we summarize all the differences between VBScript and Visual Basic in Appendix A, "VBScript Syntax Quick Reference." You'll notice quite a number of differences. If you've used Visual Basic in the past and you think you can use exactly the same code in VBScript, you will probably be surprised. It's important to understand how the languages differ. On Day 20, "Porting Between Visual Basic and VBScript," you will see how to properly port code from Visual Basic over to VBScript. Knowing what will and will not port is essential to that process. Day 20 will give you the information you need to make the transition as painless as possible.

Summary

Today you have been given a taste of what VBScript can do for you. The lesson begins by telling you a bit more about VBScript and what it does. You have learned that VBScript enables you to accomplish tasks otherwise impossible in HTML, and you can often do them with a small, simple amount of code. You have also been introduced to many of the technologies that surround the World Wide Web and VBScript, including CGI, ActiveX controls, intrinsic HTML form controls, OLE objects, and so on. All these accompanying technologies help make your Web pages even more powerful and useful in ways never before possible. VBScript acts as a "glue" that helps you integrate all these components together. VBScript keeps the components together and working with each other, much like cartilage helps keep our bones moving freely and cooperatively.

You have seen several examples of VBScript in action. The purpose of these examples is to show you some of the things VBScript can do. The examples are by no means advanced nor even very sophisticated, but they do help convey some of the exciting and powerful possibilities at your fingertips. The examples today are centered around getting information from the user, processing that information, and then displaying the information back to the user. As you progress through this book, you will learn how to manage this flow of information and make your Web pages very useful to your users. You want them to have a productive, fun, and exciting time working with your Web pages, and you will learn the tools and techniques to accomplish that.

After providing a taste of what VBScript can do, today's lesson discusses some of the questions people have about this new language. You have learned what tools and programs you need to get started with VBScript. You must have a browser that includes VBScript run-time support, such as Microsoft's Internet Explorer 3.0, to use VBScript pages. Any text editor will suffice to create or modify pages.

The lesson then discusses the safety and security of VBScript. There has been a lot of discussion recently about Internet security in the industry. VBScript is a very safe language because it prevents a Web page from accessing any of the data on your computer. Furthermore, the language itself is very safe. It is not easy to cause a VBScript program to crash or otherwise interrupt the normal flow of a Web page or its browser. Security is discussed in detail on Day 21.

Finally, today's lesson provides information for programmers who have used VBScript's parent product, Visual Basic. The lesson discusses the similarities and differences between the languages and what the Visual Basic or Visual Basic for Applications programmer should expect when working with VBScript. This subject is also discussed in detail on Day 20. As the lesson points out, those who have programmed in Visual Basic or Visual Basic for Applications will realize that VBScript is a subset of Visual Basic and Visual Basic for Applications. In other words, many of the commands in Visual Basic and Visual Basic for Applications do not exist in VBScript. This makes VBScript more stable, safe, and efficient. Stripped of the weight of its parent, VBScript can perform very efficiently and reliably in the browser.

Today's lesson gives you an overview of VBScript, its capabilities, what you need to know to learn it and use it, how safe and secure it is, and how it differs from its parent. Tomorrow, you will learn more about where VBScript is supported and, in particular, how it is supported. You will learn how VBScript is "connected" to a browser, and you will also write your first VBScript program.

Q&A

Q
You mentioned that VBScript acts like "glue" to bind components together on a Web page. What do you mean by the term "glue"?
A
The term "glue" is borrowed from VBScript's parent, Visual Basic. Controls and objects placed on a Web page can be linked together using VBScript. For example, you can use VBScript to respond and execute code when a user clicks a command button that places text in a text box control. In this way, you can create a "fabric" that makes all the components work together the way you intend. This applies not only to simple intrinsic controls, but also to more sophisticated objects such as Java applets, ActiveX controls, and other objects.
Q
When a user is working with a Web page, what's so important about having the Web page execute code on the user's computer? Why can't all the code execute on the server?
A
The most significant benefit of being able to execute code on the user's computer rather than the server is that first, it eliminates all the extra Internet traffic required between the client and the server; second, it relieves the server from the burden of performing the processing that the client could do itself. To the user, this means the Web page will be more peppy and responsive. To Internet users as a whole, this means less traffic and better response time. To the owners of the Internet servers, it means less work they have to handle on their servers and the more people they can serve at once who access those servers.
Q
I've never done any programming at all. I just want to be able to write Web pages without going through a lot of hassle and learning time. Will it take me a long time to learn VBScript?
A
If ever there was an easy language to learn, VBScript is it. Unlike more complicated scripting languages such as JavaScript, VBScript is derived from one of the easiest-to-learn languages used for programming today. One could argue that the simplicity of VBScript comes at the expense of power and functionality. Power and simplicity have always been competing goals. VBScript gives the programmer a reasonable trade-off, however, because it is sufficiently powerful for sophisticated Web page applications, yet it is safe enough and easy enough for the beginner to learn quickly.
Q
If VBScript is secure, but other components within a Web page might not be, how can I be sure a Web page won't damage my system when I load it into my browser and work with it?
A
Security is a concern that is foremost on the minds of Internet developers and solutions providers. The bottom line is that market and corporate pressures will force control vendors and solution providers to make sure their controls do not contain security threats. Industry movements underway to clearly establish the identity of components (discussed further on Day 21) will further these efforts. At the very least, any possibility of such threats should be fully documented to the programmer and the users of the Web page before they have a chance of doing any potential damage.

Workshop

Take each of the examples presented today, and knowing what you know now about what VBScript can do, make a list of additional ideas and suggestions for these pages. Are there other Web pages you can create that will help make these examples more useful? Is there anything you can add to the Web pages to make them more powerful? When you've finished reading this book, take another look at these examples and ask yourself these questions again.

Quiz

Note
Refer to Appendix C, "Answers to Quiz Questions," for the answers to these questions.

  1. What major capability does a scripting language give to a Web page?
  2. Name a few of the surrounding technologies that can be "glued" together using VBScript.
  3. What aspects of VBScript make it safe so that a Web page using VBScript cannot destroy or corrupt information on a user's computer?
  4. Where did the VBScript language originate?
  5. What do you need to write VBScript code or use Web pages that contain VBScript code?