Building Internet applications

by Peter Vogel

If you've ever tried to create an interactive, Web-based application, you know that "doing it with HTML" is considerably more difficult than building applications with Visual Basic. Among the problems you may have discovered are maintaining state information (what did the user do last?), getting information from the client browser, and dealing with the capabilities of different browsers. Not to mention that retrieving data from a database is just flat-out impossible.

If you've despaired of ever moving into the lucrative world of Internet development, there is hope--and it includes using Visual Basic. The solution to your problems is called Active Server Pages (ASP), a product Microsoft announced at the Site Builder Network conference in November 1996. And best of all, you can download the program free from www.microsoft.com/iis/default.asp.

ASP provides an Internet Information Server development environment for programmatically generating Web pages using ActiveX technologies. ASP is part of the Active Server toolset that, along with Active Desktop, makes up the Active Platform, Microsoft's strategy for delivering Internet and intranet solutions. In this article, we'll offer an introduction to how ASP works.

Getting started

First, you'll be happy to know that if you've been building HTML pages, you already have a lot of the raw material you need for creating Active Server Pages: Simply rename your *.HTM files to *.ASP files. When the Internet Information Server (IIS) receives a request for an ASP file, it invokes special processing that's a superset of standard HTML. This superset includes handling a set of tags delimited by <%..%>--these tags enclose code written in any ActiveX scripting language you care to use. (Presently, you're limited to JScript and VBScript.)

For example, consider an existing HTM file that looks like this:

<HTML>
<BODY>
Today's date is 08/01/97
</BODY>
</HTML>

Suppose we rename this file to an ASP file and extend it to take advantage of ASP's scripting functionality. We could change it as follows:

<HTML>
<BODY>
Today's date is <%=Date()%>
</BODY>
</HTML>

When IIS sees the <%..%> delimiters, it will invoke the current scripting engine to process the text between the tags. The <%=code%> syntax will cause any text generated by the code to be inserted into the Web page before it's sent to the user. In the case of our example, which includes the Date() function, the current date will be inserted into the page. This intermixing of HTML text and code--with the script living on the page rather than in a separate code file--will look familiar to VBScript programmers.

In the end, it doesn't matter whether the page sent to the user comes from the standard HTML file or the ASP-enhanced version. In either case, the HTML that the browser receives will look like this:

<HTML>
<BODY>
Today's date is 08/01/97
</BODY>

And, once the browser processes the HTML tags, the user will see the following line:

Today's date is 08/01/97

ASP pages can include VBScript routines and OBJECT tags (which call in ActiveX controls) as part of the HTML sent to the browser. This inclusiveness opens the possibility of dynamically generating the programs sent to run in the client browser. Essentially, ASP could custom generate a VBScript program to execute inside the user's Web browser each time the page is accessed.

Since the browser sees the same HTML whether the page is generated from a static file or from an ASP-scripted file, Active Server Pages are "browser neutral." An ASP script can generate HTML that any browser from Mosaic 1.0 to Internet Explorer 3.0 can process. Nor do ActiveX scripts have to be recompiled after a change, unlike Perl scripts and other CGI-based methods. Instead, ASP pages take advantage of just-in-time compilation to recompile before use.

More power

ASP can access ActiveX server components (formerly "OLE automation servers") to extend its functionality. One component, for instance, ensures that you don't have to target your HTML for the lowest-common-denominator browser. By accessing the BrowserType object of the MSWC server, ASP allows you to check a browser's capabilities and choose how you want to deal with it.

The BrowserType object contains a set of properties that let the ASP developer determine which functionality the user's browser supports, as follows:

<%Set UBrwsr=Server.CreatObject("MSWC.BrowserType")
If UBrwsr.ActiveXControls=False Then%>
<IMG SRC="/Sub.GIF">
<%End If%>

This sample code accesses the properties of the BrowserType object to see if the user's browser supports ActiveX controls. The text in the If...End If block is included in the page sent to the browser only when the If condition is true. In this case, if the browser doesn't support ActiveX controls, the tag to display a static GIF file is added to the HTML to be returned to the user. Presumably, if the browser does support ActiveX controls, you'd write to the page an OBJECT tag to download a control.

