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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maximum Value in an Array 1

Status
Not open for further replies.

McNasti

Technical User
Nov 11, 2004
13
US
I have 4 subreports containing one shared numbervar field each. And a formula that I'd like return the maximum values of each sub value.

I'd like it to return data as follows.


ID SUB1 SUB2 SUB3 SUB4 MAX
-- ---- ---- ---- ---- ----
01 4.50 1.00 2.39 6.90 6.90
04 1.00 2.00 3.33 2.45 3.33
08 2.19 1.00 4.45 2.34 4.45
11 3.31 2.45 6.78 3.45 6.78
13 2.45 2.00 4.55 7.00 7.00

This is the formula i've incorrectly created to get the Max.

Whileprintingrecords;
Shared NumberVar Sub1;
Shared NumberVar Sub2;
Shared NumberVar Sub3;
Shared NumberVar Sub4;

Maximum([Sub1,Sub2,Sub3,Sub4])

Thanks for you help in advance. Pat
 
Try:

Whileprintingrecords;
Shared NumberVar Sub1;
Shared NumberVar Sub2;
Shared NumberVar Sub3;
Shared NumberVar Sub4;
numbervar array MyValues:= [Sub1,Sub2,Sub3,Sub4];
maximum(MyValues)

-k
 
This doesn't quite work.

Oddly, it returns the max value for the current row on the next row. It also maintains the max value for multiple rows until a row of greater value is reached. If i use the example from above it returns something like the following.

ID SUB1 SUB2 SUB3 SUB4 MAX
-- ---- ---- ---- ---- ----
01 4.50 1.00 2.39 6.90 0.00
04 1.00 2.00 3.33 2.45 6.90
08 2.19 1.00 4.45 2.34 6.90
11 3.31 2.45 6.78 3.45 6.90
13 2.45 2.00 4.55 7.00 6.90
15 1.45 1.00 3.55 4.00 7.00

 
It works fine, it may be that you are evaluating prior to the variables being set.

At any rate, if the value is zero in the first row, that means that the values for the shared variables aren't set within this formula, right?

In general, if you think that something doesn't work with variables being passed, test it by changing the variables to numerics, you would have seen that it works.

And when you initially posted you stated that you incorrectly created a formula, when you should have posted what you were getting (error, bad data, or?). Post specifics, saying it's incorrect doesn't help much.

It probably has something to do with how/when you are populating your variables.

You might try using:

evaluateafter({@YourSub1Formula});
Shared NumberVar Sub1;
Shared NumberVar Sub2;
Shared NumberVar Sub3;
Shared NumberVar Sub4;
numbervar array MyValues:= [Sub1,Sub2,Sub3,Sub4];
maximum(MyValues)

-k
 
The formula must be placed in a section below the subreports (not in the same section) because a formula placed in the same section will always evaluate BEFORE the subreports are processed.

- Ido

Visual CUT & DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
Hi Again,

Thanks Synapse,

I've tried using the following. It doesn't seem to recognize @shared_Sub1

evaluateafter({@shared_Sub1});//formula in my subreport
Shared NumberVar Sub1;
Shared NumberVar Sub2;
Shared NumberVar Sub3;
Shared NumberVar Sub4;
numbervar array MyValues:= [Sub1,Sub2,Sub3,Sub4];
maximum(MyValues)

Thanks, Ido- I can't place the formula in a section below because i need it display my formula result in the same row as the other values so that the report looks tabular.

Any other suggestions? Thanks




 
As a main report formula, there is no way around this requirement.

Perhaps you can place the formula in a subreport but you would need to ensure that this subreport gets evaluated after all the other subreports. Other than trial and error, I'm not aware of a way to ensure an order of execution across subreports.

Another option is to get the necessary data in the main report query/command or from the data source (database View).

hth,
- Ido

Visual CUT & DataLink Viewer:
view, e-mail, export, burst, distribute, and schedule Crystal Reports.
 
Where is {@shared_Sub1}, in a subreport? You'd need it available in the main report.
It appears that soemthing Ido suggeste worked for you.

In general subreports are a bad idea, consider using a data source which returns everything required.

-k
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top