Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shared Variable Not resetting on Change of Group 1

Status
Not open for further replies.

Wabush

Programmer
Jan 15, 2001
31
0
0
US
I have a shared variable in a subreport that sums an amount field and is linked to the main report by the group field 'Bank No'. If there are no fields in the subreport for the main report's group, the variable retains the value from the last group. How do I get this to reset?
To ease the confusion, here's the formula I am using to create the shared variable and an example report.
(As an aside, is there any way to get this to show 0 instead of null?)

Formula:
Shared numberVar OpeningBalance := Sum ({@Amount_moved});

In the main report, here is the (pseudo)formula to get the balance:
Shared NumberVar OpeningBalance;

Balance = OpeningBalance - Outputs;
And, here is a sample of data:

Bank Number - SubreportValue - Outputs - Balance
0715 - 30 - 450 - (-420)
0716 - Null - 400 - (-370)
 
Create a formula like:

whileprintingrecords;
Shared numberVar OpeningBalance := 0;

Place this field either as the last field in the group footer, or one of the first fields in the group header. And make sure the field is suppressed.

With regard to turning the 'Null' string into 0, there are a few ways you can do this. One is to create a formula like:

whileprintingrecords;
numbervar total;
shared numbervar OpeningBalance;

if OpeningBalance = 'Null' then total := '0' else total := OpeningBalance;
 
there are a couple of problems here....one is that for a shared value to be valid the subreport must be run first...when they are on the same line this doesn't necessarily happen.

So the subreport should be in a subsection before the shared result

Bank Number - SubreportValue - Outputs - Balance
0715 - 30 - 450 - (-420)
0716 - Null - 400 - (-370)

In this case you are actually using the shared value twice since the Subreport is in reality the shared value.

I would approach your problem as follows

1. if this is the detail section I would have the detail
section split into an A & B subsections in the main
report.

the Subreport would be placed, suppressed, all
borders removed, the subreport field size made as thin
as possible with the subsection border tight to the
bottom and background shaded red so it can be found
months later in maintenance with the conditional
formula: if 1 = 1 then crNoColor else crRed

2. the subreport itself will have all sections suppressed
3. in the subreport report header there will be an
initialization formula

@initialization
WhilePrintingRecords;
Shared numberVar OpeningBalance := 0;

4. in the detail section would be a calculation formula

(this is unknown since I don't know what the formula
{@Amount_moved} is but what ever it is the format
should look something like this....)

@CalcOpeningBalance
WhilePrintingRecords;
Shared numberVar OpeningBalance ;
(varaible declarations of {@amount_moved};

//pick a field used in the former {@amount_moved} formula
//for a test to see if any values returned

if not isnull({Table.keyfield}) then
(
//reproduce the {@Amount_Moved} formula here
( formula details);

OpeningBalance := OpeningBalance + (amt_moved value);
);

5. In the Subreport footer place the formula

@DisplayOpeningBalance
WhilePrintingRecords;
Shared numberVar OpeningBalance ;

OpeningBalance ;

This step is not 100% necessary but it helps in debugging
if something doesn't look right.

6. now your MAIN REPORT detail section B would have
formulas: @OpeningBalance and @Balance

@OpeningBalance
WhilePrintingRecords;
Shared numberVar OpeningBalance ;

OpeningBalance ;

@Balance
WhilePrintingRecords;
Shared numberVar OpeningBalance ;

OpeningBalance - {@Outputs};

the Detail B line would look like

Bank Number - @OpeningBalance - @Outputs - @Balance

pretty detailed but I think it is clear enough....any questions just ask.

A subreport executes once even if it returns no records...testing for a Null after initializing the total gets you a non-null tesult.













Jim Broadbent
 
Thanks, Naith, that worked. I just modified the formula you gave to :

Shared numberVar OpeningBalance := 0;
if isnull(sum({@Amount_moved})) then OpeningBalance
else Sum ({@Amount_moved})

and it worked..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top