VBScript Declarations
Variables can be of only one type—variant. A variant is a variable that can hold any value—a string, number,
object, you name it. VBScript also provides a number of built-in functions that it inherited from Visual Basic,
such as the MsgBox(), CreateObject(), and IsNumeric() functions, to name a few.
You will see how you can use VBScript to manipulate a database via an Active Server Page. VBScript is
not limited to ASPs. Microsoft has taken this scripting language to the next level. VBScript can now be
used from the command line and can be used to handle non-interactive administration tasks. Let’s take
a look at some of the basic programming tasks you can accomplish with VBScript.
Program Flow
VBScript provides all the same conditional statements, flow control, and processing that its parent, Visual
Basic possesses. For example, VBScript supports conditional statements like If...Then ...Else. The
general syntax pattern is as follows:
If expression Then
Do this
Else
Do This
End If
Notice that in VBScript (as well as Visual Basic) you must include the word Then. Also, to end an
If...Then statement you must use End If. Look at the following example:
Dim vCount
Dim vLastName
If vCount > 1 Then
Response.Write("The name is " & vLastName)
Else
Response.Write("<p></p>")
End If
In the first two lines of the example, variables are declared. The next block of code is the If...Then
segment. Here, if the expression vCount>1 is a true statement, you will write the statement to the Web
page. If it equates to false, nothing is written out. This is a simple example. Your conditional statements
can be more complex but will follow the same structure.
Looping Control
VBScript also supports Do While, Do Until, While, and For...Next as general flow control
statements. You would use a loop to walk through a recordset or complete a task a number of times before
moving on. In VBScript, a typical loop can look like the following:
Do Until rstCustomers.EOF
Response.Write("<P>" & rstCustomers("LastName") & "</P>")
RstCustomers.MoveNext
Loop
In this example, whatever is in between the Do and Loop keywords will continue until the expression
rstCustomers.EOF equates to TRUE. This type of statement is handy for walking through records
sets.
VBScript also can perform calculations, such as addition and subtraction. It supports comparison
operators as well. It is not recommended that you do complex computations in an Active Server Page.
Remember that the code in an ASP is performed on the server prior to being downloaded to the client. If
complex computations are being performed, this could delay the response to the client's request.