<h1><a name="ChapTop">Module 3: Writing Your Own ActiveX Server Components</a></h1>
<p>Now that <a href="atumd2.asp">Module 2</a> 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.
<p>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.
<p><strong><a name="note">Note</a> </strong>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.
<!--The purpose of the server component is to
provide any OLE-compliant program (including ASP) access to financial functions. This component will be created using Visual
Basic. Visual Basic has a rich set of financial functions that will be exposed as methods of your Finance component. </p> -->
<hr>
<h2><a name="CreatingtheFinanceComponent">Lesson 1: Creating the Finance Component</a></h2>
<p>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 <strong>DDB</strong> function (double-declining
balance), <strong>FV</strong> function (future value), <strong>IPmt</strong> function (interest payment), <strong>IRR</strong> function (internal rate of return), and others. Only the
implementation of the <strong>FV</strong> function will be documented in this tutorial. </p>
<li>Click <strong>Visual Basic 4.0</strong> in the submenu to run the design environment. </li>
</ol>
<h3><A NAME="LearnMoreAboutVisualBasicFinanceFunctions">Learn More About Visual Basic Finance Functions</A></h3>
<p>The Visual Basic Help system describes the available functions.</p>
<ol>
<li>Click <strong>Help</strong>. </li>
<li>Select <strong>Search For Help On</strong>.</li>
<li>With the <strong>Index</strong> tab selected, type <strong>finance</strong> as the word to look for.</li>
<li>Double-click the <strong>finance</strong> index entry. </li>
<li>Click <strong>FV Function</strong> to learn more about it.</li>
<li>Close the <strong>Visual Basic Help</strong> dialog box when you have finished reviewing the finance functions.</li>
</ol>
<h3><A NAME="NametheProject">Name the Project</A></h3>
<p>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. </p>
<ol>
<li>Click <strong>Tools</strong>. </li>
<li>Select <strong>Options</strong>.</li>
<li>Click the <strong>Project</strong> tab. </li>
<li>Double-click the value <strong>Project1 </strong>in the <strong>Project Name</strong> text box. </li>
<li>Type <strong>MS</strong> and click <strong>OK</strong>. </li>
</ol>
<p>The project is now named MS. Later, you will reference the Finance server component as <FONT FACE="COURIER"><code>MS.Finance</code></FONT> from an ASP script. </p>
<h3><A NAME="Removethedefaultform">Remove the default form</A></h3>
<p>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.</p>
<p>Visual Basic version 4.0 automatically creates a form. You will remove this default form from your project because it will not be used. </p>
<ol>
<li>In the <strong>View</strong> menu, select <strong>Project</strong>. </li>
<li>Select <strong>Form1</strong> within the <strong>Project1</strong> project window. </li>
<li>In the <strong>File</strong> menu, select <strong>Remove File</strong>. </li>
</ol>
<h3><A NAME="AddtheFinanceClasstotheProject">Add the Finance Class to the Project</A></h3>
<p>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. </p>
<ol>
<li>From the <strong>Insert</strong> menu, choose <strong>Class Module</strong>. </li>
<li>Press the F4 key to display the property sheet for Class1. </li>
<li>Click the value <strong>0 - Not Creatable</strong> for <strong>Instancing</strong>. </li>
<li>Click the arrow, then select <strong>2 - Creatable MultiUse</strong>. </li>
<li>Triple-click <strong>Class 1</strong> to select the class name. </li>
<li>Type <strong>Finance</strong> to change the class name. </li>
<li>Click <strong>False </strong>for the property <strong>Public</strong>.</li>
<li>Type <strong>t </strong>to select <strong>True</strong> for the property <strong>Public</strong>. </li>
<li>Close the <strong>Finance</strong> property sheet </li>
</ol>
<h3><A NAME="AddtheCalcFVFunctiontotheFinanceclass">Add the CalcFV Function to the Finance class</A></h3>
<p>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. </p>
<p>In the Finance class window, type the following lines:</p>
<blockquote>
<FONT FACE="COURIER"><pre>Public Function CalcFV(rate, nper, pmt, Optional pv, Optional whendue)
CalcFV = FV(rate, nper, pmt, pv, whendue)
End Function</pre></FONT>
</blockquote>
<h3><A NAME="AddtheComponentsEntryPoint">Add the Components Entry Point</A></h3>
<p>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 <strong>Server.CreateObject</strong>, an instance is created of an object. When the <strong>Server.CreateObject </strong>statement
is executed the <strong>Sub Main</strong> procedure in a server component (created with Visual Basic) is called. </p>
<p>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) <strong>Sub</strong> <strong>Main</strong> procedure. </p>
<ol>
<li>In the <strong>Insert </strong>menu, select <strong>Module</strong>.<br>
</li>
<li>In the <strong>Module 1</strong> window, type <strong>Sub Main.</strong><br>
</li>
<li>Press ENTER.<br>
</li>
</ol>
<blockquote>
<p>This automatically enters the following code: </p>
</blockquote>
<blockquote>
<p><FONT FACE="COURIER"><code>Sub Main()<br>
End Sub</code></FONT> <br>
</p>
</blockquote>
<h3><A NAME="SavetheFinanceProject">Save the Finance Project</A></h3>
<p>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. </p>
<ol>
<li>Open the <strong>File </strong>menu.</li>
<li>Select <strong>Save Project</strong>. </li>
<li>In the <strong>File name</strong> text box, type <strong>c:\winnt\system32\inetsrv\asp\cmpnts\Finance</strong>. (If you did not accept the default installation directory, substitute the name of your installation directory for \winnt\system32.)</li>
<li>Click the <strong>Save</strong> button. <br>
</li>
<li>Click the <strong>Save</strong> button to save Module1. <br>
</li>
<li>Double-click the value <strong>Project1</strong> in the <strong>File name</strong> text box to select it. </li>
<li>Type the name <strong>Finance</strong> for the Project file (.vbp). </li>
<li>Click the <strong>Save</strong> button to save the project .</li>
</ol>
<h3><A NAME="MaketheComponentanIn-ProcessComponent">Make the Component an In-Process Component</A></h3>
<p>Visual Basic allows you to create in-process ActiveX components (OLE Automation Servers) and out-of-process ActiveX components. An <em>in-process</em> ActiveX component is a dynamic-link library (file name extension .dll that is loaded by the calling process. An <em>out-of-process</em> 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.</p>
<p>To make the Finance server component an in-process ActiveX component</p>
<ol>
<li>Open the <strong>File</strong> menu.</li>
<li>Select <strong>Make OLE DLL File</strong>. </li>
<li>Click the <strong>Options</strong> button. </li>
<li>Select the <strong>Auto Increment</strong> check box. </li>
<li>Click <strong>OK</strong>. </li>
<li>Type <strong>c:\winnt\system32\inetsrv\asp\cmpnts\Finance.dll</strong> in the File name text box and click <Strong>OK</Strong>. (If you did not accept the default installation directory, substitute the name of your installation directory for \winnt\system32.)</li>
<li> Exit Visual Basic.</li>
</ol>
<h3><A NAME="RegistertheFinanceServerComponent">Register the Finance Server Component</A></h3>
<p>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. </p>
<ol>
<li>Click the <strong>Start</strong> button on the taskbar. </li>
<li>Select <strong>Run</strong>.</li>
<li>Type <strong>command</strong>. </li>
<li>Press the <font size=2><strong>ENTER</strong></font> key. </li>
<li>Type <strong>cd \winnt\system32\inetsrv\asp\cmpnts</strong> at the command prompt. (If you did not accept the default installation directory, substitute the name of your installation directory for \winnt\system32.)</li>
<li>Press the <font size=2><strong>ENTER</strong></font> key. </li>
<li>Press the <font size=2><strong>ENTER</strong></font> key. </li>
<li>Click the <strong>OK</strong> button when a dialog box appears that says <strong>DllRegisterServer in finance.dll succeeded</strong>. </li>
<li> Close the command prompt window.</li>
</ol>
<!--
<H3><A Name="finsamp">Use</a> this form to determine the future value of a savings plan.</H3>
-->
<hr>
<h2> <a name="CallingtheFinancecomponentfromascript">Lesson 2: Calling the Finance Component From a Script</a></h2>
<p>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.</p>
<p>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. </p>
<h3><A NAME="TheHTMLform">The HTML form</A></h3>
<p>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 <strong>Request</strong> 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 <FONT FACE="COURIER"><strong><code>Request("APR")</code></strong></FONT>. The HTML tag <FONT FACE="COURIER"><code><INPUT TYPE=TEXT NAME=APR></code></FONT> provides
the input field necessary to enter a value. </p>
<p>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 <FONT FACE="COURIER"><code>ACTION</code></FONT> property of the HTML form tag. The HTML tag for the Submit button (<FONT FACE="COURIER"><code><INPUT
TYPE=SUBMIT VALUE=" Calculate Future Value "></code></FONT>) uses the value for <FONT FACE="COURIER"><code>ACTION</code></FONT> from the HTML form tag (<FONT FACE="COURIER"><code><FORM METHOD=POST
ACTION="Finance.asp"></code></FONT>) to call the ASP page Finance.asp.</p>
<p>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).</p>
<h3><A NAME="Thescript">The script</A></h3>
<p>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 <strong>IsNumeric</strong> function is used to test whether or not a numeric (valid)
value was entered for each of the text inputs on the HTML form. </p>
<p><strong>Server.CreateObject</strong> is used to create an instance of (that is, make usable) your Finance component named <FONT FACE="COURIER"><code>MS.Finance</code></FONT>. Once an instance
of a server component is created, you can make use of its methods and properties. On the script line immediately following
<strong>Server.CreateObject</strong>, the method <strong>CalcFV</strong> 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.</p>
<p>To view the script, use a text editor to open the file Finance.asp in the Lessons directory (\Inetpub\Aspsamp\Tutorial\Lessons by default).</p>
<h3><A NAME="Usingyourbrowsertorunthetest">Using your browser to run the test</A></h3>
<p>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. </p>
<ol>
<li>Open Finance.htm by pointing your browser to <a href="http://localhost/aspsamp/tutorial/lessons/finance.htm">http://localhost/aspsamp/tutorial/lessons/finance.htm</a>.</li>
<li>Optionally, enter values for the form inputs in the <strong>Savings Plan</strong> form. <br><br></li>
<li>Click the <strong>Calculate Future Value</strong> button. <p>The value of your savings plan should appear. </p></li>
</ol>
<p>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. </p><hr>