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

problem with Variables and where to put them

edited January 2003 in General
I have a question about variables and calculating them correctly.

I have one table with a pipeline and a report.

Report is layed out like this


__________________________________________
title
__________________________________________
Group.Header[0] Salesperson_1
__________________________________________
Group.Header[1] TypeofProspect
__________________________________________
Detail
Var1 (Count: RefNum) (*Just counts the customers within this group *)
__________________________________________
Group.Footer[1] TypeofProspect
dbField(TypeOfProspect) var2 (Count : RefNum) (Label1)
__________________________________________
Group.Footer[0] SalesPerson_1
var3 (Count: RefNum)
__________________________________________



Var1,2,3 all come up with the correct numbers.
But what I am trying to do is assign Label1 the value of Var2 / Var3 to get
a percent. My problem is that I don't know what Var3 is until after the
grouping is done... How do I do this?

I have tried Lookahed property but my label1 still comes up as 20% each time
(which is the value of the last record in that TypeofProspect grouping.

Hope this makes sense.

I am using Delphi 4, FlashFiler DB, and Report Buider Version 5 (package
name of dclRB54.bpl)

thanks,
Mike

Comments

  • edited January 2003
    When are you assigning the value to the label? Use a TppVariable instead of
    a Label to show this percent. Then set it to LookAhead. That would be the
    only chance to quickly get it to update the variable in the group footer
    [1]. Otherwise, you'll need to build the total information in the first pass
    of the report and perform the calculations of the percentages in the second
    pass. Here is an example showing how to do this:

    http://www.digital-metaphors.com/tips/PercentageOfGroupTotal.zip

    Cheers,

    Jim Bennett
    Digital Metaphors


  • edited January 2003
    I get invalid stream format.
    Was this created with D4?
    That is what I am using.

    thanks,
    Mike




  • edited January 2003
    The latest zip on the web server is in RB 7.01 for D6 format. I ran it in D5
    and it works fine, as long as I remove Variants from the uses clause. I
    don't have D4 installed. Can you open up the pas file to see the Delphi
    code without the dfm needing to be opened?

    unit PercentageOfGroupTotal;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, DB, ADODB, ppBands, ppClass, ppCtrls, ppVar, ppPrnabl, ppCache,
    ppProd, ppReport, ppComm, ppRelatv, ppDB, ppDBPipe, ppModule,
    StdCtrls, ppTypes,
    DBTables, daDataModule;

    type

    TForm1 = class(TForm)
    ppDBPipeline1: TppDBPipeline;
    ppReport1: TppReport;
    ppReport1DetailBand1: TppDetailBand;
    ppReport1DBText3: TppDBText;
    ppReport1DBText4: TppDBText;
    varGrandTotalPercent: TppVariable;
    ppReport1FooterBand1: TppFooterBand;
    ppReport1SystemVariable1: TppSystemVariable;
    ppReport1SystemVariable2: TppSystemVariable;
    ppReport1SummaryBand1: TppSummaryBand;
    ppGroup1: TppGroup;
    ppReport1GroupHeaderBand1: TppGroupHeaderBand;
    ppReport1GroupFooterBand1: TppGroupFooterBand;
    dbcAmountPaid: TppDBCalc;
    varCustomerTotalPercent: TppVariable;
    ppReport1Label9: TppLabel;
    dbcGrandTotal: TppDBCalc;
    ppReport1Label13: TppLabel;
    daDataModule1: TdaDataModule;
    Preview: TButton;
    DataSource1: TDataSource;
    Query1: TQuery;
    ppDBText1: TppDBText;
    ppLabel1: TppLabel;
    ppLabel2: TppLabel;
    procedure ppReport1GroupFooterBand1AfterPrint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure varGrandTotalPercentCalc(Sender: TObject; var Value: Variant);
    procedure varCustomerTotalPercentCalc(Sender: TObject; var Value:
    Variant);
    procedure ppReport1SummaryBand1AfterPrint(Sender: TObject);
    procedure PreviewClick(Sender: TObject);
    private
    FCustomerTotals: TList;
    FGrandTotal: Double;
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    FCustomerTotals := TList.Create;
    end;

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    FCustomerTotals.Free;
    end;

    procedure TForm1.ppReport1GroupFooterBand1AfterPrint(Sender: TObject);
    var
    liAmount: Integer;
    begin

    if (ppReport1.FirstPass) then
    begin
    liAmount := Trunc(dbcAmountPaid.Value * 100.0);

    FCustomerTotals.Add(TObject(liAmount));
    end;

    end;

    procedure TForm1.varCustomerTotalPercentCalc(Sender: TObject; var Value:
    Variant);
    var
    ldAmountPaid: Double;
    ldTotal: Double;
    liIndex: Integer;
    begin

    if (ppReport1.SecondPass) then
    begin
    liIndex := ppGroup1.BreakNo;

    ldTotal := Integer(FCustomerTotals[liIndex]) / 100.0;
    ldAmountPaid := ppDBPipeline1['AmountPaid'];

    if (ldTotal > 0) then
    Value := (ldAmountPaid / ldTotal) * 100
    else
    Value := 0;
    end;

    end;

    procedure TForm1.varGrandTotalPercentCalc(Sender: TObject; var Value:
    Variant);
    var
    liIndex: Integer;
    begin

    if (ppReport1.SecondPass) then
    begin
    liIndex := ppGroup1.BreakNo;

    Value := Integer(FCustomerTotals[liIndex]) / FGrandTotal;
    end;

    end;

    procedure TForm1.ppReport1SummaryBand1AfterPrint(Sender: TObject);
    begin
    FGrandTotal := dbcGrandTotal.Value;
    end;

    procedure TForm1.PreviewClick(Sender: TObject);
    begin
    ppReport1.Print;
    end;

    end.


    Cheers,

    Jim Bennett
    Digital Metaphors


  • edited January 2003
    no it won't... but i see how you are doing what i need to do ..
    i will test it out and let you know.

    thanks for the help,
    Mike


  • edited January 2003
    Jim,

    i can't get this to work .. i tried putting what you had in this unit with
    what i got and i keep getting blank answers...it is hard to find out what i
    am doing wrong without seeing the actual report in the designer. Is there
    any way you can give this to me in D4 or maybe someone else out there has an
    example of putting variables values in first pass then displaying them in
    2nd pass ?

    sorry for any confusion.

    thanks,
    Mike


  • edited February 2003
    Sorry, I don't have D4 installed. If you are using RB 5.0, then you should
    be able to load the report template into a report that you create in D4.
    I've added it to the download zip file. Ignore the property streaming errors
    and it may just work, to at least show you the layout. Then you can port the
    event handlers from my previous email to your new report form/unit.



    Cheers,

    Jim Bennett
    Digital Metaphors


  • edited February 2003
    that worked great
    Thank you for dealing with me.
    I appreciate the help tremendously.

    thanks,
    Mike


This discussion has been closed.