Home General
New Blog Posts: Merging Reports - Part 1 and Part 2

Refresing data for a report

edited April 2007 in General
I place the following code in the AfterPrint method of all reports to
ensure that the report queries are closed after the report is run.

Otherwise, changes to the DB are not being reflected in the report next
time it is run.

The TppDBPipeline properties "OpenDatasource" & "CloseDatasource" are both
set to TRUE.

I suspect there is a better way. What should I be doing to avoid this?

Thank you for your assistance.

Richard Harding . . . .


procedure TfmReports.ppReport1AfterPrint(Sender: TObject);
var
i: integer;
aReportList: TStringList;
aReport: TppReport;
lDBPipeline: TppDBPipeline;

begin
aReport := TppReport(Sender);
if (aReport.DataPipeline is TppDBPipeline) then
begin
lDBPipeline := TppDBPipeline(aReport.DataPipeline);
if (lDBPipeline.DataSource.Dataset is TQuery) then
begin
lDBPipeline.DataSource.DataSet.Close;
end;
end;

aReportList := TStringList.Create;
try
aReport.GetSubReports(aReportList);
for i := 0 to aReportList.Count-1 do
begin
if
TppDBPipeline(TppCustomReport(aReportList.Objects[i]).DataPipeline).DataSource.DataSet
is TQuery then
begin

TppDBPipeline(TppCustomReport(aReportList.Objects[i]).DataPipeline).DataSource.DataSet.Close;
end;
end;
finally
FreeAndNil(aReportList);
end;
end;



--- posted by geoForum on http://delphi.newswhat.com

Comments

  • edited April 2007
    Hi Richard,

    Which database and connectivity are you using? Also, which version of
    ReportBuilder and Delphi are you using? Are you using DADE to access your
    data? In my quick testing with ReportBuilder 10.06, Delphi 2007, and
    Paradox queries, I was able to successfully run a report, close it, change
    the SQL, and re-run the report without having to manually close the
    datasets.

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited April 2007
    Thanks Nico,

    I am using report Builder 7.04 with Delphi 5 and Report Builder 10.05 with
    Delphi 2006.

    I receive the same results with both versions of Report Builder and with
    PARADOX, DBISAM and ElevateDB databases.

    I am not using DADE.

    I can create the situation by:
    . placing a DBGRID, Query, DataSource, ppReport and ppDBPipeline
    components on a form.
    . Using the DBDEMOS PARADOX database, set the SQL of the query to SELECT *
    FROM COUNTRY.
    . Run the report to display the country details.
    . Update a row in the Grid.
    . Re-run the report.
    . The report does not show the updated details unless the query is
    explicitly closed before the report is run.

    The same thing happen to all reports that are based on queries.

    Richard Harding



    --- posted by geoForum on http://delphi.newswhat.com
  • edited April 2007
    Hi Richard,

    I created the exact example you described below and am still able to see the
    changes I made to the Country table. You can download my test example
    below. Let me know how your setup differs.

    http://www.digital-metaphors.com/tips/UpdateDataTest.zip

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited April 2007
    Thank you for example. Unfortunately I did not describe the situation
    correctly.

    . As well as placing a DBGRID, Query, DataSource, ppReport and
    ppDBPipeline components on a form, I also had a Table component.
    . The DBGRID is connected to the COUNTRY Table with the report connected
    to the QUERY - SELECT * FROM COUNTRY.
    . I am using the AFTERPRINT method to close all the queries associated
    with the report and subreports to ensure that data is refreshed after
    updating the tables. This is somewhat clumsy and am seeking an alternative.

    I have just tried the following which is simpler and does not rely on
    event handlers.

    . Place the ppReport and ppDBPipeline components on a separate form -
    REPORT form.
    . When a command is received to print a report, create the REPORT form,
    print the report, close the REPORT form.
    . By setting the CloseDataSource property of all the ppDBPipelines to
    TRUE, the query is closed after the REPORT form is closed and the report
    now has the updated data when next run.

    Is this the "best" way of achieving the desired outcome or is there
    another way?

    Richard Harding . . .



    --- posted by geoForum on http://delphi.newswhat.com
  • edited April 2007
    Hi Richard,

    Thanks for the clairification. In this case you will need to close the
    dataset connected to the report before changes to the data will be
    refreshed. Has using the AfterPrint event caused problems with your
    application? Your second idea sounds like it should work as well however I
    do not see the need to change you app if it already functions correctly.
    You could also search the form for TQuery objects and close them all after
    the report prints if you do not want to access the queries through
    typecasting the report and pipeline object as you have done already.

    --
    Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited April 2007
    Thanks, Nico . . .

    Using the Afterprint does cause a problem when distributing reports as
    .RTM files. I do sometimes forget to assign the AfterPrint event which is
    inconvenient.

    By removing the responiblity of closing the queries from the report does
    make it easier to distribute new reports.

    I do appreciate the level of support that you provide.


    --
    Richard Harding
    Windella Computer Knowhow
    28 Freeman Drive
    Lochinvar NSW 2321
    Phone: 61 2 4930 7336
    Mobile: 0419 016 032
    email: rharding@wck.com.au



    --- posted by geoForum on http://delphi.newswhat.com
This discussion has been closed.