Appendix A

VBScript Syntax Quick Reference


VBScript is composed of a great deal of syntax you need to keep straight. You will probably use many elements of the syntax so often that you will easily remember what they do. However, even an experienced programmer uses some of the syntax elements infrequently. When you come across those elements in your code or strain to remember if you have the syntax exactly right, a good reference can serve as a welcome relief.

Languages such as Visual Basic 4.0 come complete with a very useful context-sensitive help facility. VBScript provides no such inherent support. It is a hosted language that comes along for the ride with the environment that supports it, such as a browser. It is not a separate product with its own development environment and the help that is typical of such an environment. You can turn to other places for help on the Internet, however. At the time of this writing, Microsoft had a very helpful VBScript language reference that could be reached at http://www.microsoft.com/vbscript.

This appendix is intended to supplement the VBScript information available from the Web location. Unlike some of the other language references available, the information in this appendix is presented in an alphabetical fashion, intermixing functions, statements, intrinsic constants, and major properties in the same list. A brief description of each element is provided, but it's not enough to replace the official reference material-just enough to give you a quick, high-level understanding of what that element does.

You'll develop a common way to refer to information as you work your way through a new book or sample programs. You might find a language element that is unfamiliar, but all you need to do to quickly get back on course is see a brief description of what it does and find out if it is a property, function, or other type of construct. This appendix can serve as a quick first resort for such information.

