Omnis Technical Note TNPR0004

Report Class Prints Itself

For Omnis Studio version 1.2
By Michael Monschau

How to code report classes which implement their own printing loop.

Contents

The correct way
More than one method
Printing a list
Printing Omnis data
Opening the Job Setup Dialog
Finally

The correct way

There is never just one correct way to do something. Consistency and reusability however are
some of the keys to successful development. Reusability is addressed easily by using inheritance. Consistency can be very difficult to achieve and is not within the scope of this Technical Note.

Our example is based on the idea that the report class we create shall be used as a super class to
all other report classes we develop. Therefore the methods we create for it have to cater for all our development needs. They need to be flexible and we may need more then one.

More than one method

Coding a report which prints itself. I can think of several ways to go about it. Here are two of them.

We can code everything in the $construct of the report class. So the calling code can simply say
'Do myReport.$open()'. However this restricts us to only one way of printing the report, unless we
want the make the method and its parameters very complex and difficult to understand.

My preferred way is to write one or more public methods which print the report. This means when we want to print the report, we will have to make two calls. One to open the report and the second to print
it. Ok it is an extra call, but it is far cleaner and more intuitive.

Do myReportClass.$open('instName') returns repInst
Do repInst.$print()


Printing a list

We shall call our method $printlist. It will have three parameters, a field reference to the list, a boolean
telling as to print all rows or just the selected rows, and another boolean telling us whether to open the
job setup dialog.


##### Method '$printlist' #####
No. Parameter Type Subtype Init.Val/Calc
1 pList Field reference
2 pSelectedOnly Boolean
3 pOpenJobSetup Boolean
 
No. Local Variable Type   Subtype Init.Val/Calc
1 ok Boolean kTrue
2 theRow Item reference  
 
1 ; call method to open job setup if we are asked to
2 If pOpenJobSetup
3   Do method openJobSetup Returns ok
4 End If
5 If ok
6   ; fetch the first row
7   Set reference theRow to pList.$first(pSelectedOnly)
8   While theRow&ok
9     ; first load the crb fields from the list row
10     Do row.$loadcols()
11     ; now print a record
12     Do $cinst.$printrecord() Returns ok
13     ; fetch next row
14     Set reference theRow to pList.$next(theRow,pSelectedOnly)
15   End While
16   ; if everything went well finish off
17   If ok
18     Do $cinst.$endprint() Returns ok  
19   End If  
20 End If  
21 ; if something went wrong close the instance  
22 If not(ok)  
23   Do cinst.$close()  
24 End If  


Printing Omnis data

We shall call our method $printOmnisData(). It will have two parameters, a search string for filtering records, and a boolean telling us whether to open the job setup dialog.

##### Method '$printlist' #####
No. Parameter Type Subtype Init.Val/Calc
1 pSearchString Character 10000
2 pOpenJobSetup Boolean  
 
No. Local Variable Type Subtype Init.Val/Calc
1 ok Boolean   kTrue
         
No Method text
1 ; call method to open job setup if we are asked to
2 If pOpenJobSetup
3   Do method openJobSetup Returns ok
4 End If
5 ; print
6 If ok
7   ; set the main file name from the class which inherits this class
8   Begin reversible block
9     Set read-only files {[$cinst.$class().$mainfile]}
10     Set main file {[$cinst.$class().$mainfile]}
11   End reversible block
12   ; set the search if one has been specified
13   If len(pSearchString)
14     Set search as calculation {evalf(pSearchString)}
15   Else
16     Clear search class
17   End If
18   ; the main printing loop
19   Find first (Use search)
20   While ok&#F
21     Do $cinst.$printrecord() Returns ok
22     Next (Use search)
23   End While
24   ; finish off
25   Do $cinst.$endprint()
26   Clear search class
27 End If
28 ; if something went wrong close the instance
29 If not(ok)
30   Do cinst.$close()
31 End If    

Opening the Job Setup Dialog

We will write a common private method which is responsible for opening the Job Setup dialog, when
so requested. This method will be called by our public methods $printList and $printOmnisData.

##### Method 'openJobSetup' #####
No. Local Variable Type Subtype Init.Val/Calc
1 ok Boolean    
 
No. Method text
1 ; we only want to open the job setup if we are printing
2 ; to the Printer, Memory or Disk (optional)
3 Switch $cdevice.$ident
4   Case kDevPrinter,kDevMemory,kDevDisk
5     ; force the job setup dialog by passing kTrue
6     Do $cinst.$openjobsetup(kTrue) Returns ok
7   Default
8     Calculate ok as kTrue  
9 End Switch
10 Quit method ok


Finally

In order to try our report, create a report class from an Omnis file class for which you have some data. Use the Omnis report wizard from the component store. Once created set the superclass name to the name of

We should now be able to say

Do $clib.$reports.fileReport.$open('instName') Returns theInst
Do theInst.$printOmnisData('',kTrue)
;

Also try entering some search criteria i.e. 'myField=x'.

Download RTF Document

Search Omnis Developer Resources

 

Hit enter to search

X