VBScript Programmer's Reference

VBScript Programmer's Reference

3.3 3
by Daniel Read, Adrian Kingsley-Hughes, Kathie Kingsley-Hughes, Antonio De Donatis
This book will not only serve as a reference guide to the VBScript language and syntax, but will also demonstrate its usage in context with many practical examples. It is ideal for anyone who uses VBScript as a programming language. It is a desktop reference and quick guide for ASP programmers, IE programmers and Windows Script Host programmers.


This book will not only serve as a reference guide to the VBScript language and syntax, but will also demonstrate its usage in context with many practical examples. It is ideal for anyone who uses VBScript as a programming language. It is a desktop reference and quick guide for ASP programmers, IE programmers and Windows Script Host programmers.

Product Details

Wrox Press, Inc.
Publication date:
Programmer's Reference Ser.
Product dimensions:
6.10(w) x 8.98(h) x 1.74(d)

Related Subjects

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.)...

Meet the Author

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.

Piotr Prussak works for ACEN in Buffalo, NY as a developer (although he is originally from Poland). He creates ASP, VBandjava 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 a 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 lovelyJoanne, 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 Nws.

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.

Brian would like to thank Tracy for her support, Mike Dunner for keeping him on his toes when it comes to ADO and MTS, and Mark Harris for taking care of the books while he stays glued to the PC.

After an initial start as a Visual Basic applications programmer, Paul Wilton 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.

Paul would like to wish lots of love to his fianc6e Catherine, who ensures his sanity chip remains plugged in.

Antonio De Donatis (adedona t is @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 ecommerce solutions. Antonio holds a BS degree in Computer Science from Pisa University (Italy) and he is a Microsoft Certified Professional.

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.

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >

VBScript Programmer's Reference 3.3 out of 5 based on 0 ratings. 3 reviews.
Guest More than 1 year ago
The author never does address what skill level is necessary to fully appreciate this book. From my reading it should be clarified at the beginning that this book is not for beginners. Being new to ASP I found this book very frustrating to follow. It wasn't until page 51 did the author decide to inform the reader exactly how to 'run' scripts in Windows. From cover to cover I felt the book was organized poorly. However,if I was a more experienced programmer I think that I would better understand the book's layout and be able to 'fish' through this convoluted book.
Guest More than 1 year ago
I'd buy it again, BUT it is frustrating in many ways. It is poorly edited and a beginner/intermediate will have trouble finding needed information. One of many examples I have as I tried to find info on calling procedures and functions is this: a line on page 237 (ch. 8) states 'If you know how to write Function and Sub procedures (see Chapter 3), then you know how to write methods for a class.' CHAPTER THREE BARELY MENTIONS SUB OR FUNCTION, and DOES NOT DEMONSTRATE how either is called from your script.
Guest More than 1 year ago
This book opens a whole new world for developers new to this language. It allows executing batch files in windows, managing sites through ASP on the server side, and making visual affects on the client side. Any Internet developer should master this language and this book covers it well. It is a shame book does not come with cd rom that includes sample code