Beginning VBScript Techniques

Tips for the scripting newbie

VBScript, one of the most powerful and underused features in the Windows environment, provides the same powerful programming tools (e.g., variable support, structured program control, the ability to leverage COM objects) that you'll find in full-featured development languages. In this Top 10, I introduce you to some basic but essential scripting techniques.

10. Comments—Liberal use of comments lets you quickly decipher a script's purpose months after you've forgotten about it. The apostrophe (') is the VBScript comment indicator:

'vbsample.vbs ­ Sample Script for Windows 2000 Magazine

9. Option Explicit—You should add the Option Explicit statement to the beginning of a script to force yourself to explicitly declare all variables in the script. Variables are named storage locations that contain values the script can modify. By default, scripts automatically create a variable on the first reference to it. However, this capability can mask minor typos in the script. Using Option Explicit can save you hours of debugging.

8. Dim—Dim statements let you create variables. Variable names must begin with an alphabetic character, can't contain dots, and must contain fewer than 256 characters. Many languages require you to specify the data type of each variable. However, VBScript supports only the variant data type (which can contain all types of data), so you don't need to declare a specific data type for VBScript variables:

Dim myVariable

7. Assignment—After you create a new variable, you can use the Assignment operator (=) to assign a value to it. The following line assigns the value of 3 to myVariable:

myVariable = 3

6. Math—VBScript provides support for the full range of mathematical expressions. The following line shows how to perform simple math calculations and divide the result by the contents of myVariable:

myVariable = ((5 + 1) * 2)/myVariable

5. Concatenation—A common VBScript technique is to combine the contents of different strings. You can use the Concatenation operator (&) to create a new string:

myVariable = "The value of myVariable is:" & myVariable

4. Constants—Constants, like variables, are named values that you can use in your scripts. However, you can alter the contents of variables, but you can't alter the value of a constant:

const myTitle = "VBSample"

3. MsgBox—You can use the MsgBox function to display messages to the user. The following example shows how to display the message contained in myVariable. The message box's title bar displays the value in myTitle:

MsgBox myVariable,,myTitle

2. InputBox—You use the InputBox function to prompt the user for simple input values. The function automatically assigns the value that the user enters to the variable on the left of the equal sign. Note that the InputBox function encloses its argument in parentheses:

myVariable = 
InputBox("Input 1")

1. If...Then...Else—The If statement is an important logical-flow control function. Present in virtually all scripts, the If statement evaluates a condition, then performs an action based on whether the condition is met:

If myVariable = 1 Then
	MsgBox "The value was 1"
MsgBox "The value was not 1"
End if
Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.