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

Calculations Based on Summaries

edited April 2003 in RAP
Hi,

I have a problem similar to another posted regarding calculations, which has
to do with the variable's datatype is correct. I'm running RB 7.02 on D7Ent
and WinXP Pro. I'm also trying to do everything via the Report Explorer,
which means I must use RAP rather than Delphi events and the Object
Inspector.

I have two (DBCalcs) summary values, A and B, which appear to sum up just
fine. Then I have a variable (C) which, in its OnCalc event, is set to:
Value := (A.Value / B.Value) * 100. A and B are both of type Currency, and C
is set to dtString. The result is always 0 regardless of the two sum fields,
which is kind of explained in the response to that other posted message.

I also have other calculated fields which I added using the Calcs tab of the
dataview.

With that in mind, I have the following questions:

1. Is my statement about having to use RAP rather than Delphi events and
Object Inspector correct?
2. I couldn't find a way to change the default datatype for C within RB/RAP.
I had to save the report to an .rtm file and load it into another
(development) project so I can change the datatype in Delphi. Is it not
possible to change/set the datatype with RB/RAP?
3. Is there a way to make sure the event is firing? Can I set a breakpoint
or use a "ShowMessage" equivalent?
4. When viewing the Data tab and realizing that a calculated field was added
by mistake using its Calcs tab. Is there a way to remove that incorrect
calculated field? I tried the obvious (pressing the Del key, right-clicking,
etc. -- Right-Click and selecting Delete removes the entire dataview which
is not what I want to do).

Thanks for your help

Waguih

Comments

  • edited April 2003
    Hi Waguih,

    1. Yes, RAP would be your best option if trying to use different event
    handlers for each template you load in the Report Explorer.

    2. You can change the datatype of a component using RAP. Select the
    component in the report tree and look for the datatype property in the Code
    Toolbox Objects tab in the lower right corner of the Calc Tab.

    3. The ShowMessage function is allowed in RAP and is available to drag from
    the Code Toolbox under the "language" tab and "utility" option. There is
    also a third party debugger called CodeSite which can be used with RAP
    passthu function to debug RAP code.(See the RAP demos).

    4. To Remove an item from the included list of the Calcs tab in the Query
    Designer, just double click it.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited April 2003
    Hi Nico,

    Thanks for your response. Re item #4, I don't know how I could've missed
    trying to double-click. However, re item #2, I did find my way there, but
    then all these properties appear to be read-only. I even tried
    double-clicking that. I also tried setting the DataType property in the
    OnCalc event handler, e.g. DataType := dtDouble. The RAP compiler, however,
    complained. Can you please shed some light on this for me?

    Thanks

    Waguih

  • edited April 2003
    Hi Waguih,

    Try setting the variable's datatype in the GlobalOnCreate event. You can
    access this event by right clicking in the Report Objects window of the Data
    tab in the Report Designer and selecting "Module". Then select the Events
    option and create a new OnCreate event. Insert code that looks something
    like the following into this event...

    procedure GlobalOnCreate
    begin
    Variable1.DataType := dtDouble;
    end;

    Now you can use the variable as a double in it's OnCalc event. Don't try
    changing the datatype as the report is generating. Use separate variables
    if you would like to use different datatypes.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited April 2003
    Thanks Nico. This worked very well. I suspect I also have to set the
    DisplayFormat there too after setting the DataType.

    Cheers,

    Waguih

  • edited May 2003
    Where/when is the other post that explains how to do a calc based on summary
    values? I am trying to do the same and get a value of 0 everytime. (The
    report is pretty simple, each row is selected from a stored procedure that
    sends back each field needed for the row. The report is not grouped and I
    have a summary band with a number of dbCalc objects that are simple
    summaries of the corresponding fields in the report. I need to do (A/B) *
    100 for two of these columns. This needs to be done with RAP.)

    TIA,
    Carolyn

  • edited May 2003
    The easiest way to do it from RAP is to add a calculated field on your
    dataset that is the total. Then you can simply reference that field from the
    pipeline in the RAP OnCalc event handler of a TppVariable component.

    Otherwise, here is an example of doing it in RAP without a calc field:

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

    Cheers,

    Jim Bennett
    Digital Metaphors


  • edited May 2003
    Thanks for your reply. I have not looked at the example yet because I would
    like to understand the "easiest way" first. I am fairly new to Delphi and
    Report Builder (like what a see and know so far) so here are some (probably)
    simple level questions I have.

    Each row of my report is a summary of how much money was billed against a
    given contract, how much was paid out, and some other similar fields. I
    have a header, detail, footer, and summary band in the report. In the
    Summary band I used dbCalc fields to sum the various columns. So I have a
    field called Total Billed and another called TotalPaid. I need the
    percentage paid. In the same summary band I have tried to put in a
    tppVariable and in its OnCalc event (on ReportEnd) I tried value :=
    (TotalPaid.fieldValue / TotalBilled.fieldValue)/ 100; and I get 0. I even
    tried value := 3.5; to see if it would display anything and that shows 0
    also.

    When you say to add a calculated field on the dataset that is the total, do
    you mean I should add it to the stored procedure and keep a running total
    there of the two values and pass those back each time I get a new row? If
    so, how do I reference just the last totals to calculate my percentage?

    Thanks,
    Carolyn

  • edited May 2003
    On your variable, set its DataType to double, as it defaults to string and
    will show a zero in this case.

    Also, in order to assure that the OnCalc events fire in the right order, use
    TppVariable instead of TppDBCalc. Set the variable's CalcOrder property so
    you can be 100% sure that the calcs fire in the right order. This only
    applies to the variables in the same band.

    It is problably better to not add calc fields to the stored proc, but rather
    calculate them in the first pass with report event handlers.

    Cheers,

    Jim Bennett
    Digital Metaphors


  • edited May 2003
    Thanks, for some reason I could not connect to this NG last week so I just
    saw your response. I'll try it today or tomorrow and let you know if I have
    any remaining questions.

    Thanks,
    Carolyn

  • edited May 2003
    OK please be patient with these elementary questions, but I have not really
    found anything in the tutorials or examples yet that have gotten me past
    this point yet.

    I added a variable in my summary band and put the following in it's onCalc
    method:

    Value := Value + dbpContractBalance['O_TOTAL_BILLED'];

    It works as I would hope and comes up with the same answer as when I used a
    dbCalc variable set to show the sum of the Total Billed field. I then tried
    putting another variable in that would eventually show the (total amount
    billed - total pending - total paid) / total paid. I thought I would first
    try to reference my calculated variable by just entering:

    Value := vTotalBilled;

    Where vTotalBilled is the variable I calculate above. I tried setting it as
    a Double and Currency (my calculated variable is Currency) and verified the
    calc order so that vTotalBilled is calculated first. But in all cases it
    just displays a 0.

    What am I doing wrong? If I cannot reference another object from this one,
    how do I declare another (local? global?) variable so that I can calculate
    the totals in this method and then use them (for example, I will need to
    calculate the total pending, billed, and spent and then use those in my
    equation)? Does it have to be a global variable? Is there an example or
    tutorial that would help me get a foothold on using RAP?

    Thanks,

    Carolyn

This discussion has been closed.