793.TXT ===================================================================== Using nested RUN procedures with a PAGEMODE Report ===================================================================== PRODUCT: R:BASE VERSION: 4.5++ or Higher ===================================================================== CATALOG: Forms, Reports & Labels AREA : Reports ===================================================================== You can significantly reduce the amount of code in PAGEMODE reports by using nested command files to print header and footer sections. Use the nested command file to print page headers and footer, and break headers and footers. Since these sections are printed several times throughout a report, the code is repeated for each case where a header or footer should be printed. The end result can be a very long command file. Using nested files to print the headers and footers has these advantages: Code duplication is dramatically reduced leading to fewer errors and simpler code. Your PAGEMODE report is easier to troubleshoot. You know that the same code is being executed each time a header or footer is printed. Syntax and typing errors are reduced. Changes to the layout of the header or footer section are easier to make since you make the change in only one place. You can pass parameters to your header or footer command files using the same layout but different text. Write each of your header and footer sections as a separate command file. Each time the section in question is printed, a RUN command is used instead of including the same block of code in many places in the program. Here's an example. The report prints detail sales amounts and totals by customer. The PAGEMODE report prints each customer's data on a separate page including the page number and date, general customer information, detail rows, and a total sales amount. A page header, break header, and break footer sections are needed in addition to the detail. The page header contains the date and the page number. The break header contains the customer ID number, company name, and contact name. The break footer contains the total sales amount. Separate command files are run for both the page header and break header because there are four different places in the PAGEMODE report where a new page might be required; both a page header and break header are printed at the beginning of each new page. Each time the new page check is true, it is a simple procedure to reset page checking variables and issue the needed RUN commands. The page number and customer number are passed to the header file using percent variables. For each new page, the page number is incremented using the expression SET VAR VPage = (.VPage + 1). The resulting value is then passed as a parameter to the page header file. For example: RUN PGHEAD.RMD USING .VPage. In the page header file, pghead.rmd, the expression VP_num =.%1 captures the passed parameter value; the percent variable %1 holds the value from the VPage variable. The same technique is used to pass the customer ID number, Custno, to the break header file. Custno is used to retrieve the other customer information. Since Custno was fetched in the PAGEMODE report, it is simple to pass that value to the break header file, break1.rmd, using parameters. The following example passes just one or two values each time to the nested header files. Up to nine values can be passed to a nested file. In addition to the page number, for example, a text string can be passed for dynamic header and footer titles or to use the same header file across many reports. For more information about passing parameters, refer to the article "Working With Variables" in the September/October 1993 Exchange, document #738 on the FAX server. Sample report output: This is the Page Header for my report 10/20/94 Page: 1 Cust#: 101 Company: Computer Distributors Inc. Amount: Timothy Johnson $176,000.00 $87,500.00 $22,500.00 $29,000.00 $30,000.00 TOTAL: $345,000.00 This is the Page Header for my report 10/20/94 Page: 2 Cust#: 104 Company: Industrial Concepts Inc. Amount: Jillian Bailey, President $19,500.00 $56,250.00 $95,500.00 TOTAL: $171,250.00 *(PGHEAD.RMD -- prints a page header ) SET VAR VP_Num INTEGER = .%1 SET VAR VPg TEXT = ('Page: ' + (CTXT(.VP_Num))) WRITE 'This is the Page Header for my report' AT 2, 20 WRITE .#DATE AT 3, 35 WRITE .VPg AT 4, 35 RETURN *(BREAK1.RMD -- prints a break header ) SET VAR VCustno INTEGER = .%1 SET VAR vRow INTEGER = .%2 SELECT company, firstname, lastname, title + INTO VCompany ivc, VFirst ivf, VLast ivl, VTitle ivt + FROM Cust WHERE Custno = .VCustno IF VTitle IS NULL THEN SET VAR VWname = (.VFirst & .VLast) ELSE SET VAR VWname = (.VFirst & .VLast + ',' & .VTitle) ENDIF WRITE 'Cust#:', .VCustno AT .vRow, 8 WRITE 'Company:', .VCompany AT .vRow, 24 WRITE 'Amount:' AT .vRow, 59 SET VAR vRow = (.vRow + 1) WRITE .VWname AT .vRow, 33 SET VAR vRow = (.vRow - 1) RETURN *(REPORT.RMD - the PAGEMODE report that calls the header files) SET LINES 20 SET PAGEMODE ON SET MESSAGE OFF SET ERROR MESSAGE OFF CLS SET VAR VLines TEXT = (CVAL('LINES')) SET VAR vRow INTEGER SET VAR VPage INTEGER = 1 SET VAR VCustno INTEGER SET VAR VSum CURRENCY = NULL DECLARE C1 CURSOR FOR SELECT DISTINCT Custno FROM Sales + ORDER BY Custno DECLARE C2 CURSOR FOR SELECT amount FROM Sales + WHERE Custno = .VCustno OPEN C1 FETCH C1 INTO VCustno ivc SET VAR vRow = 6 OUTPUT REP.DAT RUN PGHEAD.RMD USING .VPage WHILE SQLCODE <> 100 THEN RUN BREAK1.RMD USING .VCustno, .vRow OPEN C2 RESET FETCH C2 INTO VAmount iva SET VAR vRow = (.vRow + 1) WHILE SQLCODE <> 100 THEN SET VAR vRow = (.vRow + 1) IF vRow >= (INT(.VLines)) THEN NEWPAGE SET VAR VPage = (.VPage + 1) SET VAR vRow = 6 RUN PGHEAD.RMD USING .VPage RUN BREAK1.RMD USING .VCustno, .vRow SET VAR vRow = (.vRow + 2) ENDIF WRITE .VAmount AT .vRow 54 USING '$999,999.00' SET VAR VSum = (.VSum + .VAmount) FETCH C2 INTO VAmount iva ENDWHILE SET VAR vRow = (.vRow + 1) IF vRow >= (INT(.VLines)) THEN NEWPAGE SET VAR VPage = (.VPage + 1) SET VAR vRow = 6 RUN PGHEAD.RMD USING .VPage RUN BREAK1.RMD USING .VCustno, .vRow ENDIF SET VAR vRow = (.vRow + 1) WRITE 'TOTAL:' AT .vRow, 43 WRITE .VSum AT .vRow, 54 USING '$999,999.00 SET VAR VSum = NULL CLOSE C2 FETCH C1 INTO VCustno ivc IF SQLCODE = 100 THEN BREAK ELSE SET VAR VPage = (.VPage + 1) SET VAR vRow = 6 NEWPAGE RUN PGHEAD.RMD USING .VPage ENDIF ENDWHILE OUTPUT SCREEN CLEAR ALL VAR DROP CURSOR C1 DROP CURSOR C2 SET PAGEMODE OFF SET LINES 20 RETURN Store the passed page number value. Write the page header. Store the passed parameter values. Get the company, first and last name. If there is no title, just print the name Write the break header. Set up the report environment. Initialize variables. Define the breakpoint and detail data through related cursors. Get the first break group value. Run the page header file and write the page header on the first page. Run the break header file to write the first break header. Get the detail data for the break value. Check to see if the report has used all printable lines and needs a new page. If so, run the page header and break header files. Write the detail data and calculate the sum. Break footer processing. Check to see if footer data fits on the page. Get the next break group. If data is found, force a new page and print the page header Close the report and reset the environment