Now that Module 2 has familiarized you with the components Active Server Pages (ASP) provides, its time to think about creating your own components, components that meet your specific needs.
Suppose that you want to provide access to specific financial functions through your Web site. ASP does not explicitly provide access to such functionality, but getting it is as easy as creating your own ActiveX server componentwhich you will do in this module. You will call your component from a form like the one that follows.
Note To complete this module, you must have the 32-bit version of either Visual Basic 4.0 Professional Edition or Visual Basic 4.0 Enterprise Edition installed on your computer.
A component should contain a set of related methods (functions) that provide added value beyond what is in the scripting language that will be calling it. Because VBScript does not provide financial functions, you must give access to this functionality by using your Finance server component. This server component could expose all of the Visual Basic finance functions including the DDB function (double-declining balance), FV function (future value), IPmt function (interest payment), IRR function (internal rate of return), and others. Only the implementation of the FV function will be documented in this tutorial.
The Visual Basic Help system describes the available functions.
Visual Basic uses the project name as the first part of the name that is referenced in order to use the server component in an ASP script.
The project is now named MS. Later, you will reference the Finance server component as MS.Finance
from an ASP script.
The next section lays the groundwork for the Finance server component. Methods and properties provide the interface between ActiveX Server components and Visual Basic scripts and other languages. Because browsers can request a script to run on the server, the executing script can ask for information from a server component and then format and return that information to the browser. The server component must not bring up a dialog box on the servers screen because the screen will not be displayed on the client browser.
Visual Basic version 4.0 automatically creates a form. You will remove this default form from your project because it will not be used.
In Visual Basic, to create a component with a set of functionality you can call, you define a class. This class groups methods and properties. In your project, it will be the place within which you specify your finance methods.
The Finance server component does require some programming code. This code will make the Visual Basic built in future value function available to languages making use of your component.
In the Finance class window, type the following lines:
Public Function CalcFV(rate, nper, pmt, Optional pv, Optional whendue) CalcFV = FV(rate, nper, pmt, pv, whendue) End Function
All server components require an entry (starting) point. This is the code that will be called when the object is first made available to a language. In VBScript, when you use Server.CreateObject, an instance is created of an object. When the Server.CreateObject statement is executed the Sub Main procedure in a server component (created with Visual Basic) is called.
Your finance component does not have to do anything special to initialize itself when it is called. For that reason, you only have to provide an empty (no Visual Basic statements) Sub Main procedure.
This automatically enters the following code:
Sub Main()
End Sub
When you save your work, you will be asked to save all three parts of the Visual Basic project. These include the project file, the class module, and the code module.
Visual Basic allows you to create in-process ActiveX components (OLE Automation Servers) and out-of-process ActiveX components. An in-process ActiveX component is a dynamic-link library (file name extension .dll that is loaded by the calling process. An out-of-process ActiveX component is an executable (file name extension .exe) that runs as a separate process from the calling application. Because in-process components are in the same process space as the calling process, they provide better performance than out-of-process components.
To make the Finance server component an in-process ActiveX component
All server components must be registered. Windows NT and Windows 95 make use of the system registry to keep track of what server components are available for use. By registering the Finance server component, you make it callable by VBScript and all of the other OLE-compatible languages on your computer.
To test the component, you can call the component from Active Server Pages (ASP), Visual Basic, Microsoft® Office products that use Visual Basic for Applications, or any other OLE Automation controller.
To call the Finance server component from Active Server Pages by using VBScript, you can use an HTML form as input to calculate the future value of a persons savings plan.
An HTML form will be used to gather values that describe a savings plan. These values are assigned variables that are made available to an
ASP script as part of the Request object. You can reference a value from an HTML form. For example, the annual percentage
rate entered on a form can be referenced by a script using Request("APR")
. The HTML tag <INPUT TYPE=TEXT NAME=APR>
provides
the input field necessary to enter a value.
To send the form to a server running Microsoft Internet Information Server and ASP, the user presses a Submit button. The
Submit button calls the page indicated by the ACTION
property of the HTML form tag. The HTML tag for the Submit button (<INPUT
TYPE=SUBMIT VALUE=" Calculate Future Value ">
) uses the value for ACTION
from the HTML form tag (<FORM METHOD=POST
ACTION="Finance.asp">
) to call the ASP page Finance.asp.
We have created the form for you. Use your text editor to open the file Finance.htm in the Lessons directory (\Inetpub\Aspsamp\Tutorial\Lessons by default).
VBScript is used to call your Finance server component. The script starts by validating the inputs from the HTML form and assigning default values for any values that were not entered on the form. The VBScript IsNumeric function is used to test whether or not a numeric (valid) value was entered for each of the text inputs on the HTML form.
Server.CreateObject is used to create an instance of (that is, make usable) your Finance component named MS.Finance
. Once an instance
of a server component is created, you can make use of its methods and properties. On the script line immediately following
Server.CreateObject, the method CalcFV is used to calculate a savings plans future value. The result of this calculation is then sent to the
browser of the user requesting the information.
To view the script, use a text editor to open the file Finance.asp in the Lessons directory (\Inetpub\Aspsamp\Tutorial\Lessons by default).
To run the Finance.asp ASP page, open the Finance.htm form, which then calls the Finance.asp script to calculate the future value of the savings plan specified on the HTML form.
The value of your savings plan should appear.
In a relatively short time you have created a useful ActiveX server component. If you need access to other financial functions, you can implement the other financial functions built into Visual Basic as additional methods of your Finance server component.