Redefining Database connection for a report
I have a TppReport that is called using predefined criteria like:
Report.template.DatabaseSettings.Name := 'Docket';
Report.template.LoadFromDatabase;
Report.CreateAutoSearchCriteria('jobsp', 'JobID', soEqual ,
dmATBaseData.QryJobs.FieldByName('JobID').AsString, False);
Report.ShowAutoSearchDialog := False;
Report.Print;
The report is designed during a different session and has the database
connection details and query details stored within. The above code retrieves
the report definition from the database, applies some search criteria and
calls the report print that is setup to be send to the screen.
However, the database connection used is ADO. I need to be able to redefine
the ADO connection used by the report. Is there a way to do this?
I have tried to involve the Designer as it provides a datasettings property
were I can define the databasename like:
ReportItem.Close;
ADOConnection.Close;
ADOConnection.ConnectionString := MyConnectionString;
Designer.DataSettings.DatabaseName := 'ADOConnection';
ADOConnection.Open;
ReportItem.Open;
Designer.Report.template.DatabaseSettings.Name := 'Docket';
Designer.Report.template.LoadFromDatabase;
Designer.Report.CreateAutoSearchCriteria('jobsp', 'JobID', soEqual ,
dmATBaseData.QryJobs.FieldByName('JobID').AsString, False);
Designer.Report.ShowAutoSearchDialog := False;
Designer.Report.Print;
ReportItem.Close;
ADOConnection.Close;
Unfortunately this does not appear to work. At runtime I always get the
Database: 'DefaultADOConnection' login which fails to connect. (Bit strange
that the DatabaseName property is a TFileName but happy to list my
TADOConnection object in the combo)
To be sure I also have set the ADO connection in the designer object stored
in the dfm. Still no success.
What am I doing wrong?
Thanks
Paul van Dinther
Report.template.DatabaseSettings.Name := 'Docket';
Report.template.LoadFromDatabase;
Report.CreateAutoSearchCriteria('jobsp', 'JobID', soEqual ,
dmATBaseData.QryJobs.FieldByName('JobID').AsString, False);
Report.ShowAutoSearchDialog := False;
Report.Print;
The report is designed during a different session and has the database
connection details and query details stored within. The above code retrieves
the report definition from the database, applies some search criteria and
calls the report print that is setup to be send to the screen.
However, the database connection used is ADO. I need to be able to redefine
the ADO connection used by the report. Is there a way to do this?
I have tried to involve the Designer as it provides a datasettings property
were I can define the databasename like:
ReportItem.Close;
ADOConnection.Close;
ADOConnection.ConnectionString := MyConnectionString;
Designer.DataSettings.DatabaseName := 'ADOConnection';
ADOConnection.Open;
ReportItem.Open;
Designer.Report.template.DatabaseSettings.Name := 'Docket';
Designer.Report.template.LoadFromDatabase;
Designer.Report.CreateAutoSearchCriteria('jobsp', 'JobID', soEqual ,
dmATBaseData.QryJobs.FieldByName('JobID').AsString, False);
Designer.Report.ShowAutoSearchDialog := False;
Designer.Report.Print;
ReportItem.Close;
ADOConnection.Close;
Unfortunately this does not appear to work. At runtime I always get the
Database: 'DefaultADOConnection' login which fails to connect. (Bit strange
that the DatabaseName property is a TFileName but happy to list my
TADOConnection object in the combo)
To be sure I also have set the ADO connection in the designer object stored
in the dfm. Still no success.
What am I doing wrong?
Thanks
Paul van Dinther
This discussion has been closed.
Comments
connectivity for the report. However, this property is only for the query
dataviews contained in DADE.
Can you move between ADO connections just by using the runtime designer and
changing DADE's Datasettings property? This would be the first test to make
sure that the connections can be opened in the report designer.
The Designer.DataSettings.DatabaseName should show all of the available
ADOConnection objects.
What object is ReportItem?
Cheers,
Jim Bennett
Digital Metaphors
http://www.digital-metaphors.com
info@digital-metaphors.com
see the following article from the tech-tips section ...
-------------------------------------------------
Tech Tip: 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
a FieldAlias 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.
1. Declare an event-handler procedure in the private
section of your form declaration.
type
myForm = class(TForm)
private
procedure ReportTemplateLoadEndEvent(Sender: TObject);
public
end;
2. Use the FormCreate event to assign the event-handler to
the event property.
procedure myForm.FormCreate(Sender: TObject)
begin
Report1.Template.OnLoadEnd := ReportTemplateLoadEndEvent;
end;
3. Add code to the event-handler to specify the database name.
procedure myForm.ReportTemplateLoadEndEvent(Sender: TObject)
var
lSQL: TdaSQL;
begin
if GetSQLObject(Report1, lSQL) then
lSQL.DatabaseName := Designer.DataSettings.DatabaseName;
end;
4. Below is a tech tip for extracting the SQL object
from a report. TdaSQL is a class defined in daQClass.pas.
-------------------------------------------------
Tech Tip: How to access the SQL object associated
with a Report created using DADE
-------------------------------------------------
uses
daDatMod;
function GetSQLObject(aReport: TppReport; var aSQL: TdaSQL): Boolean;
var
lDataModule: TdaDataModule;
lDataView: TdaDataView;
begin
aSQL := 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 := TdaQueryDataView(lDataView).SQL;
end;
Result := (aSQL <> nil);
end;
--
Tech Support mailto:support@digital-metaphors.com
Digital Metaphors http://www.digital-metaphors.com
HTH,
Chris Ueberall;