Teach Yourself

    VBScript  in 21 Days



by Keith Brophy, Timothy Koets


C  O  N  T  E  N  T  S



Introduction


WEEK 1  At a Glance

Day 1  Introducing VBScript and the World Wide Web

Day 2  The Essence of VBScript

Day 3  Extending the Power of Web Pages with VBScript

Day 4  Creating Variables in VBScript

Day 5  Putting Operators to Work in VBScript

Day 6  Controlling the Flow of VBScript Code

Day 7  Building a Home for Your Code

WEEK 1  In Review

WEEK 2  At a Glance

Day 8  Intrinsic HTML Form Controls

Day 9  More Intrinsic HTML Form Controls

Day 10  An Introduction to Objects and ActiveX Controls

Day 11  More ActiveX Controls

Day 12  Advanced Objects: ActiveX, Java, and ActiveVRML

Day 13  VBScript Standards and Conventions

Day 14  Working with Documents and User Interface Functions

WEEK 2  In Review

WEEK 3  At a Glance

Day 15  Extending Your Web Pages with Strings

Day 16  Extending Your Web Pages with Dates and Advanced Math

Day 17  Exterminating Bugs from Your Script

Day 18  Advanced User Interface and Browser Object Techniques

Day 19  Dynamic Page Flow and Server Submittal

Day 20  Porting Between Visual Basic and VBScript

Day 21  Security, Stability, and Distributed Source Control Issues

WEEK 3  In Review

Appendix A  VBScript Syntax Quick Reference

Appendix B  Information Resources

Appendix C  Answers to Quiz Questions



Credits



From Keith to Sue, and from Tim to Michelle.


Copyright © 1996 by Sams.net Publishing

FIRST EDITION

All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. For information, address Sams.net Publishing, 201 W. 103rd St., Indianapolis, IN 46290.
International Standard Book Number: 1-57521-120-3

HTML conversion by :
    M/s. LeafWriters (India) Pvt. Ltd.
    Website : http://leaf.stpn.soft.net
    e-mail : leafwriters@leaf.stpn.soft.net

Interpretation of the printing code: The rightmost double-digit number is the year of the book's printing; the rightmost single-digit, the number of the book's printing. For example, a printing code of 96-1 shows that the first printing of the book occurred in 1996.

Trademarks

All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams.net Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark. Microsoft and Visual Basic are registered trademarks of Microsoft Corporation.

President, Sams PublishingRichard K. Swadley
Acquisitions ManagerGreg S. Wiegand
Managing EditorCindy Morrow
Marketing ManagerJohn Pierce
Assistant Marketing ManagerKristina Perry


Acquisitions EditorChristopher Denny Development EditorAnthony Amico
Software Development Specialist Steve StraigerProduction Editors Kitty Wilson Kris Simmons
Copy EditorsKate Shoup, Colleen A. Williams Technical ReviewerGreg Guntle
Editorial CoordinatorBill Whitmer Technical Edit CoordinatorLynette Quinn
Editorial AssistantsCarol Ackerman, Andi Richter, Rhonda Tinch-Mize Cover DesignerTim Amrhein
Book DesignerGary Adair Copy WriterPeter Fuller
Production Team SupervisorBrad Chinn
ProductionMary Ann Abramson,Georgiana Briggs, Cheryl Dietsch, Michael Dietsch, Chris Livengood, Paula Lowell, Mike Henry, Dana Rhodes, Andy Stone


Foreword

A popular saying in the Internet development arena nowadays states that if a technology is more than three months old, it's obsolete! This sentiment is perhaps exaggerated for effect, but it does underscore a very significant change the industry is undergoing. New software technologies and breakthroughs are coming at a breakneck, leapfrog pace. Staying current is more of a challenge than ever before. Picking the technologies that will be here to stay, as opposed to pretenders to the throne, has never been tougher.

If you have a copy of this book, though, we think you've picked a winner. VBScript opens up a world of possibilities for the environments you apply it to-Web pages in particular. You can create smart, active, and interactive Web pages with VBScript with unparalleled ease. It is difficult to appreciate the full power and impact of this new scripting language without a good resource to back it up. It's been our aim to get such a resource into your hands as soon as possible. The "under-construction dust" is just settling as this book goes to print, but already it is clear that a capable, stable, mature product has emerged.

