Link to home
Start Free TrialLog in
Avatar of mitai
mitaiFlag for Canada

asked on

crystal reports shared variable works until i try to add it to a another field

hi experts

i have a report that is made up of 3 subreports.  the subreports run for each detail line of the main report.  They are located in the detail a, detail b, and detail c sections.  Each of these subreports display a bunch of details and then create a shared variable that is passed back to the main report.  This shared variable will display on the detail d section but if I try to add it to another field from the main report it turns to 0.  Not sure why this is happening.

I am using the following code to assign and display the shared variable


shared numbervar nVariablename:= sum({fieldname});


shared numbervar nVariablename;

display with addition (this is the one that doesn't work.  if I have a display one on the line it will also zero out the display one that was working before i add a addition to another field)

shared numbervar nVariablename;
nVariablename + {Reportnumberfield}

Any thoughts or direction would be appreciated as it is driving me crazy.

Avatar of Mike McCracken
Mike McCracken

If you display {Reportnumberfield} on th report, do you get a value?

Try adding a formula to the main report report header

shared numbervar nVariablename;

Changed zones
Deleted Apache
Added Crystal Reports

In general, what you're doing should work.  If the field was null, that might prevent the "addition" formula from displaying a result, but it wouldn't affect a formula that just displays the variable value, unless the "addition" formula was actually changing the variable (eg. nVariablename := nVariablename + {Reportnumberfield}).

 It might help if you could post the report, so that we could take a look at the formulas, etc.

 Were you using the field in the "addition" formula (Reportnumberfield in your example) in the report before you added it to that formula?

Avatar of mitai


Hi Experts,

Sorry for the delay on this but I have been busy on another project.  So here is the issue, when I create a new field that is not attached to anything (blank field) and I type the following i get the result

shared numbervar nVariablename;

where the nVariablename is the name i set in the subreport that ran in the previous detail line.  

But I also have a field that has the exact same code in it but returns 0, the only difference that I can see is that field is used in this detail line in another formula.  What am i missing, I am going insane.  

I have attached the report although I am not sure what good it will do as I can't attach the data, (multiple sources, vendor won't let me etc)

I know there must be something small I am missing.  I have tried removing all the whileprintingrecords; and adding them all back in and nothing changes.

Help Help Help Help  :)
SInce you didn't include the data with the report, which formula is causing the problem?

Avatar of mitai


Hi Mlmcc,

The @Testme field in the details e section is the one i added that works fine but if I try to add it to another field like the ACCT_EST_INc field  see @estGrossProfit it zeros it out.  I am sure it is a valuation time thing or I have exceeded the number of shared variables allowed or something like that.  Also this is not my report just one I was asked to fix so please don't judge to hard :)  I would have done it differently but I don't have the budget to redo the report.

Thanks for your help

Could this field be NULL


Avatar of mitai


nope it is not null,

Really am stumped with this one, I have used shared variables before and never had any issues, not sure why this is such a problem.  Is there a memory limit on the number of variables that you can use in crystal?  I am using quite a few.

Are you saying that when you add the EstGrossProfit formula, the testme formula comes out 0?  Or the testme formula is still OK, and EstGrossProfit comes out 0?

 You have the "PO and PO Hist Total", "PO Amount Main" and "PO History Total" formulas in the same section.  Do they show the correct values?  If so, then I think the problem has to be the JOB_HEADER.ACCT_ACT_INC field.  You could try changing the EstGrossProfit formula to this:

if IsNull ({JOB_HEADER.ACCT_ACT_INC}) then
  {JOB_HEADER.ACCT_ACT_INC} + {@PO and PO Hist Total}

 If you get 99999.99 on the report, then that field is null.

 I also noticed a few other things, which may or may not have anything to do with that specific problem.

 DetailsA uses this conditional suppression formula:

{@EstGrossProfit} = 0 or isnull({@EstGrossProfit})

 That seems odd, since EstGrossProfit uses shared variables that are set in subreports that are located in sections after that.  So, the values in the variables at that point will be from the previous record, assuming that the variables weren't changed somewhere in the main report after the last record was read.  IAC, those variables will not have values for the current record, because those subreports haven't been run yet.

 The "reset variables" formula in the main report seems to have an error.  It uses variable name pototal, but the actual name appears to be postotal.

 Likewise, in "Purhcase Order Subreport.rpt", the "variables" formula uses POTotal, instead of postotal.

 I suggest that you review all of your formulas in the main and subreports and check the variable names (and declarations) carefully.  CR does not try to check the variables at all.  If you misspell a name, or get a declaration wrong (Global in one formula, when it should be Shared), CR just creates a different variable.

Avatar of mitai


Thanks James I will take a look and see if what you said makes a difference..
Avatar of mitai
Flag of Canada image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
WHat reference do you have for that?

Avatar of mitai


others were not 100% correct