In this chapter, you will learn:
If you have ever needed to display the contents of a database in an application or Web page, you know how much work and code can be involved. ActiveX design-time controls are designed to take care of the implementation details, freeing you to concentrate on your design objectives. Design-time controls are embedded onto Web pages (.HTML or .ASP), and create much of the code for you. Because they are ActiveX, they handle their own drawing, and accept mouse and keyboard input. The developer interacts with them through the context menus and property pages provided by the control. Unlike run-time controls, such as a data-bound grid control or a calendar control, design-time controls are available only at design time, and only in the development environment.
To use design-time controls, they must first be installed on the computer that is being used to run them. Visual InterDev comes bundled with several design-time controls, and automatically installs them for you during setup. These controls include the following:
NOTE: As with run-time controls, you can create your own design-time controls or use ActiveX controls developed by third-party vendors. See Chapter 14, "Visual Basic 5.0, Control Creation Edition," for more information about building and installing ActiveX controls.
The Data Command design-time ActiveX control allows you to create a connection to an external data source and create the command to browse or modify data from that source. As a prerequisite to using the Data Command Control (and the Data Range Control), you must have established a project data connection. For more information, see the sidebar "Creating a Project Data Connection."
To insert a Data Command Control, take the following steps:
Figure15.1.
Inserting a design-time ActiveX control on your Web page.
Figure 15.3.
The Query Designer after running the SQL query entered in Step 9.
Figure 15.4.
The Query Run Icon.
The control will insert the code shown in Listing 15.1 into your .ASP file.
*** End Listing ***
When this script is run on the server, the results of the query will be stored in
the DataCommand1 recordset variable. A recordset is a representation of the data
that is returned by the query. For example, if you insert the code in Listing 15.2
below the data command control, it will loop through and display all of the customers.
*** End Listing ***
TIP: If at any time you need to edit your design-time control, right-click anywhere within the code generated by the control, and select Edit Design-time Control... from the menu. Script generated by design-time controls begins with:
<!--METADATA TYPE="DesignerControl" startspanand ends with:
<!--METADATA TYPE="DesignerControl" endspan-->If you wish to change the script generated by the control, make sure you remove the METATDATA comments, otherwise your changes will be overwritten the next time you open the control.
The Data Range design-time ActiveX control expands on the Data Command Control by adding the code that will be repeated for each record in the recordset. The Data Range Control consists of two separate controls: a header and footer. The header control incorporates the same logic as the Data Command Control, allowing the developer easily to build an SQL query. It also creates the code that will allow the user to navigate forward and backward through the results. The footer simply denotes the end of the data range loop and displays the navigation buttons.
To make the results returned by a Data Range Control presentable, you need to
do a little legwork. By default, the data range header control displays data with
limited formatting. I have included a template below to get you started. You can
create your own .ASP page with your favorite HTML editor, such as Front Page 97.
The complete file is available on the disk
ON THE DISK:
at CHAPTER15/TEMPLATE.ASP.
*** End Listing ***
You can now add a Data Range Control to the .ASP file by taking the following steps:
Figure 15.5.
The Data Range Header Control and its Property pages.
Figure 15.6.
The selected fields from the Customers table.
The Data Range Control creates all the code you need to read the records from the database and page through them. However, it does not automatically create the code to display the records. The Data Range Control provides a mechanism to insert the appropriate fields into your .ASP file manually. Take the following steps:
Figure 15.7.
The Copy Fields dialog box copies script to the Clipboard.
WARNING:
If you get an error message in your page that reads:
<!--
ERROR: This file must be an 'HTML Server-Side Script' file (.ASP).
Please open properties for this file and update the current language setting.
-->You will need to select View, Properties from the menu, and change the Language to HTML Server Side Script. Then reopen the header control, and close it again. The new code should now appear.
*** End Listing ***
To finish your page, insert a data range footer control. The footer denotes the end
of the recordset loop and inserts the navigation buttons at the bottom of your page.
To insert the footer, take the following steps:
Complete the page by selecting File, Save from the menu. When you
view your new page in a browser, the database records will be displayed dynamically.
Your results should look like Figure 15.8.
Figure 15.8.
A sample Web page created with the design-time Data Range Control.
One of the advantages of using the Data Range Control is that it makes only one call
to the database to fill the recordset. So if your query returns a large number of
records, the end user does not have to wait for a new query before viewing a new
page. This can make a big difference if you have a slow connection or a large number
of records in the recordset.
It is important to remember that the Data Range Control and the SQL Query Designer
are capable of returning the results from multiple tables in the same query. I have
included two sample pages that expand on our example from above. CUSTOMERS.ASP returns
a table similar to Figure 15.8 above, only this time the contact names are hyperlinks.
Selecting a name calls ORDERS.ASP with the CustomerID as part of the URL.
ORDERS.ASP queries three tables and returns all orders for that customer. If you
want to see how the queries were generated, you can right-click the Data Range header
control, and press the SQL Builder button on the Property page. To use the pages
in your project, you will need to create a project data connection to the Northwinds
database.
NOTE: The Northwinds database is a sample database that is shipped with Microsoft Access. A copy of this database is included on the CD.
ON THE DISK:
The files are titled CHAPTER15/CUSTOMERS.ASP and CHAPTER15/ORDERS.ASP.
If you have common elements on your Web site, such as headers and footers, you know what a problem it is to update every page when a change is made to one of the elements. Active Server Pages allow you to create one file with the common element, and insert its contents into another file at run-time. This can also be very useful for including script functions and procedures, thus maintaining one version of the code.
Visual InterDev provides an ActiveX design-time control that makes inserting files easy. More importantly, it ensures that the path and filename you are inserting are valid. To insert an Include Control, take the following steps:
Figure 15.9.
The URL Builder that is displayed by the Include Control.
The controls and examples I have shown you so far deal strictly with viewing data. This is fine if all you want to do is view reports. However, in the real world you often need to add or modify records in a database. And if the database is large, you may want to view only those records that meet certain criteria. In the past, building Web-based applications that are bound to one or many data sources was very complex and required a great deal of cgi programming. Today, companies live and work in "Web time," and Web-based applications always seem to be needed yesterday. Separate, stand-alone applications are being replaced by applications that "live" on the Internet or a corporate intranet.
One of the most useful tools included with Visual InterDev is the Data Form Wizard. The Data Form Wizard will help you build a custom application that will allow you to view and modify records, or search for records meeting certain requirements.
To build this example, we will again be using the Northwind Trader's database that ships with Microsoft Access. Take the following steps:
Figure 15.10.
Starting a new Data Form Wizard.
Figure 15.11.
The Data Form Wizard after selecting all of the available fields.
You should now have a fully functional Web application that will allow you to
view and modify the Customer database. Click the File View Pane and you should see
three new pages: CUSTOMERSACTION.ASP, CUSTOMERSFORM.ASP, and CUSTOMERSLIST.ASP.
To see the pages in action, right-click one of the new .ASP pages, and select Preview
in Browser from the menu.
TIP: If you view the results of these pages, you will notice that the tables and list boxes are labeled with the field names from the database. To specify alternative text for fields, click the Advanced button in Step 6 and enter the text of your choice in the Alternative Field Label box.
- Copy file CHAPTER15/NORTHWIND.MDB to a directory on your Web server. If Visual InterDev is running on a machine other than your Web server, make sure this is a shared directory so you can access the file.
ON THE DISK:
NORTHWIND.MDB
- Right-click the GLOBAL.ASA file in the File View Pane.
- Select Add Data Connection from the menu.
- Click New.
- Select Microsoft Access Driver (*.mdb) from the list. Click Next.
- Enter NorthWinds for the name of the data source. This is your Data Source Name, or DSN.
- Click Finish.
- A new dialog box will now appear, titled ODBC Microsoft Access Setup.
- Under Database, press the Select button.
- If Visual InterDev is running on the same machine as your Web server, enter the path that you copied the file NORTHWIND.MDB to in Step 1. If Visual InterDev is running on a machine other than your Web server, you will need to map a network drive, then select the file.
- Click OK.
- NORTHWINDS.DSN should now appear in your list of data sources. Click OK again.
If you open your GLOBAL.ASA file, the following code should be in Session_OnStart procedure:
'==Visual InterDev Generated - DataConnection startspan==
--Project Data Connection
Session("DataConn_ConnectionString") = "DBQ=W:\SAMPLES\NORTHWIND.MDB;DefaultDir=W:\SAMPLES\;Driver={Microsoft Access Driver(*.mdb)};DriverId=25;FIL=MSAccess;ImplicitCommitSync=Yes;MaxBufferSize=512;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"
Session("DataConn_ConnectionTimeout") = 15
Session("DataConn_CommandTimeout") = 30
Session("DataConn_RuntimeUserName") = "admin"
Session("DataConn_RuntimePassword") = ""
'==Visual InterDev Generated - DataConnection endspan==When you have completed the preceding steps, a Data View tab will appear in the project workspace, and a DataConn entry should appear under your GLOBAL.ASA file in the FileVieöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþöþrect for your development machine, it will probably fail for your Web server. To solve this problem, edit the connect string to specify a Universal Naming Code (UNC) path to your database, instead of the mapped drive information. Change the DataConn_ConnectionString variable to read:
"DBQ=\\MyServer\Samples\northwind.mdb;DefaultDir=W:\SAMPLES\;Driver={Microsoft Access Driver(*.mdb)};DriverId=25;FIL=MSAccess;ImplicitCommitSync=Yes;MaxBufferSize=512;MaxScanRows=8;PageTimeout=5;SafeTransactions=0;Threads=3;UID=admin;UserCommitSync=Yes;"
where MyServer is the name of your server.
From Here...
The following chapters will provide you with more information about database access and ActiveX Controls:
- Chapter 17, "Using the Advanced Data Connector," will show you how to use Microsoft's latest technology for creating interactive database applications.
- Chapters 32, "Setting Up a Database," will show you how to create your own databases.
- Chapter 33, "Database Development with Visual InterDev," will show you the database tools that come with Visual Interdev, including the Query Designer.
- Chapter 31, "Using the ActiveX Data Objects" will give you more information about ActiveX Data Objects and how to use them in your applications.