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

Default ADO connection

edited September 2007 in End User
Dear all,
I have gone through the tech tips and other posts and still can't
work this out.

I have an application where the user can design reports and store them in th
database.

Within the same app the user can select reports using
ppreport3.Template.LoadFromDatabase and all is well.

Now I want to access the same reports from another app.

I am using the same code, however when I go to print the report up pops the
DefaultADO login box.

I know that the database settings from the report creation in the first app
is ADOconnection1. I added a connection with this name to my new form.
I changed the ppReport component to point ot this database property.

I left the LoginPrompt for the connection set to true.

Now when I print the report, it brings up the login box for adoconnection1.
I press OK and then the DefaultADO login box appears.

So it wants to connect to 2 connections.

What am I doing wrong?

thanks

John

Comments

  • edited September 2007
    Hi John,

    The database connection information is stored inside the template so once
    you load the template, the connection setting you gave the report object is
    overwritten. Try using the template event OnLoadEnd to assign the database
    connection. See the article below on using template events.

    ----------------------------------------------
    Tech Tip: Using Template Events
    ----------------------------------------------

    The Report.Template object has several events that can be used for
    customizing what happens when a report is loaded or saved:

    - OnLoadStart
    - OnLoadEnd
    - OnNew
    - OnSaveStart
    - OnSaveEnd


    The OnLoadEnd and OnNew events are often used to perform actions related
    to report and data initialization.

    The OnSaveEnd event is often used to save additional descriptive
    ("meta") data to the database each time the report is saved.

    Example:

    The Report.Template events are public and therefore must be assigned at
    run-time.


    1. In the private section of your form declaration you can declare an
    event-handler method:

    TForm = class(TForm)
    private
    procedure myTemplateOnLoadEndEvent(Sender: TObject);

    public

    end;


    2. In the Form.OnCreate event, you can assign the event-handler to the
    event:

    procedure TForm1.FormCreate(Sender: TObject);
    begin

    ppReport1.Template.OnLoadEnd := myTemplateOnLoadEndEvent;

    end;


    3. Implement the event-handler method:

    procedure TForm1.myTemplateOnLoadEndEvent(Sender: TObject);
    begin

    {add code here to initial the report or data, etc. }
    ppReport1.PrinterSetup.MarginTop := 0.5;

    end;


    --
    Regards,

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

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited September 2007
    Dear Nico,
    I have done that and no joy. Are there other database settings I
    need to look at?

    My code is shown below. It steps through the reportonload event ok.

    thanks

    John

    main proc
    ...
    ppreport3.Template.DatabaseSettings.Name :=
    dmprintorders.qlulabelsnewitemname.AsString;
    if
    ReportLocateRecordEvent(sender,dmprintorders.qlulabelsnewitemname.AsString)
    then
    ppreport3.Template.LoadFromDatabase;

    ppreport3.PrinterSetup.PrinterName:=rbitemtable.fieldbyname('printer').asstr
    ing;
    ppreport3.DeviceType:='screen';
    ppreport3.ShowPrintDialog:=false;
    ppreport3.DataPipeline:=ppDBPipeline1;
    ppreport3.Print;
    ...

    function TfrmPrintDialog1.ReportLocateRecordEvent(Sender: TObject; const
    aReportName:String): Boolean;
    begin
    result:=false;
    rbitemtable.Open;
    if rbitemtable.Locate('folderid;itemname', vararrayof([0,
    aReportName]),[])
    then
    Result :=true;
    end;

    procedure TfrmPrintDialog1.ReportOnloadEnd(Sender: TObject);
    begin
    ppreport3.Template.DatabaseSettings.DataPipeline:=pipedb;
    // the pipe component on the form, links to rbItemtable component.
    ppreport3.Template.DatabaseSettings.TemplateField:='template';
    ppreport3.Template.DatabaseSettings.NameField:='itemname';
    end;




    I have sterpped through and it is loading the correct template. I then

  • edited September 2007
    Sorry John, perhaps I wasn't clear. The reason you would be getting the
    default ADO connection would be that DADE cannot find the connection defined
    when the templates were created. This is the equivalent of going to the
    Data tab and selecting Data Settings from the file menu. These are the data
    settings I was referring to when the OnLoadEnd event is fired. Setting the
    Template.DatabaseSettings will most likely not have any effect as the
    template has already been loaded.

    -------------------------------------------------
    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;


    --
    Regards,

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

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited September 2007
    Dear Nico,
    where do I find 'GetSQLObject'

    I have added units daQclass, daDatMod, daSQL, daDataView, daQueryDataView,
    daADO, daDataModule, daDatamanager

    and still no joy!

    regards

    John



  • edited September 2007
    Dear Nico,
    forget last email, I get it that I have to include the GetSQLObject
    function myself.
    I have done this, and now I am stuck on the line
    lSQL.DatabaseName := Designer.DataSettings.DatabaseName;

    'Designer' is not declared. What is it referring to?

    regards

    John

  • edited September 2007
    Hi John,

    This is simply referring to the database name used by the TppDesigner
    object. You can manually set this as well to the name of the connection on
    your form.

    --
    Regards,

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

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited September 2007
    Dear Nico,
    great, it now works fine.

    Many thanks

    John



This discussion has been closed.