VBScript Programmer's Reference

VBScript Programmer's Reference

by Susanne Clark, Antonio De Donatis, Adrian Kingsley-Hughes, Kathie Kingsley-Hughes
     
 

VBScript is one of Microsoft’s scripting languages, which can be employed in a variety of ways — from client-side scripting in Internet Explorer to server-side programming in ASP and the new Microsoft® Windows® Script Host. The language itself has been gradually increasing in power and flexibility, and the newest release, VBScript 5.0 (which comes… See more details below

Overview

VBScript is one of Microsoft’s scripting languages, which can be employed in a variety of ways — from client-side scripting in Internet Explorer to server-side programming in ASP and the new Microsoft® Windows® Script Host. The language itself has been gradually increasing in power and flexibility, and the newest release, VBScript 5.0 (which comes with IE5.0), represents a huge increase in functionality and effectiveness.

Who is this book for?

This book will be useful for anyone who wishes to get to grips with VBScript. Whether you've just played around with HTML and want to find out about the world of programming, or whether you're an experienced programmer who needs to learn the VBScript language in order to work with the Windows Script Host or develop ASP pages, this book will show you the way. No prior knowledge of programming is assumed.

What does this book cover?

  • Complete guide to the VBScript language and its syntax
  • Up-to-date details of the most recent scripting engines for Internet Explorer®, ASP and the Windows Script Host
  • All the new features in VBScript 5, including constructing classes, DCOM support, and using regular expressions
  • The use of VBScript in context with various technologies, including WSH, ASP, ADO, DHTML and VB
  • Extensive reference covering the various implementations and the relevant object models

As an experienced developer, you need to get the facts on a new technology fast. Without the marketing hype, without the trivial introduction. That’s what Wrox Programmer’s References deliver. Hard facts on the newest technologies with practical examples of how to apply new tools to your development projects today.

Read More

Product Details

ISBN-13:
9780764543678
Publisher:
Wiley
Publication date:
10/01/1999
Series:
Programmer's Reference Series
Pages:
840
Product dimensions:
6.05(w) x 9.04(h) x 1.77(d)

Related Subjects

Meet the Author

Susanne Clark has worked with the latest internet technologies, including DHTML, XML, XSL, IE5 programming and scripting, and has contributed chapters on these subjects to the recently published Professional Visual InterDev 6 Programming from Wrox Press. She is currently working as a user interfaces developer in Seattle, USA. When not dealing with computers, she enjoys spending time with her cats, and reading mystery books. Susanne would like to thank Luca for all of his patience and help. Ti amo.

Antonio De Donatis (adedonatis@yahoo.com) began programming in 1984, using a mixture of Basic and assembler on a Commodore 64. Since then, he has designed software the object-oriented way, employing more programming languages and technologies than he can count. He finds programming to be one of the best activities to exercise creativity and to be intellectually active. He has worked for many of the major firms in IT either as employee or as freelance. His current interests include all the technologies involved in the design and implementation of e-commerce solutions. Antonio holds a BS degree in Computer Science from Pisa University (Italy) and he is a Microsoft Certified Professional.

Adrian Kingsley-Hughes is Technical Director of Kingsley-Hughes Development Ltd, a UK-based training and development firm, where he is a consultant in Internet Development and Windows platform programming. He has co-authored 8 books for Active Path and Wrox Press. His abiding passion is the Search for Extra Terrestrial Intelligence and he is the Welsh Regional Coordinator for SETILeague Inc. In his spare time he writes horror novels, plays the didgeridoo and is currently building a radio telescope in his back yard.

Kathie Kingsley-Hughes In addition to writing web development books for Active Path and Wrox Press, Kathie Kingsley-Hughes is the Managing Director of UK-based Kingsley-Hughes Development Ltd, where she specializes primarily in the development and delivery of training courses in web development skills. She began teaching in 1985 and has lectured at several UK colleges and other training establishments and she is currently teaching at ZDU. Any spare time is taken up with astronomy, photography and hiking in the mountains.

Brian Matsik is the President and Senior Consultant at OOCS in Charlotte, NC. He is MCSD certified and has been working with Visual Basic, VBScript, and VBA for over six years. He currently specializes in ASP, SQL Server, and VB COM. Brian can be reached at brianmat@oocs.com.

Erick Nelson is a 18 year old Internet developer and programmer, and creator of the award winning website cues.com. about which he spoke at both the 1998 and 1999 Professional ASP Developer's Conference. Erick began his programming career at age 16, and now has experience using ASP, VBScript and Visual Basic to design sites. Erick enjoys playing computer games and working on his online RPG system. His outdoor activities include mountain biking, swimming, cliff jumping, playing tennis and snow skiing. He can be reached at erick@ericknelson.com.

Piotr Prussak works for ACEN in Buffalo, NY as a developer (although he is originally from Poland). He creates ASP, VB and Java Applications for the company and its clients. He also spends some time working with the SQL server and ColdFusion, probably more than he should. When he's not programming and mentoring, he either writes stuff, or edits stuff written by others. Sometimes he sleeps, eats and relaxes like any other human being. In the past life he used to be a photographer, but that was just too relaxing. Piotr (or Peter, as some might call him) is married to lovely Joanne, who is an MIS student at a local University. Sometimes they travel all over the world and are disconnected from the world of computers and 24 hour news.

Daniel Read is Senior Developer at Compass, Inc. in Atlanta, GA, where he and fellow team members build IIS/ASP/VB/MTS e-commerce web sites, as well as client/server and distributed systems. Daniel sometimes longs for his simpler, statically linked days as an X-Base developer and DOS command line junkie, but today enjoys the power afforded by the arsenal of Microsoft's Windows DNA tools and technologies. His other interests include camping, crime fiction, listening to music of all sorts, and going to as many concerts as he can. He's not sure if co-authoring a programming book will help him meet the woman of his dreams, but it sure would be nice.

Carsten Thomsen is a Microsoft Certified Systems Engineer, who started programming in Visual Basic back in 1993, when it was version 3.0. Presently he is pursuing Microsoft Certified Systems Developer certification and was recently awarded the MVP title for his contributions to the Microsoft Visual Basic newgroups. For the last 4 years he has been developing 2-tier and 3-tier client/server solutions based on MS SQL Server, MTS and MSMQ, but has more recently been moving towards browser-based development, based on MS IIS, using ASP/VBScript, COM/ActiveX components and Visual Basic WebClasses. In whatever spare time he has, he enjoys traveling and spending time with his two daughters, Nicole and Caroline, and his girlfriend Mia. He works out at a local gym and (at spectator-level) enjoys ice hockey and soccer. He is generally considered a computer freak, and spends too much time at the computer.

Paul Wilton After an initial start as a Visual Basic applications programmer, Paul found himself pulled into the net and has spent the last 18 months helping create internet and intranet solutions. Currently he is developing web-based systems, primarily using Visual Basic 6 and SQL Server 7, along with numerous other technologies.

Read More

Read an Excerpt


Chapter 1: A (Very) Short Introduction to Programming

In trying to squeeze the basics of writing computer programs into one chapter, we may be attempting the impossible, but we're going to do our best. The reason for including this chapter is that many people come to a scripting language, like VBScript, as their first language. Perhaps you're a network systems expert who wants to use VBScript and the Windows Script Host to write login scripts and automate administration tasks. Or perhaps you're a web designer who feels the need to branch out from the visual aspects of creating web pages and into writing scripts to drive content. Or perhaps you , re just a person who wants to learn a programming language for the fun of it. Either way, you've come to the right place.

Programming - or "writing code," as some people like to call it - is a huge subject. Many volumes have been written about it. During this chapter, in a single paragraph, we might introduce multiple unfamiliar concepts. We're going to move pretty fast, but if you read along closely and try out the examples, you'll probably be just fine.

Keep in mind that even an in-depth discussion of how to write computer programs might not even begin to touch on subjects such as architecture, systems design, database design, testing, documentation, and all the rest of the subjects that an accomplished software developer must master. But don't let all that discourage you. Everyone starts somewhere, and this is a great place for you to start learning the art and science of making a computer sing. Consider this chapter a brief introduction to the important building blocks. It won't make you an expert overnight, but hopefully it will give you a the know-how you'll need in order to get the most out of the rest of the book.

Variables and Data Types

In this section, we're going to be moving quickly through some of the most basic concepts of programming: variables, comments, using built-in VBScript functions, a other syntax issues.

The first concept we're going to touch on is that of variables. Simply put, a variable is a place in memory where your script holds a piece (or a set) of information (we'll use term data in place of "information" throughout most of this discussion). The data stored in a variable might be very simple, like the number 10,000, or very complex, such as a whole series of numbers, ranging from 1 to 10,000.

