ASP and VB Script - Introduction

Active Server Pages (ASP) allow HTML and scripts to be included in a page and be processed by the Microsoft Internet Information Server (IIS) version 3 and above, providing equivalent interactivity of a CGI program in other Web servers.

We have seen previously the ASP model and how to install the Personal Web Server (PWS) to develop and test ASP scripts. You can also see a tutorial from Microsoft on ASP. We also reviewed Visual Basic as related to Web Programming. In this tutorial we will see how to use VB Script -- a subset of Visual Basic -- to write simple ASP scripts.

VB Script setup and documentation

Before you start installing VB Script be sure you have installed the latest version of Internet Explorer (at least 5.1), for it will update and add components to Windows needed by VB Script.

The basic reference to VB Script is the Microsoft VB Script page. To setup VB Script in your machine download its latest version from Microsoft, selecting your version of Windows and language. For example for English, Windows 98, ME & NT 4 download scr56en.exe, and for Windows 2000 is scripten.exe. You should also download a copy of the VB Script documentation to your machine.

VB Script basics

VB Script and ASP

Define at the top of the page to be saved as .asp the default script language as follows:

<%@ LANGUAGE = "VBScript"%>

Active Server Pages recognize script commands entered between the pair <%   %> . In this format it handles code, but not necessarily it will produce output, you need to explicitly use output commands in order to produce output. When you use the pair <%=   %> any variable, function will produce automatically output. Here are two simple examples displaying Hello! in the browser:

<% Response.Write("Hello!") %>      ----> explicit output

<%= "Hello!" %>      ----> implicit output

We will see and discuss four examples in order to understand how VBScript is used within the ASP tags.

  1. The first example is our "old friend" Hello World! script:


    <title>Hello World</title> </head>

    <body bgcolor="White">
    <div align="center"><h1>Hello World</h1></div>
    <%Response.Write("Hello World!")%>


    As you can see we used the explicit ASAP tags to have Hello World! sent to the browser and everything else is HTML.

  2. Our second example shows how an ASP page URL replaces the CGI program in a form:


    <title>Form with ASP</title>

    <body bgcolor="White">
    <div align="center"><h1>Forms with ASP</h1>
    <form method="POST" action="http://L500/decode.asp">
    Name: <input type="text" name="name" size="60" maxlength="80"><br>
    <input type="submit" value="Submit"><input type="Reset" value="Reset"></div>


  3. The third example is a little more interesting showing how a function is defined and used, as well as, how you can make decisions in your script:


    <TITLE>Date and time ASP Page</TITLE>
    <% Private Function Hello()
    Dim timNow
    timNow = Time
       If timNow <= CDate("12:00:00") Then
         Hello = "Good morning"
       ElseIf timNow <= CDate("18:00:00") Then
         Hello = "Good afternoon"
         Hello = "Good evening"
       End If
    End Function%>

    <div align="center"><%= Hello %>! Today is <%= Date %> and the day and time are <%Response.Write Now%></div>

    Please note that once I opened the first tag <% I could start writing the script, almost as if I was writing a VB program not a VBScript. Please note that Time and Now are VBScript functions, not variables I created. Please also note that I used as the return value -- Hello -- the name of the function.

    The line <div align="center"><%= Hello %>! Today is <%= Date %> and the day and time are <%Response.Write Now%></div> does two interesting things:

  4. The final example is the Fahrenheit to Celsius conversion program we saw first in Perl:


    <title>convert to Celsius</title>
    <body bgcolor="White">
    <div align="center"><h1>Convert to Celsius</h1></div>
    <% Dim sFahrenheit, sCelsius, strMethod
    strMethod = Request.ServerVariables("REQUEST_METHOD")
    If strMethod = "GET" Then
    Response.Write "<html>"
    Response.Write "<head>"
    Response.Write "<title>Input of convert</title>" Response.Write "</head>"
    Response.Write "<body bgcolor=White>"
    Response.Write "<form action=convcelsius.asp method=Post>"
    Response.Write "<p>Please enter the degrees in Fahrenheit: <input type=text name=txtFahrenheit size=6 maxlength=6></p>"
    Response.Write "<input type=submit value=Submit><input type=Reset>"
    Response.Write "</form></div>"
    Response.Write "</body>"
    Response.Write "</html>"
    sFahrenheit = CSng(Request.Form("txtFahrenheit"))
    sCelsius = round((5 / 9 * (sFahrenheit - 32)),1)

    Response.Write (sFahrenheit & " degrees Fahrenheit are " & sCelsius & " degrees Celsius")
    End If %>

    This example was written in a very similar way a CGI program in Perl is written. Note the following:

ASP Objects

It is beyond the scope of this tutorial the detailed description of the various ASP objects. You can read a brief introduction to the subject on this tutorial.

You can see other examples of VB Script used with ASP at the Microsoft site, or other sites on the Web, like this or this other one.

This page is maintained by Al Bento who can be reached at This page was last updated on November 26, 2004. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.