Copy Autosearch values to Subreport

edited April 2014 in Subreports
I have a report that has one sub report. The main report and the sub report
each have their own table with search criteria.
When the report runs I populate the search criteria on the main report but
would like to copy this search criteria to the sub report when the sub
report runs.

I have tried using GetAutoSearchValues with the following code:

lDataModule := daGetDataModule(ppReport2);

for liIndex := 1 to lDataModule.DataViewCount-1 do
lQueryDataView := TdaQueryDataView(lDataModule.DataViews[liIndex]);

lSQL := lQueryDataView.SQL;

lSQL.Criteria[0].Value :=
lSQL.Criteria[1].Value :=
lSQL.Criteria[2].Value :=
lSQL.Criteria[3].Value :=



Everything runs fine but the sub report does not seem to be using the search
criteria of the main report.

I am running RB Pro version 15.01



  • edited April 2014
    Hi Phil,

    Take a look at the following article/example on copying the same
    autosearch criteria to multiple queries. You would want to do something
    similar in your case as well.

    I also recommend upgrading your version of RB to the latest (15.04) to
    stay up to date with all bug fixes and enhancements.

    Best Regards,

    Nico Cizik
    Digital Metaphors
  • edited April 2014
    That article did not seem to help me much. I could not get the example to
    run or the code example to work with my code.
    I have also tried to copy the auto search values using the following:

    procedure TIWUserSession.ConfigureSubReportSearchCriteria;
    lSubReportSQL: TdaSQL;
    If GetSQLObject(ppSubReport2.Report, lSubReportSQL) Then
    lSubReportSQL.Criteria[0].Value :=
    lSubReportSQL.Criteria[1].Value :=
    lSubReportSQL.Criteria[2].Value :=
    lSubReportSQL.Criteria[3].Value :=

    function TIWUserSession.GetSQLObject(aReport: TppCustomReport; var aSQL:
    TdaSQL): Boolean;
    liIndex: Integer;
    lDataModule: TdaDataModule;
    lDataView: TdaDataView;
    aSQL := nil;
    {get the datamodule}
    lDataModule := daGetDataModule(aReport.MainReport);
    if (lDataModule <> nil) then
    for liIndex := 0 to lDatamodule.DataViewCount - 1 do
    lDataView := lDataModule.DataViews[liIndex];
    if (lDataView <> nil) and (lDataView is TdaADOQueryDataView) and
    (lDataview.DataPipelines[0] = aReport.DataPipeline) then
    aSQL := TdaADOQueryDataView(lDataView).SQL;
    Result := (aSQL <> nil);

    As with my previous attempt it seems to apply the search expression to the
    sub report but when the report generates it's pulling in all the data not
    what is defined by the search criteria.
  • edited April 2014
    Hi Phil,

    I apologize, the example in the article uses RAP. Below is a link to a
    version that does not use RAP. I tested it and it functions correctly
    copying the search criteria from one query to another.

    I highly suggest using the TdaSQLBuilder rather than the TdaSQL object.
    The SQLBuilder was designed as an easy-to-use wrapper around the SQL
    object for performing tasks such as this one.

    Though your code may function, you are assigning the values too late for
    them to have any effect. As in the example, the autosearch criteria
    needs to be set before the pipelines are opened.

    Best Regards,

    Nico Cizik
    Digital Metaphors
  • edited April 2014
    Another (much easier) option would be to use Report Parameters to keep
    your autosearch criteria synchronized across multiple datasets.

    Best Regards,

    Nico Cizik
    Digital Metaphors
  • edited April 2014
    Thanks for the new example. This one helped point me in the right direction
    and it looks like I have things working now.
This discussion has been closed.