Behind the scenes, a variable is a reserved section of the computer's memory. just to make sure we're dear, memory is temporary working space for the computer. Memo is transient -that is, things that are placed in memory are not stored there permanently. That's what the hard drive is for. Since memory is transient, and since variables are stored in the computer's memory, variables are by extension transient a well. Your script will use variables to temporarily store a piece of information that the script needs to keep track of. (If your script needs to store that information permanently, it would store it in a file on the hard disk, or in a database, which is also stored permanently on the hard drive.)

In order to make it easier for the computer to keep track of all the millions of pieces 0 information that might be stored in memory at any given moment, memory is broke up into chunks. Each chunk is exactly the same size, and each chunk is given an address. You don't need to worry about memory addresses, but it's useful to know that a variable is a reserved set of one or more chunks. Different types of variables take up different amounts of memory. In your VBScript program, you will declare (or "dimension") variables before you use them, and you will give them a name in the process. Here's an example of a variable declaration in VBScript:

Dim CustomerName

When you declare a variable like this, you are basically telling the computer "Reserve some memory for me, and please give that memory the name CustomerName." The computer (or, more precisely, the VBScript engine) keeps track of that memory for you and whenever you use the variable name CustomerName, it will know what you're talking about.

Note: it is not strictly required that you declare all of the variables you use. VBScript by default allows you to use undeclared variables. However, we strongly recommend that you declare all of the variables you use in your scripts. We'll cover this topic in more depth in Chapter 2.