Table A.1. Summary of VBScript syntax.
SymbolType Description
+ (addition) OperatorAdds two numbers together. You can also use this to concatenate strings.
' (comment indicator) KeywordWhat follows this symbol on the line is a comment.
" (double quote) KeywordShows the start and end of a literal string.
& (string concat-enation) OperatorPerforms the concatenation of two string expressions.
/ (division, floating point) OperatorDivides two numbers and returns a floating-point result.
\ (division, integer) OperatorDivides two numbers and returns an integer result.
= (equals) ComparisonChecks whether two expressions are equivalent.
^ (exponent) OperatorRaises a number to the power of an exponent.
> (greater than) ComparisonChecks whether the left-side expression is greater than the right-side expression.
>= (greater than or equal) ComparisonChecks whether the left-side expression isgreater than or equal to the right-side expression.
< (less than) ComparisonChecks whether the left-side expression is less than the right-side expression.
<= (less than or equal) ComparisonChecks whether the left-side expression is less than or equal to the right-side expression.
_ (line continuation character)- KeywordIndicates that the current line continues onto the next line.
* (multiplication) OperatorMultiplies two numbers.
- (subtraction or indicator) OperatorFinds the difference between two numbers; or, when applied to one operand, treats it as a negative value.
: (colon)  Indicates line separation, as in A = A + 1 : B = C + 1.
<> (not equal) ComparisonChecks whether two expressions are not equivalent.
Abs FunctionReturns the absolute value of a number.
And Logical operatorPerforms a logical conjunction on two expressions.
Array FunctionReturns a variant comprising an array.
Asc FunctionProvides the character code corresponding to the first letter in a string as a return code.
AscB FunctionLike Asc, but returns the first byte rather than the first character of a string.
Atn FunctionProvides the arctangent of a number as a return code.
Call StatementCalls a procedure.
CBool FunctionProvides as a return code an expression that has been converted to a variant of subtype Boolean.
Cbyte FunctionProvides as a return code an expression that has been converted to a variant of subtype Byte.
CDate FunctionProvides as a return code an expression that has been converted to a variant of subtype Date.
CDbl FunctionProvides as a return code an expression that has been converted to a variant of subtype Double.
Chr FunctionProvides as a return code the character associated with the ASCII code. This function can return 1 or 2 bytes.
ChrB FunctionReturns the byte associated with the ASCII code.
Cint FunctionProvides as a return code an expression that has been converted to a variant of subtype Integer.
Clear Method for the Err object Results in resetting all of the Err objects properties to a no-error state.
CLng FunctionProvides as a return code an expression that has been converted to a variant of subtype Long.
Cos FunctionProvides as a return code the cosine of an angle.
CSng FunctionProvides as a return code an expression that has been converted to a variant of subtype Single.
CStr FunctionProvides as a return code an expression that has been converted to a variant of subtype String.
CVErr FunctionProvides as a return code a variant of subtype Error containing a user-provided error code.
Date FunctionReturns the current system date.
DateSerial FunctionProvides as a return code a variant of subtype Date for the given month, day, and year.
DateValue FunctionProvides as a return code a variant of subtype Date for the given string.
Day FunctionProvides as a return code a whole number of 1 through 31, representing the day of the month for the given date string.
Description Property for he Err object The string that describes the current error.
Dim StatementDeclares variables.
Do…Loop StatementRepeats a block of code while a condition is True or until a condition becomes True.
Empty LiteralSignifies that a variable is uninitialized.
Erase StatementReinitializes fixed-size array elements and frees the storage space of dynamic arrays.
Err ObjectProvides information about run-time errors.
Eqv Logical operatorPerforms logical equivalence for the two expressions provided as operands.
Exit Do StatementExits a Do…Loop code block.
Exit For StatementExits a For…Next code block.
Exit Function StatementExits a function.
Exit Sub StatementExits a procedure.
Exp FunctionReturns e (the base of natural logarithms) raised to a power.
Fix FunctionProvides the integer portion of a number as a return code; if the number is negative, it provides the first number greater than the operand.
For…Next StatementRepeats a group of statements a designated number of times.
For Each…Next StatementRepeats a group of statements for each element in an array or a collection.
Function StatementDeclares a procedure-level block of code that returns a value when called.
HelpContext Property for The context ID in a corresponding help the Err objectfile for the current error.
HelpFile Property for the Err object The corresponding help file for the current error.
Hex FunctionProvides as a return code a string repre-senting the hexadecimal value of a number.
Hour FunctionProvides as a return code a whole number from 0 through 23, representing the hour of the day.
If…Then…Else StatementExecutes a group of statements, depending on the value of an expression.
Imp Logical operatorPerforms a logical implication on the two operands provided.
InputBox FunctionDisplays a dialog box with a prompt, appropriate buttons, and an input text box and then returns the input text and button response to the calling program.
Int FunctionProvides the integer portion of a number as a return code; if the number is negative, it provides the first number less than the operand.
InStr FunctionProvides the position indicator of the first character of the target string within the search string.
InstrB FunctionProvides the position indicator of the first byte of the target string within the search string.
Is OperatorCompares two object reference variables.
IsArray FunctionProvides as a return code a Boolean value signifying whether a variable is an array.
IsDate FunctionProvides as a return code a Boolean value signifying whether an expression can be converted to a date.
IsEmpty FunctionProvides as a return code a Boolean value signifying whether a variable has been initialized.
IsError FunctionProvides as a return code a Boolean value signifying whether an expression is an error value.
IsNull FunctionProvides as a return code a Boolean value signifying whether an expression contains no valid data (Null).
IsNumeric FunctionProvides as a return code a Boolean value signifying whether an expression can be evaluated as a number.
IsObject FunctionProvides as a return code a Boolean value signifying whether an expression repre-sents an OLE automation object.
LBound FunctionProvides as a return code the smallest available subscript for the stated dimen-sion of an array.
Lcase FunctionProvides as a return code a string that has been converted to lowercase.
Left FunctionProvides as a return code a specified number of characters from the left side of a string.
Len FunctionProvides as a return code the number of characters in a string or the number of bytes required to store a variable.
LenB FunctionProvides as a return code the number of bytes in a string or the number of bytes required to store a variable.
Log FunctionProvides as a return code the natural logarithm of a number.
Ltrim FunctionProvides a string that has leading (leftmost) spaces removed.
Mid FunctionProvides as a return code a described number of characters from a string.
Minute FunctionProvides as a return code a whole number from 0 through 59, representing the minute of the hour.
Mod OperatorDivides two numbers and returns only the remainder.
Month FunctionProvides as a return code a whole number from 1 through 12, representing the month of the year.
MsgBox FunctionShows a given prompt in a message dialog box with a button and returns the user's button response.
Not Logical operatorChecks for logical Not equivalent condition.
Nothing LiteralRemoves reference and frees memory.
Now FunctionReturns the current setting of your computer's system date and time.
Null LiteralIndicates no valid data state, as opposed to Empty, which indicates no data was ever assigned.
Number Property for the Err object The numeric error code for the current error.
Oct FunctionProvides as a return code a string repre-senting the octal value of a number.
On Error ResumeNext StatementIndicates that if an error occurs, control should resume at next statement.
Option Explicit StatementIndicates that variables must be declared.
Or OperatorPerforms a logical disjunction on two expressions.
Raise Method for the Err object Forces a run-time error to be raised.
Randomize StatementStarts a sequence for the random-number generator.
ReDim StatementAllocates or reallocates storage space and declares dynamic-array variables at the procedure level.
Rem StatementUsed to include comments in a program. The single quote, which achieves the same purpose, is used more often.
Right FunctionProvides as a return code a described number of characters from the right side of a string.
Rnd FunctionProvides a random number as a return code.
Rtrim FunctionProvides a string that has trailing (rightmost) spaces removed.
Second FunctionProvides as a return code a whole number from 0 through 59, representing the second of the minute.
Set StatementAssigns an object reference.
Sgn FunctionProvides as a return code an integer disclosing the sign of a number.
Sin FunctionProvides as a return code the sine of an angle.
Source Property for the Err object The source of the error for the current error.
Space FunctionReturns the given number of spaces.
Sqr FunctionProvides as a return code the square root of a number.
Static Statement (Procedure-level)Declares a persistent variable. Contents will be retained from one call to the next.
StrComp FunctionReturns a value signifying the outcome of a string comparison.
String FunctionReturns a repeating character string of the number of characters described.
Sub StatementDeclares procedures that do not provide a return code.
Tan FunctionProvides as a return code the tangent of an angle.
Time FunctionReturns a variant of subtype Date showing the current system time.
TimeSerial FunctionReturns a variant of subtype Date comprising the time for a specific hour, minute, and second.
TimeValue FunctionReturns a variant of subtype Date containing the time.
Trim FunctionProvides as a return code a duplicate of a string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim).
Ubound FunctionProvides as a return code the largest available subscript for the indicated dimension of an array.
Ucase FunctionReturns a string that has been converted to uppercase.
Val FunctionReturns the numbers enclosed in a string. Not supported in current beta.
VarType FunctionReturns a value disclosing the subtype of a variable.
vbAbort (See footnote*)Applies to the MsgBox function; Value = 3 indicates abort.
vbAbortRetryIgnore (See footnote*)Applies to the MsgBox function; Value = 2 displays Abort, Retry, and Ignore buttons.
vbApplicationModal (See footnote*)Applies to the MsgBox function; Value = 0 indicates that the application is modal. The user must respond to the message box before continuing work in the current application.
vbArray (See footnote*)Applies to the VarType function; Value = 8192 indicates array.
vbBoolean (See footnote*)Applies to the VarType function; Value = 11 indicates Boolean.
vbByte (See footnote*)Applies to the VarType function; Value = 17 indicates byte.
vbCancel (See footnote*)Applies to the MsgBox function; Value = 2 indicates cancel.
vbCr (See footnote*)General purpose; indicates carriage-return character.
vbCritical (See footnote*)Applies to the MsgBox function; Value = 16 displays critical message icon.
vbCrLf (See footnote*)General purpose; indicates carriage-return and line-feed characters.
vbCurrency (See footnote*)Applies to the VarType function; Value = 6 indicates currency.
vbDataObject (See footnote*)Applies to the VarType function; Value = 13 indicates non-OLE automa-tion object.
vbDate (See footnote*)Applies to the VarType function; Value = 7 indicates date.
vbDefaultButton1 (See footnote*)Applies to the MsgBox function; Value = 0 indicates the first button is the default.
vbDefaultButton2 (See footnote*)Applies to the MsgBox function; Value = 256 indicates the second button is the default.
vbDefaultButton3 (See footnote*)Applies to the MsgBox function; Value = 512 indicates the third button is the default.
vbDouble (See footnote*)Applies to the VarType function; Value = 5 indicates a double-precision, floating-point number.
vbEmpty (See footnote*)Applies to the VarType function; Value = 0 indicates empty.
vbError (See footnote*)Applies to the VarType function; Value = 10 indicates error.
vbExclamation (See footnote*)Applies to the MsgBox function; Value = 48 displays the warning message icon.
vbFalse (See footnote*)General purpose; indicates Boolean value represented by 0.
vbFriday (See footnote*)Applies to the Weekday function; Value = 6 indicates Friday.
vbIgnore (See footnote*)Applies to the MsgBox function; Value = 5 indicates ignore.
vbInformation (See footnote*)Applies to the MsgBox function; Value = 64 displays information message icon.
vbInteger (See footnote*)Applies to the VarType function; Value = 2 indicates integer.
vbLf (See footnote*)General purpose; indicates line-feed character.
vbLong (See footnote*)Applies to the VarType function; Value = 3 indicates long integer.
vbMonday (See footnote*)Applies to the Weekday function; Value = 2 indicates Monday.
vbNo (See footnote*)Applies to the MsgBox function; Value = 7 indicates no.
vbNull (See footnote*)Applies to the VarType function; Value = 1 indicates null.
vbObject (See footnote*)Applies to the VarType function; Value = 9 indicates OLE automation object.
vbObjectError (See footnote*)Indicates an object-generated error.
vbOK (See footnote*)Applies to the MsgBox function; Value = 1 indicates OK.
vbOKCancel (See footnote*)Applies to MsgBox function; Value = 1 displays OK and Cancel buttons.
vbOKOnly (See footnote*)Applies to the MsgBox function; Value = 0 displays OK button only.
vbQuestion (See footnote*)Applies to the MsgBox function; Value = 32 displays warning query icon.
vbRetry (See footnote*)Applies to the MsgBox function; Value = 4 indicates retry.
vbRetryCancel (See footnote*)Applies to the MsgBox function; Value = 5 displays Retry and Cancel buttons.
vbSaturday (See footnote*)Applies to the Weekday function; Value = 7 indicates Saturday.
vbSingle (See footnote*)Applies to the VarType function; Value = 4 indicates single-precision, floating-point number.
vbString (See footnote*)Applies to the VarType function; Value = 8 indicates string.
vbSunday (See footnote*)Applies to the Weekday function; Value = 1 indicates Sunday.
vbSystemModal (See footnote*)Applies to the MsgBox function; Value = 4096 indicates system modal. All applications are suspended until the user responds to the message box.
vbThursday (See footnote*)Applies to the Weekday function; Value = 5 indicates Thursday.
vbTrue (See footnote*)General purpose; indicates Boolean value represented by -1.
vbTuesday (See footnote*)Applies to the Weekday function; Value = 3 indicates Tuesday.
vbUseSystem (See footnote*)Applies to the Weekday function; Value = 0 uses the NLS API setting.
vbVariant (See footnote*)Applies to the VarType function; Value = 12 indicates variant.
vbWednesday (See footnote*)Applies to the Weekday function; Value = 4 indicates Wednesday.
vbYes (See footnote*)Applies to the MsgBox function; Value = 6 indicates yes.
vbYesNo (See footnote*)Applies to the MsgBox function; Value = 4 displays Yes and No buttons.
vbYesNoCancel (See footnote*)Applies to the MsgBox function; Value = 3 displays Yes, No, and Cancel buttons.
Weekday FunctionReturns a whole number showing the day of the week.
While…Wend StatementPerforms a series of statements as long as a given condition is True.
Xor Logical operatorPerforms logical exclusion on the operands.
Year FunctionReturns a whole number showing the year for the date string representation.
*The symbols with the vb prefix are values expected by various VBScript functions. In Visual Basic these are intrinsic contants. In VBScript, you must declare these as variables if you wish to treat them as constants since there is no direct constant support.