How to modify the DatabaseName stored with a DADE Query
Currently when DADE is used to create dataviews, the DatabaseName is stored as part of the query definition. (This is consistent with Delphi’s approach to specifying a database connection for a Query object).
In some cases, you may decide that the DatabaseName needs to be modified at a later date.
We recommend that the DatabaseName refer to a TDatabase connection component or to an Alias to provide flexibility for changing the connection parameters.
A second way to handle this issue is to implement some code that specifies a DatabaseName whenever a template is loaded. This can be accomplished by using the Report.Template.OnLoadEnd event.
- Declare an event-handler procedure in the private section of your form declaration.
123456typemyForm = class(TForm)privateprocedure ReportTemplateLoadEndEvent(Sender: TObject);publicend;
- Use the FormCreate event to assign the event-handler to the event property.
12345procedure myForm.FormCreate(Sender: TObject)beginReport1.Template.OnLoadEnd := ReportTemplateLoadEndEvent;end;
- Add code to the event-handler to specify the database name.
1234567891011121314procedure myForm.ReportTemplateLoadEndEvent(Sender: TObject)varlSQL: TdaSQL;beginif GetSQLObject(Report1, lSQL) thenbeginlSQL.DatabaseName := Designer.DataSettings.DatabaseName;SetSQLObject(Report, lSQL);lSQL.Free;end;end;
Below is a tech tip for extracting the SQL object from a report. TdaSQL is a class defined in daSQL.pas.
The example code extracts only DataViews[0]. If the report contains multiple dataviews, you will need to iterate thru all of the entries in the DataViews[] array.
How to access the SQL object associated with a Report created using DADE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
uses daDatMod; function GetSQLObject(aReport: TppReport; var aSQL: TdaSQL): Boolean; var lDataModule: TdaDataModule; lDataView: TdaDataView; begin aSQL := TdaSQL.Create(nil); {get the datamodule} lDataModule := daGetDataModule(aReport); if (lDataModule <> nil) then begin lDataView := lDataModule.DataViews[0]; if (lDataView <> nil) and (lDataView is TdaQueryDataView) then aSQL.Assign(TdaQueryDataView(lDataView).SQL); end; Result := (aSQL <> nil); end; procedure SetSQLObject(aReport: TppReport; aSQL: TdaSQL); var lDataModule: TdaDataModule; lDataView: TdaDataView; begin {get the datamodule} lDataModule := daGetDataModule(aReport); if (lDataModule <> nil) then begin lDataView := lDataModule.DataViews[0]; if (lDataView <> nil) and (lDataView is TdaQueryDataView) then TdaQueryDataView(lDataView).SQL := aSQL; end; end; |