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

Change databases Aliases

edited July 2005 in General
Hi,

I use an application similar to Demo End User -> Report Explorer and users
can edit reports stored in DB.

On aplication start, user select the database alias for database files but
reports is
generated with the database aliases used on this creation.

How to change database alias BEFORE report is processed ?

Any help ?

Samuel

Comments

  • edited July 2005
    Hi Samuel,

    If you are referencing the database alias rather than the TDatabase object,
    you will need to change this using the template event OnLoadEnd. From there
    you will need to extract the SQL object and change the database alias as you
    need.

    Check out the following articles.

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



    -------------------------------------------------
    Tech Tip: How to access the SQL object associated
    with a Report created using DADE
    -------------------------------------------------

    TdaSQL is a class defined in daQClass.pas.
    TdaSQL has a run-time interface for adding search Criteria,
    etc. For an example see the EndUser\Custom DataViews example.

    OR you can assign its SQLText property. Please note that
    once you directly assign the SQLText property the Query tool
    buttons such as Sort, Search can no longer be used.



    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

    Once you've retrieved the SQL object, you can do something useful to it,
    like adding criteria values:

    procedure ppReport1BeforePrint(Sender: TObject);
    var
    lSQL: TdaSQL;
    lFields: TStringList;
    lCriteria: TdaCriteria;
    liIndex: Integer;
    begin

    {get SQL object}
    lSQL := GetSQLObject(ppReport1);

    {if criteria have not been created, then add them}
    if (lSQL.CriteriaCount = 0) then
    begin
    {get all available criteria fields}
    lFields := TStringList.Create;

    lSQL.AvailableCriteriaList(lFields);

    {set string list entries to field names}
    for liIndex := 0 to lFields.Count - 1 do
    lFields[liIndex] := TdaField(lFields[liIndex]).FieldName;

    {create order no criteria}
    liIndex := lFields.IndexOf('OrderNo');

    if (liIndex <> -1) then
    begin
    lCriteria := lSQL.SelectCriteria(liIndex);
    lCriteria.Operator := dacoEqual;
    end;

    {create art code criteria}
    liIndex := lFields.IndexOf('ArtCode');

    if (liIndex <> -1) then
    begin
    lCriteria := lSQL.SelectCriteria(liIndex);
    lCriteria.Operator := dacoEqual;
    end;

    {create begin/end date criteria}
    liIndex := lFields.IndexOf('OrderDate');

    if (liIndex <> -1) then
    begin
    lCriteria := lSQL.SelectCriteria(liIndex);
    lCriteria.Operator := dacoBetween;
    end;

    lFields.Free;
    end;

    {set order no. search value}
    lSQL.Criteria[0].Value := '1020';

    {set art code search value}
    lSQL.Criteria[1].Value := 'C';

    {set begin/end date search value}
    lSQL.Criteria[2].Value := '31/12/90,31/12/99';

    end;

    --
    Regards,

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

    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
This discussion has been closed.