Microsoft, like many companies in the opinionated Internet fray, has its detractors. As a large, high-profile company and somewhat of a late arrival onto the Internet scene, its technologies are the focus of great anticipation and in some corners, even skepticism. The skepticism has been unfounded. Microsoft has delivered a scripting language that is easy to use, flexible, powerful, and secure. The most prominent home of the original release of this language is the browser, which is the main focus of this book. However, you can certainly expect it to become integral to many more products as the future unfolds. VBScript is a technology that is here to stay. As the waves of technology change in the exciting months and years ahead, it is a safe bet that VBScript will be riding the tide. To some extent, the course of many other of the vessels traveling the sea might be changed because of VBScript.

Keith and Tim
July 1996

Note
This book is based on a stable beta version of Internet Explorer 3.0 and Visual Basic, Scripting Edition (also called VBScript). We've expended a lot of effort to ensure that it is as accurate as possible. However, if any discrepancies between the material covered here and the official product release of Internet Explorer 3.0 and VBScript come to light, they will be fully documented and available online. Refer to Appendix B, "Information Resources," for information on how to obtain the latest updates, if any, from Macmillan Publishing's site

(http://www.mcp.com/).

Disclaimer from the Publisher

The information contained in this book is subject to the following disclaimers:

  1. The information in this book is based on a beta that is subject to change.
  2. Microsoft does not endorse the content of this book, nor will it provide support for the beta product.
  3. You can visit Microsoft's Web site at http://www.microsoft.com/vbscript for up-to-date information about Visual Basic, Scripting Edition, and http://www.microsoft.com/intdev for related ActiveX information and software.

Acknowledgments

We would like to thank Sams.net for the opportunity to tackle a topic that we view as a very important, exciting step in software evolution. Along with this, we extend a special thanks to Chris Denny for his help and support all along the way; Tony Amico, our key development editor; Kitty Wilson, our ever-patient production editor; Greg Guntle, technical editor; and the rest of the great Sams team behind us. Appreciation is similarly extended to the great gang at CNS, our Internet service provider and the best "voice of experience" we've found on rapidly evolving Internet connectivity issues.

We'd also like to thank our colleagues at X-Rite, Incorporated, where we develop software by day. Although this book is a project independent of the company, we certainly benefit in knowledge and spirit from our daily association with such a fine group of co-workers. Thanks especially to Steve Peterson for encouraging a view toward the leading edge of technology.

Thanks also to our friends and families. Without them, the book couldn't have come about. After spending much time on the last book saying, "It's gonna be a long time before we do this again," something unexpected happened. A fantastic topic coaxed us out of retirement and made liars of us within just a few months, and there we were, doing it again. This book came during busy times in our lives. During the time the book was written, we dealt with sick cockatiels (Tim), rescuing kids locked in the bathroom and taking them to the circus (Keith), wedding preparations (Tim), the Boston Marathon (Keith), busy work schedules (Tim and Keith), and then the wedding itself (Tim and Michelle)! We strove very hard for quality in this book and felt we never sacrificed in this regard. We did sacrifice time with family and friends, who nevertheless were right there behind us every step of the way. So much credit goes to them all.

Much thanks and love from Keith to Sue and from Tim to Michelle. This book is dedicated to them.

Keith adds: Thanks to my wife, Sue, for assisting with so many aspects of the book preparations in addition to being there every step of the way. Thanks also to Ginger, Mom, and Dad, not only for the support in general, but also for making the kids the world's happiest grandkids. To Ginny, Emma, and Ben, who are still waiting on that pool to go up, to family and friends who are wondering when my hair will stop looking so ruffled, and to special running comrades who are waiting for me to stop complaining that I'm out of shape: Thanks for understanding. Another book has been borne into the world, and we think it's an important one.

Tim adds: Thanks also to my wife, Michelle, for being so understanding and supportive during the busy time of wedding preparations and beginning a new life together. She has been patient and tolerant of those evenings when I had to spend an inordinate amount of time in the den at my computer and when my schedule got a bit hectic. She has been a terrific partner, and I look forward to a wonderful life together with her. Thanks also to our pet cockatiel, Buddy Bird, who not only provided me with some great examples in the book, but was a great companion on those lonely days when Michelle was at work. We also thank our parents for their support all through the years, both past and present.


About the Authors

Keith Brophy

Keith Brophy has many years of experience in the design, development, and testing of software systems. He is currently a software release coordinator for X-Rite, Incorporated, a leading worldwide provider of color and appearance quality control software and instrumentation in Grandville, Michigan. Prior to that, he was a lead software developer for IBM's System Integration and Federal Systems divisions in the Washington, D.C., area and worked on a wide variety of systems. His experience includes building Internet systems in the "pre-Web" era. During this time, he also was responsible for various operating system, performance, and graphical user interface research and development projects. He has taught in various venues, including Northern Virginia Community College and as the advanced Visual Basic adjunct faculty member at Grand Rapids Community College.

Mr. Brophy, along with Mr. Koets, co-authored Visual Basic 4 Performance Tuning and Optimization (Sams 1996) and was a contributing author for Visual Basic 4.0 Unleashed (Sams 1995). He also served as technical editor on Real-World Programming with Visual Basic (Sams 1995) and the revised edition of Teach Yourself Visual Basic 4 in 21 Days (Sams 1995). He has a B.S. in computer science from the University of Michigan in Ann Arbor and an M.S. in information systems from Strayer College in Washington, D.C. Mr. Brophy is the founder of DoubleBlaze Software Consortium (http://www.doubleblaze.com), an ActiveX Internet research and development company involved in endeavors such as research for this book.

Timothy Koets

Timothy Koets is a software engineer at X-Rite, Incorporated, a leading worldwide provider of color and appearance quality control software and instrumentation in Grandville, Michigan. Prior to this, Mr. Koets was a computer systems engineer in the Systems Engineering and Integration division of Martin Marietta in the Washington, D.C., area. In addition to developing Visual Basic applications, Mr. Koets has experience in many other areas including Visual C++, computer networking, client/server application design, parallel processing, and performance analysis. He, too, has previous experience building pre-Web systems that were Internet aware. Mr. Koets is an adjunct faculty member at Grand Rapids Community College, where he teaches advanced Visual Basic, and has prior teaching experience ranging from computer programming and engineering laboratory classes to Lotus Notes training courses.

Mr. Koets, along with Mr. Brophy, co-authored Visual Basic 4 Performance Tuning and Optimization (Sams 1996) and was a contributing author for Visual Basic 4.0 Unleashed (Sams 1995). He has a B.S. in electrical engineering and an M.S. in electrical engineering from Michigan Technological University in Houghton, Michigan. Mr. Koets is the founder of Cockatiel Software, an Internet research and development company that is an affiliate of DoubleBlaze Software Consortium (www.doubleblaze.com).

Tell Us What You Think!

As a reader, you are the most important critic of and commentator on our books. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can help us make strong books that meet your needs and give you the computer guidance you require.

Do you have access to CompuServe or the World Wide Web? Then check out our CompuServe forum by typing GO SAMS at any prompt. If you prefer the World Wide Web, check out our site at http://www.mcp.com.

Note
If you have a technical question about this book, call the technical support line at 800-571-5840, ext. 3668.

As the team leader of the group that created this book, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn't like about this book-as well as what we can do to make our books stronger. Here's the information:

Fax:317-581-4669
E-mail:programming_mgr@sams.mcp.com
Mail:Greg Wiegand
Comments Department
Sams Publishing
201 W. 103rd Street
Indianapolis, IN 46290


Introduction

How important is VBScript to the future of computing? How important is it to you? Does it give you new capabilities that you can't get in other languages, or is it just another choice in a sea laden with confusing buzzwords? If you've pondered questions like these, you're not alone. VBScript is one of the most exciting new players in the rapidly expanding universe of technologies loosely termed the Internet. The purpose of this book is to teach you how to use VBScript. As a brief prerequisite to that journey, consider why VBScript is such an important part of the Web page development arsenal. Perhaps the best way to understand the potential of this future-centered technology is to take a look at how far the Internet has come. You might find that you have been involved in many of the trends leading up to the advent of VBScript without even realizing it, just as the authors have been.

A little over a decade ago, one of the authors was producing Department of Defense software and the reams of documentation that go along with it. One of the requirements in putting together this documentation stipulated that it should be generated in a markup language called SGML. This markup language was quite cumbersome. For example, it required that each heading start with an h1 tag and each paragraph with a p tag. Eventually, this project came to an end, and it seemed that this memory was just a relic of the past. Then, along came the World Wide Web. Behold: The World Wide Web is based on a page description language inherited to a large extent from SGML! The tag-oriented approach for Web pages succeeded precisely because it leaves the work of presenting Web pages to the browser. This approach provides an efficient, low-overhead means for communicating across the Internet because it enables the information sent across the network to be content-centered while the browser takes care of the cosmetic details on its own.

About a decade ago, another important change in the computer industry began. The mainframe-centered computing world, where all work was performed on a central computer, was quickly being replaced by a more distributed model. Users could now do some of the work locally, using the horsepower of their own pcs rather than the mainframe. For example, an accountant could now do much of her work in a spreadsheet on her own pc, rather than vie for time on an overloaded mainframe to perform accounting analyses.

Once users had more computing power in front of them, they could take advantage of that power with a graphical user environment. Windows filled this need, but the next big problem was creating the Windows applications themselves. In the early days when C was the only viable language alternative, Windows program development required such degrees of experience and development time that Windows programming was out of the reach of many. It was also quite difficult to integrate other commercial components into applications. Visual Basic came forward to fill these voids. Because of its ease of use and component integration capability, Visual Basic achieved widespread acceptance over the course of the next few years and releases. Some even claim that this language, which did not exist a decade ago, is now the world's most popular programming language.

That brings us to the very recent past. The Web provides a way to deliver content across the Internet to client computers, often pcs, using the tag-oriented HTML language. A page is sent from some host computer for an end user to look at. The browser on the end user's computer has the job of presenting the information. What if more complex processing is needed in conjunction with a page? Suppose you want to trigger a series of simple financial calculations from a Web page. The HTML language doesn't provide such support. For a while, the only way to do this was to use an approach similar to the old mainframe approach-send requests back to the host computer and make it perform the processing. Then came the advent of JavaScript. With JavaScript, when the browser on a client computer presents the page to the user, it can also act on the embedded JavaScript instructions to perform smart processing. However, it takes experience to develop JavaScript programs, which are somewhat like C++ in nature. If you are a new programmer or one of the million or more programmers whose background is Visual Basic, you'd require a certain amount of effort to get up to speed on the JavaScript language.

Development for this very exciting environment required a language that not all could easily master. Sound like a familiar problem to you? It did to Microsoft, too, and in the process of inventing a comprehensive Internet strategy, Microsoft reinvented, or at least reengineered, the wheel. Visual Basic, Scripting Edition was its answer to these problems. VBScript, a subset of its parent, Visual Basic, is an easy-to-use language that is a cinch to integrate and provides an easy path to incorporate components.

If this brief history of the Internet and the reasons why VBScript came about are new to you, don't worry-you can take comfort in the fact that the currents of change are flowing in the right direction to make your Web page development easier than ever before. As to the "how" of VBScript, we'll tell you everything you need to know about using VBScript in the pages ahead.

Step-by-step, we will teach you how to use Visual Basic, Scripting Edition to its fullest potential. You will also see how to take advantage of powerful intrinsic controls, ActiveX controls, Java applets, and other objects through VBScript. During this journey, we will cut through all the Internet jargon and buzzwords, helping you to clearly understand how all the pieces of the Internet fit together and how you can participate in this new way of sharing information. We won't bore you with all kinds of information on the Internet that you don't need, nor will we give you just a cookbook of techniques. Rather, this book will give you the ability to creatively and expressively use VBScript to write impressive, powerful, useful Web pages.

Who Should Read This Book

This book is for you if you find yourself in one of the following categories:

What This Book Contains

This book is intended to be completed in 21 days-one chapter per day-although the pace is really up to you. We have designed the book as a teacher would teach a course. We start with the basics and continue to introduce more of the language to you as the chapters progress. By the end of the first week, you will be creating your first VBScript Web page. As you work through the chapters, you will continue to design Web pages using VBScript that will become increasingly more powerful. In the second and third weeks, you will be exploring the more advanced features VBScript provides. In addition to instruction, we will furnish an abundance of examples, as well as exercises for you to try. You can learn a great deal by reading, but only when you try building a few VBScript Web pages of your own can you become truly experienced.

With this book, you will become comfortable enough with VBScript to design and implement powerful Web pages of your own and become confident in your abilities to use it effectively-all in three weeks or less!

What You Need Before You Start

Because this is a book about creating Web pages, we assume that you are already able to access and use the Internet. You should have a World Wide Web browser that supports VBScript, such as Microsoft's Internet Explorer 3.0, and you should be somewhat familiar with it. (Steps to obtain such a browser are provided on Day 1.) A basic knowledge of the other Internet services such as electronic mail, FTP, Telnet, Usenet news, and Gopher is not necessary for working through this book, but it may be helpful for your future page development, particularly if you want your Web pages to interact with these services. As is true whenever you learn a new programming language, the more experience you have with programming, the better. Don't worry, though, if programming is new to you; you should be able to write VBScript code with a minimal amount of previous programming experience. Our objective is to begin from the ground up, and we do not assume you are an expert programmer. After all, VBScript is not designed just for experts, but for anyone interested in building active Web pages quickly-you don't necessarily have to be a computer language guru.

You're now ready to begin the first chapter on your journey of learning how to use VBScript. We're excited about showing you the potential of this new, exciting language. The journey will be well worth the effort.