The Basics of VBScript and Active Server Pages

VBScript is a rich, yet simple scripting language that forms the foundation of Active Server Pages. A full discussion of the language and the environment in which an ASP page executes is beyond the scope of this manual. This section, however, provides a reference to the capabilities most often needed by a WordPage author. Many online and printed resources exist that show all of the details of the VBScript language and the ASP object model.

 

To author complex ASP documents using WordPage it is important to understand how commands may be interspersed with document text. Commands are the parts of the document contained in curly braces. The key concept is that document text may appear anywhere that VBScript allows statements. For example, the if…then…end if command shown below may be broken up into three command blocks like this:

 

{if data.amount>1000then} Some document text {else} Some other text {end if}

 

In the following lists items in bold represent keywords that are used exactly as shown. Italics represent the items that you control. Items in brackets [ ] are optional.

 

Looping and Conditional Execution

Do while condition

Statements

Loop

Loop as long as the condition is true. Note that you may replace “while” with “until” to loop as long as the condition is false.

Do

Statements

Loop while condition

This “Do” is the same as the earlier one except that the condition is checked at the end of the loop.

For variable = start to end [step increment]

Statements

Next

Execute the statements incrementing the variable over the specified range.

If condition then

Statements1

[elseif

Statements2]

[else

Statements3]

End if

Execute the code in Statements1 if the condition is true. If any elseif blocks are found they are tested in the order in which they are encountered. You may have as many elseif blocks as you need. The else block holds the code to execute if none of the other statement blocks is executed.

While condition

Statements

Wend

Execute the statements as long as the condition is true. This is the same as the Do while…Loop command discussed above.

 

Variable Creation and Assignment

Variable = expression

Assign a new value to the variable. If the variable does not exist this command creates it. This works for all types of data other than objects.

Set variable = expression

This is the form of assignment that is required if the expression is an object reference.

Dim variable[(dimension)]

Declare a variable. This is only required if you want to declare an array by specifying a dimension.

 

Arithmetic and String Operators

Value1 + value2

Add value1 and value2 (may also used be to concatenate strings)

Value1 value2

Subtract value2 from value1

Value1 * value2

Multiply value1 by value2

Value1 / value2

Divide value1 by value2

Value1 & value2

Concatenation string value2 to string value1

Value1 > value2

True if value1 greater than value2

Value1 < value2

True if value1 less than value2

Value1 = value2

True if value1 equals value2

Value1 >= value2

True if value1 greater than or equal to value2

Value1 <= value2

True if value1 less than or equal value2

Value1 <> value2

True if value1 not equal to value2

Value1 and value2

True if both values are true

Value1 or value2

True if either value is true

Value1 xor value2

True if only one of the values is true

Arithmetic Functions

Abs(value)

Return the absolute value of value.

Int(value)

Return the integer portion of value.

Sgn(value)

Return 0 if value is 0, -1 if value is negative, and 1 if value is positive.

Rnd

Return a random number between 0 and 1.

Round(value[,numdecimalplaces])

Round value to numdecimalplaces decimal places (default is 0).

 

Commonly Used String Functions

Asc(string)

Return the ASCII value of the first character in the string.

Chr(value)

Return a string consisting of a single character with the specified ASCII value.

InStr([start,]string1,string2[,flag])

Look for string2 in string1 starting at character start. InStr returns the character position at which the match starts. If flag is 0 the search is case sensitive. If flag is 1 the search is not case sensitive. InStr returns 0 if string2 is not found.

InStrRev(string1,string2[,start[,flag]])

Perform the same operation as InStr, but go backwards from the end of string1.

Len(string)

Return the length of a string

LCase(string)

Return the lower case version of a string

UCase(string)

Return the upper case version of a string

Left(string,length)

Return characters from the start of a string.

Mid(string,start[,length])

Return characters from the middle of a string. If length is omitted then all characters from start to the end of string are returned.

Right(string,length)

Return characters from the end of a string.