Variables are essential to the activity of writing a VBScript program (or any program for that matter). Without variables, you'd have no way of keeping track of all of the pieces of information your script is going to be manipulating, adding up, and displaying on the screen. Picture yourself at your desk keeping track of your household income and expenses in a paper-based ledger.

This process entails adding up and keeping track of multiple pieces of information: paychecks and other kinds of income, grocery expenses, automobile expenses, medical expenses, debt service, and so on. You'd keep each of these running totals in a separate column, and later you'd probably add them all up to create a view into your financial situation. If you were writing a computer program to keep up with all this information instead of using your paper-based ledger, you would probably store the permanent data in a database, but while your program was accepting input of the numbers and keeping track of the totals, it would use different variables to keep up with each different piece of information.

In VBScript, whenever you have a piece of information that you need to work with, you would declare a variable using the syntax we demonstrated a moment ago. At some point in your script, you're going to need to place a value in that variable otherwise, what would be the point of declaring it? Placing a value in a variable for the first time is called initializing the variable. Sometimes you initialize a variable with a default value. Other times, you might ask the user for some information, and initialize the variable with whatever the user types in. Or you might open a database and use a previously-stored value to initialize the variable.

Initializing the variable gives you a starting point. After its been initialized, you can use the variable in calculations, store it in a database, or display it back to the user in other form. Here's a simple VBScript example:

Dim DateToday

'Initialize the variable
DateToday = Date
MsgBox 'Today's date is DateToday

Now we've opened up a bit of a can of worms. What's all I that other stuff in this code? We'll look at it line-by-line. The first line is the variable declaration. We've asked the computer to reserve some memory for us, and to please remember the variable name DateToday for us.

All of the examples in this chapter are tailored so that they can be run by the Windows Script Host. The Windows Script Host is a scripting host that allows you to run VBScript programs within Windows. WSH will allow you to try these example programs out for yourself. You may already have WSH installed. To find out, type the above example script into a text editor, save the file as TEST. VBS, and double click the file in Windows Explorer. If the script runs, then you're all set. If Windows does not recognize the file, then you'll need to download and install WSH from http://msdn.microsoft.com/ scripting.

If you like, you car, skip ahead briefly and check out the beginning sections of Chapter 10. You don't need to read the whole chapter, just the first sections, which describe how to install the Windows Script Host, and how to use WSH to run scripts.

Let's get back to the code extract shown above. The second line is a comment In VBScript, any text that follows the single quote character ( ' ) is treated as a comment. This means that the VBScript engine will ignore this text. This introduces an interesting point: if the script engine is going to ignore this text, why type it in at all? It doesn't contribute to the execution of the script, right? This is correct, but it excludes one of most important principles of programming: it is equally important to write a script with human readers in mind as it is to write with the computer in mind.

Of course, when we are writing a script, we must write it with the computer (or, more specifically, the script engine) in mind, because if we don't type it in correctly (that is we don't use the correct syntax), the script engine won't execute the script. However, programming is an inherently human-involved activity. Once you've written some useful scripts, you're probably going to have to go back to make changes to a script you wrote six months ago. If you did not write that code with human readers in mini it might be difficult to figure out what in the world you were thinking at the time you wrote the script. Worse yet, what happens when one of your co-workers has to go in and make changes to a script you wrote six months ago? If you did not write that script to be readable and maintainable, your co-worker will probably curse you as they try to decipher every line of your code.

Adding good comments to your code is only one aspect of writing readable, maintainable programs. We'll touch on some other principles later, such as choosing good variable names, indenting properly, using white space in a helpful way, and organizing your code clearly and logically. That said, keep in mind that adding too many comments, or adding comments that are not useful, can make a script almost a bad as one with no comments at all, Also, if you are scripting for a web page that mu be downloaded to a user's browser, too many comments can affect the time that it takes to download the page.

We'll discuss some good commenting principles later in this chapter, but suffice it to say now that the comment in line two of our script is not really a good comment for everyday use. This is because, to any semi-experienced programmer, it is painfully obvious that what we are doing is initializing the DateToday variable. Throughout this book, you will often see the code commented this way. This is because the point our code is to instruct you, the reader, in how a particular aspect of VBScript programming works.

Back to our example script. It should now be clear that what we're doing in line three initializing the variable DateToday. To do this, we are using a built-in VBScript function called Date. A function is a piece of code that returns a value. VBScript comes with a lot of built-in functions. These functions are part of the language, and they're always available for your use. You can find a full listing of all of VBScript's built-in functions in Appendix A. The Date function is one such function that returns the current date according to the computer's internal clock. (In a few minutes, we'll get into the idea of writing your own functions.) In line three, we are telling the script engine, "Take the value returned by the Date function, and store it in the variable called DateToday." (Notice that we did not name the variable Date, but rather DateToday. This is necessary because, since Date is a built-in VBScript function, "date" is a reserved word. You cannot name your variables with the same name as a reserved word.)...

Read More

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >