Are the complexities of printing AS/400 data driving you insane? Are you finding it difficult to change fonts in the middle of a line or draw a border around the page? Do you look with envy at the people who can format reports on a PC quickly and easily?
Well, now it's your turn. Using a PC word processor, you can create dazzling reports from your AS/400 data automatically, and we'll show you how.
For this article, we'll focus on some techniques that not only allow you to accomplish this feat, but also teach you some important concepts that you can use in the future. The techniques we'll use are a virtual alphabet soup of acronyms: OLE, ODBC, and VB (Object Linking and Embedding, Open Database Connectivity, and Visual Basic, respectively). Since we've covered client/server VB programming and ODBC in the past (see "Custom Controls: The Muscle Behind Visual Basic," MC, December, 1994 and "ODBC Overview," MC, February 1995), the main focus will be on the concepts of OLE (pronounced "oh-LAY") and OLE automation (OA). Microsoft Word will be used to demonstrate these concepts, but they should apply to any OLE-enabled application.
To demonstrate the concepts of OLE and OA, we will use VB to gather and format AS/400 data and pass it on to the sample purchase order template that comes with Word. You could really impress your boss by modifying this template to add your company's information and artwork. Before we show you the nuts and bolts of the code, let's explore some of the concepts of OLE and how it has evolved.
The Oil of OLE
In OLE's original incarnation, Version 1.0, its primary purpose was to allow data (objects) from one source to be either linked to or embedded within data from another source. The most common use for OLE was to create compound documents; that is, documents created from several different programs.
The advantage of compound documents is the ability to use the appropriate tool for the job. For instance, to include an illustration in a document, you could create the illustration in your favorite drawing program and link it to or embed it in your word processing document.
Today, OLE has migrated from a compound document strategy to a more ambitious standard for defining the way that programs and data can interact with one another. The OLE 2.0 standard enhanced the original functionality of OLE 1.0 for both users and developers. For users, it made working with compound documents easier and more seamless. For developers, it defined a standard (OA) for interapplication communication, making it easier for applications to control each other and share data.
OLE Automation
Many OLE-enabled applications can control each other through OA. When one application is controlling another, the application being controlled is the server; the application doing the controlling is the client (these terms shouldn't be confused with general client/server terminology). The following is a list of just a few of the applications that are OLE-enabled:
* Microsoft's Excel
* Visio's Visio
* Novell's WordPerfect
* Pinnacle Publishing's Graphics Server
OLE objects can be thought of as a unit of work for a particular program. OA server applications expose objects for the client programs to manipulate. When an object is exposed, it is effectively "published" for other applications to use. For example, a word processor might expose a document object, a spreadsheet might expose a cell or range of cells, or a drawing program might expose the drawing and the figures that make up the drawing. Only the objects that are exposed by a server program are available to OLE clients.
OLE servers also expose methods and properties that allow the client to manipulate objects. For instance, if a spreadsheet application exposed a cell object, an example of a property might be the alignment of the text within the cell?left, right, or center. A method might be clear, which would clear the contents of the cell. To get a listing of the objects a particular application exposes and the methods and properties for those objects, consult the application's manuals.
Word currently only exposes the Basic object, which is the WordBasic command language. This means that, in order to program Word using OA, you must know a little about WordBasic. In contrast, Visio, an OLE-enabled drawing package by Visio Inc., exposes its documents and the elements of those documents and allows you to manipulate them directly by properties and methods. This is a subtle difference, but it makes Visio easier to control and more object-oriented. Future versions of Word should be better OA servers.
Know the Code
Word comes with many sample templates that can be used as a basis for creating your own documents. One of these templates is a company purchase order (PURCHORD.DOT). We will use this as the foundation for our example.
1 shows the VB code that was used to generate the purchase order in 2. This code is not complete and is meant to serve as a framework around which you could build a working program. Sample database names were used for this example. To make it a complete working project, you will have to substitute your database files and fields.
Figure 1 shows the VB code that was used to generate the purchase order in Figure 2. This code is not complete and is meant to serve as a framework around which you could build a working program. Sample database names were used for this example. To make it a complete working project, you will have to substitute your database files and fields.
The code at Label A retrieves the data from the AS/400 database using ODBC. Two snapshots are used (a snapshot is a nonupdateable reference to a set of records in a database). The first snapshot retrieves vendor information from the vendor master file. The second snapshot retrieves the purchase order line items. This data is then used to fill in Word form fields on the purchase order template.
The code at Label B establishes an OA session with Word. The CREATEOBJECT command creates an instance of the object specified in its parameter. (Some of the terminology used here is discussed in "Object-Oriented Programming Concepts," MC, September 1994.) The parameter to the CREATEOBJECT command is a combination that specifies the application, the type of object to be created (class), and, optionally, the version of the application. For instance, to specify a Word 6.0 Basic class, the class parameter would be Word.Basic.6.
After the OA session is established, we can begin issuing commands to the word processor. In this case, the primary command we are using is the SETFORMRESULT WordBasic command (see Label C). This command sets the text of the specified form field. This command is used to fill in each field on the form. The FILEPRINTPREVIEW command (see Label D) is used to display the document as it will look when it is printed.
Caf? OLE
One problem with the current generation of OA applications is the large amount of system resources they require. For instance, when the program in 1 is run, both the program and Word must be loaded into memory at the same time. This shouldn't be a problem, but if you try to do complex applications with multiple large OLE servers, you could find yourself running out of resources. OA servers don't necessarily use a large amount of resource; much depends upon the OA tools that you use in your application.
One problem with the current generation of OA applications is the large amount of system resources they require. For instance, when the program in Figure 1 is run, both the program and Word must be loaded into memory at the same time. This shouldn't be a problem, but if you try to do complex applications with multiple large OLE servers, you could find yourself running out of resources. OA servers don't necessarily use a large amount of resource; much depends upon the OA tools that you use in your application.
Another nuisance of programming OA with Visual Basic 3.0 is VB's lack of support for named arguments. VB supports only positional arguments, so if you have a command with many parameters, you have to get them in the right order and in the correct position. For example, to turn off background printing in Word, you have to issue the command
wp.ToolsOptionsPrint , , , , , , , , ,0
The zero, for off, is the tenth parameter to this command. Future versions of VB should be able to handle named arguments, so the command would be similar to
wp.ToolsOptionsPrint.Background=0
A Word About OCXs
OLE controls (OCXs) are an extension of OA servers. They are aimed at being a replacement for VBXs, which are closely tied to VB. OLE controls are component objects that can be easily used in many different development tools. The idea behind OLE controls is the same as that for VBXs?create objects or components that can be reused to speed the development process. OLE controls are currently only supported in a limited number of applications, but that number is certain to grow, as is the number of OCX controls on the market.
Is OLE in Your Future?
OLE and OA are cornerstone strategies in Microsoft's vision of the future of computing. In fact, the company is relying upon the technology in its forthcoming object-oriented version of Windows NT, code-named Cairo. Other major vendors are incorporating OLE into their development plans as well. One thing is for sure, component-based software development is here to stay, and since OLE and OA are viable, working architectures for the component-based software model, they'll be around for quite some time.
Brian Singleton is an AS/400 client-server programmer/analyst. He can be reached through Midrange Computing or Internet E-mail at
Using OLE with AS/400 Data
Figure 1: Visual Basic Code Sample to Generate Purchase Order
Sub Command1_Click () Dim wp As object ' Allocate an object pointer Dim db As database ' Pointer to AS/400 database Dim ssVendorMaster As snapshot ' Vendor master table Dim ssPurchases As snapshot ' Purchase order table '-------------------------------------- ' First get the data from the AS/400 '-------------------------------------- Set db = OpenDatabase("", False, False, "ODBC;")' Point to AS/400 database Set ssVendorMaster = db.CreateSnapshot("Select * from VndMstr where VndNbr=15", 64) ' Use passthrough for speed Set ssPurchases = db.CreateSnapshot("Select * from Purchord where PoNbr=12345", 64) '-------------------------------------- ' Then open Word for OLE Automation '-------------------------------------- Set wp = CreateObject("Word.basic") '-------------------------------------- ' Note that everything after the "wp." is ' WordBasic syntax. ' Create a new document based ' on the "purchord" template '-------------------------------------- wp.FileNew "purchord" '-------------------------------------- ' Now we will assign the data retrieved from ' the AS/400 to our Word form fields using ' the WordBasic command "SetFormResult" '-------------------------------------- wp.SetFormResult "PO_Number", ssPurchases("PoNbr") wp.SetFormResult "Quantity_1", ssPurchases("Qty") wp.SetFormResult "Description_1", ssPurchases("Desc") wp.SetFormResult "Price_1", ssPurchases("Price") wp.SetFormResult "Unit_1", ssPurchases("Unit") wp.SetFormResult "Requisitioner", ssPurchases("Reqstr") wp.SetFormResult "SalesTax", ssPurchases("STax") wp.SetFormResult "Ship", ssPurchases("Ship") wp.SetFormResult "To_1", ssVendorMaster("TName") wp.SetFormResult "To_2", ssVendorMaster("TAddr1") wp.SetFormResult "To_3", ssVendorMaster("TAddr2") wp.SetFormResult "To_4", ssVendorMaster("TCSZ") wp.SetFormResult "Ship_1", ssVendorMaster("SName") wp.SetFormResult "Ship_2", ssVendorMaster("SAddr1") wp.SetFormResult "Ship_3", ssVendorMaster("SAddr2") wp.SetFormResult "Ship_4", ssVendorMaster("SCSZ") wp.SetFormResult "Terms", ssVendorMaster("Terms") wp.SetFormResult "Shipping", ssPurchases("Ship") '-------------------------------------- ' Show me '-------------------------------------- wp.FilePrintPreview '-------------------------------------- ' Wait for user input '-------------------------------------- MsgBox "Alt-Tab to the Word document" '-------------------------------------- ' Clear the memory used by Word '-------------------------------------- Set wp = Nothing End Sub
LATEST COMMENTS
MC Press Online