Top
PowerCOBOL V11.0 User's Guide
FUJITSU Software

9.7.4 Using the *COM Class to access Microsoft ADO

The following example shows a PowerCOBOL application using the *COM class to access Microsoft's ADO (Active Data Objects).

It uses an ADO Connection object to establish a connection to the database, and uses an ADO Recordset object to read records back from the database and place them into a ListBox control on the form.

You must remember when using the *COM class to define it in the form's REPOSITORY by right clicking on the form, selecting Edit ENVIRONMENT DIVISION, and then clicking on REPOSITORY. Then place the following line of code in the REPOSITORY section and save it:

CLASS COM AS "*COM".

In one of the form's event procedures, the following code will open an Access data base, read the records and add them to a ListBox control named LST-DISPLAY:

 ENVIRONMENT     DIVISION.
 DATA            DIVISION.
 WORKING-STORAGE SECTION.
 01 OBJ-CONNECTION    OBJECT REFERENCE COM.
 01 OBJ-RECORDSET     OBJECT REFERENCE COM.
 01 OBJ-FIELDS        OBJECT REFERENCE COM.
 01 OBJ-FIELD         OBJECT REFERENCE COM.
 01 PROGID-CONNECTION PIC X(8192) VALUE "ADODB.Connection".
 01 PROGID-RECORDSET  PIC X(8192) VALUE "ADODB.Recordset".
 01 NAME-PROVIDER     PIC X(8192) VALUE "Microsoft.Jet.OLEDB.4.0".
 01 NAME-DB           PIC X(8192) VALUE "c:\sample.mdb".
 01 NAME-TABLE        PIC X(8192) VALUE "Employee".
 01 NAME-FIELD        PIC X(8192) VALUE "Name".
 01 WK-VAL            PIC X(8192).
 01 WK-0              PIC S9(9) COMP-5 VALUE 0.
 01 WK-1              PIC S9(9) COMP-5 VALUE 1.
 01 IS-EOF            PIC S9(4) COMP-5.

 PROCEDURE       DIVISION.
* Create ADO Connection object and Connect to DB
     invoke COM "CREATE-OBJECT" using PROGID-CONNECTION 
                                returning OBJ-CONNECTION.
     invoke OBJ-CONNECTION "SET-Provider" using NAME-PROVIDER.
     invoke OBJ-CONNECTION "Open" using NAME-DB.

* Create ADO Recordset object and associate with "Employee" table
     invoke COM "CREATE-OBJECT" using PROGID-RECORDSET 
                                returning OBJ-RECORDSET.
     invoke OBJ-RECORDSET "Open" using NAME-TABLE 
                                       OBJ-CONNECTION 
                                       WK-0 
                                       WK-1 
                                       WK-0.

* Copy "Name" field of "Employee" table to ListBox
     invoke OBJ-RECORDSET "GET-EOF" returning IS-EOF.
     perform with test before until IS-EOF not = 0
         invoke OBJ-RECORDSET "GET-Fields" returning OBJ-FIELDS
         invoke OBJ-FIELDS "GET-Item" using NAME-FIELD 
                                      returning OBJ-FIELD
         invoke OBJ-FIELD "GET-Value" returning WK-VAL
         invoke LST-DISPLAY "AddString" using WK-VAL
         set OBJ-FIELD to Null
         set OBJ-FIELDS to Null
         invoke OBJ-RECORDSET "MoveNext"
         invoke OBJ-RECORDSET "GET-EOF" returning IS-EOF
     end-perform.

* Clean up
     invoke OBJ-RECORDSET "Close".
     set OBJ-RECORDSET to Null.
     invoke OBJ-CONNECTION "Close".
     set OBJ-CONNECTION to Null.

Note that PowerCOBOL ships with an ADO Data control to ease ADO development. This means that you do not need to use the *COM approach shown above to access ADO, but this example has been left in this manual to illustrate one of the more complex examples of using the *COM class.