It's important to realize, however, that the MSWC server doesn't actually query the user's browser. Instead, it uses the User Agent HTTP header sent by the browser to pull information out of the file BROWSCAP.INI. You'll want to be sure that this file contains the latest information on the capabilities of the browsers you expect to hit your site.

You can also create your own server components using any language that can create an ActiveX server. This, of course, includes VB 4.0 and 5.0. The servers can be implemented as either in-process or out-of-process servers. In fact, thanks to DCOM, these servers don't have to reside on the same computer as IIS, but can be distributed to any other server on the network. Since ASP pages execute in a controlled environment on the server, they aren't implemented in a sandbox model but have full access to all the resources of the server.

ASP offers a variety of other services to the developer. The Request object provides access to information returned from the browser (for instance, URL parameters and cookies). As an example, if the browser activates your ASP page with the URL www.mysite.com/authors.asp?subject=IDC, one of the pieces of information you'll want is the value of the subject parameter (in this case, IDC). With ASP, you'd use the code

strSubject = Request.QueryString("Subject")

The Response object lets you return information to the browser by embedding it in the Web page as follows:

Response.Write "The subject is " & strSubject

The <%=..%> syntax is a short form for Response.Write.

A subject for another article is the Session object, which allows you to maintain server state information over many HTTP requests. Since HTTP is essentially a "stateless" protocol, this object offers a way to overcome one of the biggest problems in creating intranet applications: keeping track of what the user did last.

Getting data

The Active Data Object (ADO) server is one of the more powerful servers on the market. Microsoft also introduced ADO at the Site Builder conference, as part of the Active Server platform. ADO provides automation-based, language-independent access to data. It rides on top of OLE DB as a set of objects for accessing heterogeneous OLE DB components.

OLE DB, in turn, defines a set of COM interfaces for accessing all types of data, even non-relational sources. While OLE DB includes interfaces to exploit full-featured data stores (for instance, SQL databases), developers can also access Excel spreadsheets through OLE DB and, by extension, through ADO.

Before you can access any data with ADO, you must set up an ODBC data source on the server using the 32-bit ODBC Administrator (which you can find in the Windows Control Panel). The ODBC source must be a System Data Source Name (DSN). Only by establishing the data source as a System DSN can other Windows NT services, like IIS, use it. For the purposes of this example, we'll assume that you've created an ODBC DSN based on an Access database and called MySource.

Among others, we'll use the following ADO server objects to get at the data:

Let's look at an example using VBScript. To get at a table in the Access database, you must obtain a reference to an ADO DBConnection object. Since VBScript doesn't support any variable type but variants, you can see that no declaration is required here:

Set dbc = Server.CreateObject("ADODB.Connection")

Once you have the connection object, you can open the data source. Doing so involves passing an ODBC connection string to the Open method of the dbc object, as follows:

dbc.Open "DSN=MySource;"

You can supply more parameters in the ODBC connection string if, for instance, the data source requires a password or user ID.

Now that the data source is open, you can create a recordset from it, using the Execute method:

Set rec = dbc.Execute("Select * from MyTable;")

You've probably noticed that ADO has far fewer objects and methods than DAO, the object-based data-access method used by Visual Basic and Access. In this example, dbc--the DBConnection object--fills the place of the DAO Workspace and Database objects. The DBConnection object's Execute method fulfills all the functions of the DAO Database object's OpenRecordset and Execute methods.

Finally, you move through the records in the recordset and write the data to the page with code something like this:

While not rec.eof
  Response.Write rec("MyField")
  rec.MoveNext
Wend

Conclusion

ASP, combined with VBScript executing in the browser, forms a bridge for VB programmers to start creating true Internet applications. You'll need to develop some familiarity with the server objects that make up this new environment. With that knowledge under your belt, and some understanding of DAO and the CreateObject command, you shouldn't have any trouble turning your VB skills into those Internet skills that employers want their new hires to possess these days. Best of luck!

Peter Vogel is the applications supervisor at Champion Road Machinery, a professional storyteller, and a Cub leader. You can reach him at peter.vogel@odyssey.on.ca. Peter has written a number of articles on Visual Basic, Access, and Web development, in addition to presenting on Web tools at the VBTeach conference.