Space(count)

Return a string of the specified number of blanks.

Split(string[,delimiter[,count[,flag]]])

Break string into pieces. Delimiter is a space if not specified. Count is the number of strings to return (-1 for all). If flag is 0 the comparison is case sensitive. If flag is 1 the comparison is not case sensitive. The return value is an array containing the resulting strings.

StrComp(string1,string2[,flag])

Compare string1 to string2. If flag is 0 the comparison is case sensitive. If flag is 1 the comparison if not case sensitive. The return value is –1 if string1<string2, 0 if string1=string2, and 1 if string1>string2.

String(count, character)

Return a string containing count copies of the first character in the character string.

LTrim(string)

Returns a copy of the string without any leading spaces.

RTrim(string)

Returns a copy of the string without any trailing spaces.

Trim(string)

Returns a copy of the string without leading or trailing spaces.

 

Conversion Functions

CDate(expression)

Convert an expression to a date so that other date function can be applied to it.

CInt(expression)

Convert an expression to an integer.

CStr(expression)

Convert an expression to a string.

DateSerial(year,month,day)

Creates a date from the specified year, month, and day.

TimeSerial(hour,minute,second)

Create a time from the specified hour, minute, and second.

Date/Time Functions

Date

Get the current date.

DateAdd(interval,number,date)

Add number intervals to a date. Interval can be:

Value
Meaning

yyyy

Year

q

Quarter

m

Month

d

Day

w

Weekday

ww

Week of year

h

Hour

n

Minute

s

Second

For example DateAdd(“d”, 3, Date) returns 3 days from today.

DateDiff(interval,date1,date2)

Return the number of intervals between date1 and date2. Interval has the same meaning as in the DateAdd function above.

DatePart(part, date)

Return part of date. Part is specified using the same values as the interval in DateAdd.

Day(date)

Return the day portion of a date.

Hour(time)

Returns 0-23 representing the hour portion of time.

Minute(time)

Returns 0-59 representing the minute portion of time.

Month(date)

Return the month portion of a date.

MonthName(month[, abbreviate])

Return a string that is the name of the specified month. Month is a number from 1-12. If abbreviate is True the 3 letter abbreviation is returned.

Now

Return the current date and time.

Second(time)

Returns 0-59 representing the second portion of time.

Time

Return the current time.

Weekday(date[,firstdayofweek])

Return the day of the week date falls on. Firstdayofweek can be 0 (default), or 1-7 for Sunday through Saturday. The return value is 1-7 for Sunday through Saturday.

WeekdayName(weekday[, abbreviate[, firstdayofweek]])

Weekday is a logical day of the week number. Firstdayofweek is handled the same as in the Weekday function. If abbreviate is True the 3 letter abbreviation is returned.

Year(date)

Return the year portion of a date.

String Formatting Functions

FormatCurrency(value [, digitsafterdecimal [,includeleadingdigit [,useparensfornegative [,groupdigits]]]])

Format value to have digitsafterdecimal digits to the right of the decimal place. The default is to use the system settings. Includeleadingdigit, useparensfornegative, and groupdigits are interpreted as –1=True, 0=False, and –2=Use system settings. The system currency symbol is also included.

FormatDateTime(value[,format])

Format a date according to the format specified below:

Value
Meaning

0

General date/time

1

Long date

2

Short date

3

Long time

4

24 hr format

FormatNumber(value [, digitsafterdecimal [,includeleadingdigit [,useparensfornegative [,groupdigits]]]])

Format like FormatCurrency except for the currency symbol.

FormatPercent(value [, digitsafterdecimal [,includeleadingdigit [,useparensfornegative [,groupdigits]]]])

Format like FormatNumber except that the value is first multipled by 100 and a trailing % character is added.

Commonly Used ASP Functions

Request(name)

Retrieve information from the received URL, form, or cookies.

Server.CreateObject(objectname)

Create a server-side COM object for use within your ASP application.