Subject Re: Totaling fields in a report
From Mervyn Bick <invalid@invalid.invalid>
Date Mon, 19 Dec 2022 17:31:46 +0200
Newsgroups dbase.getting-started
Attachment(s) report_tree.pdf

On 2022/12/18 22:57, Charlie wrote:
> I am trying to total price , qty in a report.  I have attached the code.  Someone helped me do this several years ago and the other program works well so I tried to replicate what I think I did back then with that report.  However the other report I believe was created from scratch where this one was created by the wizard.  Possibly that is a factor.  With this attempt I am getting an error variable undefined for either ntotal or text2.  I have tried different parent combos (which I don't understand).   The bulk of the problem I think lies at the bottom code lines of the attachment.
>
> Any help will be very much appreciated.


As Akshat has pointed out, without a table we can't even open the report
in the designer.  A few records from the original table is all we need.
If the data is sensitive then an empty table is better than nothing.

Trying to debug a report without being able to open it in the designer
or run it is hard work!

The most obvious problem is that although code in the detailband
onRender event handler assumes that there is a text object named text2
on the band I don't see the constructor code for this object.  :-(


   function DETAILBAND_onRender()
       this.parent.parent.reportgroup.ntotal +=this.text2.text
       return


Although you are grouping records by manufacturer you are not creating a
total for each manufacturer.  Is this what you want?


"Who's your daddy" can be tricky in reports.  I use the attached table
to help me trace parentage of an object.

It's not written on a tablet of stone but I prefer to create calculated
fields in the query rather than using the canRender event handler of the
text object which will display the calculated value.  If you use the *
wildcard and create a calculated field you need to use a table
correlation name (alias) with the wildcard.  In the code below I've used
i as the correlation name.


    this.IMP_SPD1 = new QUERY(this)
    with (this.IMP_SPD1)
       database = form.form.train1
       sql = "select i.*,qty*price as cost from imp_spd.dbf i ORDER BY
MFG,SKU"
       requestLive = false
       active = true
    endwith


Now the designer will show the cost field in the Field palette and you
can drag it onto the report.

dBASE has a built-in function agSUM() which can be used to calculate
totals on a field.  There can, however, be rounding errors where, say,
tax is calculated per item and then totaled.  Where this happens one
needs to resort to the DIY method you have used here.  For most reports
though agSum() works fine.

To use agSum(), select Layout -> Add Groups and Summaries in the
designer.  Select the Summaries tab and select the field(s) to sum.

I dislike the text and colour streamed out by the designer so I tend to
place a normal text object and then assign the following (for a text
object on the reportgroup footerband) to it's text property.

text =
{||this.parent.parent.agSum({||this.parent.STREAMSOURCE1.rowset.fields["cost"].value})}


  Mervyn.