Monday, 31 August 2015

How To create a report which is based on a temporary table?



How To create a report which is based on a temporary table?

Author: Luc Van Dyck

When you normally create a report, it is based on one or more tables.
Sometimes you need to create a temporary table (eg. to combine or sort data) and you want that data to appear in a report. The problem is that you can't use a temporary table directly in the DataItem-property of the report. It only accepts "real" tables.

The solution for this is to use the virtual table Integer instead, and write code to simulate the OnAfterGetRecord-trigger.

OnPreDataItem()
FOR i := 1 TO 80 DO BEGIN
  tmpItem.INIT;
  tmpItem."No." := FORMAT(i);
  tmpItem.Description := 'Item description ' + FORMAT(i);
  tmpItem."Unit Price" := i * 1000;
  tmpItem.INSERT;
END;

tmpItem.RESET;
SETRANGE(Number,1,tmpItem.COUNT);                                            
                                               
OnAfterGetRecord()
IF Number = 1 THEN
  tmpItem.FIND('-')
ELSE
  tmpItem.NEXT;

When defining the layout of your report, you use this Integer-dataitem to place your headers and body's.
You can't use the Field Menu to place fields on your report: you have to type the name of your tmp-variable together with the fieldname (eg. tmpItem.Description).

The result is a normally looking report:

No comments:

Post a Comment