Crystal Reports: global variable running total not displaying in header - crystal-reports

Using Crystal Reports I'm trying to display the running total of a database field in the header where all the labels are.
I've attempted to do this by placing the running total (RTversion) into a formula field with the following:
Shared stringvar CurrentVers;
CurrentVers := {#CurrentVers};
and then in the page header section I have the following:
Shared stringvar CurrentVers;
EvaluateAFter({#currentVers});
CurrentVers;
with {#CurrentVers} running the 1st largest number.
Is this incorrect?
Update: The goal is to display the latest version in the header near the labels to show what the current verseion is for comparison.

Running-Total Fields, in my experience, only work in the footer sections.
You will need to create a manual running total.
Add a formula field to the Details section that populates a Global variable with whatever you are trying to capture. Something like:
//use WhileReadingRecords if the values can be gathered as the report pulls in values from the database. Otherwise, use WhilePrintingRecords.
WhileReadingRecords;
Global Stringvar CurrentVers;
//logic here to capture what you want
CurrentVers:=...
Add another formula field to the Header section. Add these two lines to it:
WhilePrintingRecords;
Global Stringvar CurrentVers;

Create a formula like this
formula = Count ({Field to count},{GroupFieldName})
and place it in group header section.

I figured it out..
I had to add a subreport to the main report. I copied the main report and renamed it as a subreport. In the subreport, I added a shared variable and then passed it to the main report. The trick is to place the subreport in the same group header, but a separate section above the section that needs to be suppressed. I actually added the following sections:
New section (same group-I placed subreport here; do not suppress)
New Section (same group - I placed shared variable value here; do not suppress)
Original Section (same group that has a header I need suppressed based on a running total)

If {#CurrentVers} is a regular running total field, you should be able to place it in the page header directly rather than resort to an additional formula. I don't see the need for either formula field here, unless there's something unusual in the composition of {#CurrentVers} and a 'largest number' running total shouldn't require anything out of the ordinary.

You can solve this with just one formula field:
WhilePrintingRecords;
// Enter logic here e.g.
// maximum({mytable.myfield});
The WhilePrintingRecords; forces the formula to not be evaluated until all records have been read from the database, and therefore the formula field will display the correct result even if placed in a header.

The key I found is that the formula field being passed needs to be placed on the subreport. I placed mine in the footer then suppressed it.

Related

How can I Pass the SUM of Group of a Sub-report to Main Report on Crystal Reports

I have a Crystal Report of "SALES"
All are GROUPED AND SUMMED Daily,Weekly,and Monthly
I also have a Sub-report of "EXPENSES"
What I want to have is to have a Total SALES and EXPENSES for each GROUP FOOTER on the Main Report which would look like this
I think a Shared variable will do the trick here. When creating variables in Crystal Report you can have a scope of Local, Global, or Shared. Local variables are in scope within a single formula or expression. Global variables are in scope within different sections of a report. Shared variables are in scope throughout the different sections of the report and any sub-reports as well.
Begin by creating a Formula Field in your Expenses sub-report. This formula field will need to create the variable and populate it with a value it reads from a field on your sub-report. Since I don't know the names of your fields, I will make one up called {Table.ExpenseTotal}. Now we can write the code for the formula field as follows:
WhilePrintingRecords;
Shared NumberVar expense := {Table.ExpenseTotal};
The next step is to place this formula field into the sub-report. It should be placed somewhere in the same section of the sub-report that contains the {Table.ExpenseTotal} field. This formula field will also display its value on the sub-report, so you will probably want to set its Suppress property to hide this field.
Now you will need to create a new formula field in your main report. The code for this formula field will be as follows:
WhilePrintingRecords;
Shared NumberVar expense;
The next step is to place this formula field into the main report. It must be placed in a section that follows the section containing the sub-report. At this time the formula field should be displaying the value of the expense variable that was created and populated in your sub-report.
To apply this to your report you will want to substitute the field that contains the Sum of expenses for the example field name I used.
There are some limitations to consider when using variables like this. These variables do not exist until the report is being printed, which is why the "WhilePrintingRecords" declaration is used. This means these formula fields may not be used in Summary Fields, Running Total Fields, or Record Selection because all of these are parsed before the report begins printing. You should also be mindful of which sections you place the Formula Fields within. The code for each variable is very simple usually, but the placement within sections and how Crystal Reports moves from section to section while printing is what determines the looping structure for how the values of your variable are updated.

Crystal: value from Subreport to Main for use in formula

I have a subreport with a field called Qty not FBA that is grouped by Customer. I have used the group sort expert to show me only the top customer (the one with the most qty).
I tried what I found in the post "Passing Subreport Value to Main Report" but am still getting zeros. I am guessing it is because the sort to see only the top customer needs to be included in my code but I don't know how to do that.
I would like to pass that through to the main report for use in a formula. From what I have read elsewhere, I have created a field in the subreport:
WhilePrintingRecords;
Shared NumberVar PctofTotal := PercentOfSum ({#Qty not FBA}, {CUSTOMERS.NAME})
and a field in the main report:
WhilePrintingRecords;
Shared NumberVar PctofTotal;
PctofTotal
It is returning zero. I am guessing it is because the sort to only show the top one doesn't work with "While printing records" but I don't know how to get around that. Any help is much appreciated!

Crystal Reports, driving Header/Footer decisions based on a specific detail row

I have a simple report that can display many detail rows.
I want to target a specific row and use it's values to populate aspects in the Report Header and Footer.
For example , I know what the minimum(primarykey) within the detail rows that I display.
A simply formula can capture that.
I want to pick up other attributes of the row with the minimum(primarykey) and display in the header or footer.
I therefore want to select a specific row select and use its data in the header and footer, something like:
Select NAME, DATE from DETAILSROWS where DETAIL.PRIMARYKEY = minimum(DETAIL.PRIMARYKEY)
I've thought about using a subreport here, and passing values back to the main report, but that doesn't see to the way to go... I've tried for example passing minimum(DETAIL.PRIMARYKEY) to the subreport so that the sub loads only the row I want, then passing the values back via shared variables. But I don't want to display the subreport, and if I suppress it, my shared variables don't seem to populate.
Displaying the values in the Report Footer would be easier than the Header. All you'd need to do is compare each row's primary key to the value of maximum({DETAIL.PRIMARY_KEY}) and save off the values. So something like:
stringvar saved_column1;
stringvar saved_column2;
if {DETAIL.PRIMARY_KEY}=maximum({DETAIL.PRIMARY_KEY}) then
(saved_column1 := {DETAIL.COLUMN1};
saved_column2 := {DETAIL.COLUMN2})
For the header, really the only way I can think of is to use the subreport like you're already doing. I'd just format the subreport to display itself, though, and not mess around with shared variables.
Okay so it turns out the SUBREPORT option is the way to go.
SUBREPORT is a bare minimum report that has passed to it, the lookup value you are after.
Long winded, I link to it using a formula that holds essentially: minimum(DETAIL.PRIMARYKEY).
In turn the subreport selects that single detail record.
Within the subreport, build a Formula and drop it the into its report header:
WhilePrintingRecords;
Shared DateTimeVar subCreationDate := {DETAIL.CREATIONDATE};
Shared StringVar subCreationName := {DETAIL.CREATIONNAME};
Finally, suppress all the the sections (delete what u don't need) on the SUBREPORT, and close.
On the main report... Not sure but it looks like the SUBREPORT can sit anywhere.
And because you've suppressed elements WITHIN the SUBREPORT, it won't display on the main report.
(So no need to suppress on the main report - and if you do suppress it there, it won't work anyway!)
Now create your formulas (in my case 2) on the main report and in each, repeat the variable declaration.
Shared DateTimeVar subCreationDate
Shared StringVar subCreationName
Again, each of the above sit in their individual formula declaration.
And from there, you now have "standard" formulas to work with on your main report.
They can be referenced just like any other!
Thx Ryan - your answer certainly helped me get where I need to go - u still get my +1!

Crystal report subreport displays alternative text when no records returned in subreport

How to display alternative text when no records returned in subreport?
e.g. if there is no record returned in subreport, i want to display '-'
thanks
You need 2 details sections in your report. In 1 put the what ever you want to show when you have records. Right click the same section and select Section Expert.... Check the Suppress Blank Section checkbox so this space used by this section is suppressed when there is nothing to show. In the other details section add a formula that prints what you want to display when there are no records. In your example this would be "-". Create another formula for counting records and name it RecordCount.
WhileReadingRecords;
NumberVar RecordCount := RecordCount + 1;
Place this formula in the details section of the report and suppress it. It is not important which details section you place it in. Finally, right click the same section that will show when there are no records select Section Expert.... Click the formula button for the Suppress (No Drill-Down) and add this formula...
Not Isnull ({#RecordCount})
You can see that you are only checking if the formula itself is null so the code in the formula is not all that important, but I like to make it useful in case I want to use it elsewhere in the report.
Now when you have records the second details section is suppressed and the first details section shows your data. When you have no records it is reversed and the second details section is shown while the first details section is suppressed.
Assuming you want the "-" to appear within the subreport, put it in the subreport's report header or report footer section and set the visibility of either the text item or the section (depending on whether you want the section to appear regardless of how many records are returned) to be conditionally suppressed on a formula like:
Count ({Table.Field})>0
This is one way to do it. Create a shared variable that tracks row count of sub report and then displays a text field if that count is zero.
From memory on shared fields it goes something like this... create a formula field with this code and then put that formula somewhere on the report. You can hide it so it's not visible.
shared numberVar rowcount := 0;
To set the shared variable equal to the number of rows in the sub-report. Do the same thing, (create a formula field in the sub report) but like this:
shared numberVar rowcount := <number of rows>;
There is code all over on different ways to get number of rows.
Then back on your main report make a text field that contains "-" and have it suppressed when the shared variable is greater than 0. You can also right click the subreport and set it to suppress when blank if you want.
Finally, make sure and put the text field "-" (with the suppress function) below the sub-report because Crystal won't know how many records are in the sub-report until after it tries. Crystal is funny that way.
See Crystal Reports: Display a Message When Report Has No Data
The Best way I can Suggest You is to play around with crystal reports for solutions, from your question You want to display "-" when no data present, So here is the answer:
First:1 Create an New Formula In the Sub-Report and Use editor and write the following Formula:
shared stringvar myvar;
if isnull({Id_colounm_name}) then myvar := "-"
else myvar:="+"
Note: here Id_coloumn_name is the row member which can decide the records are empty.
Second 2:
Save the formula and create a new page header section and suppress it, then add this formula to the newly created and suppressed section.
Third 3:
Go to the main report and create a new Formula, use the editor to write the following formula:
shared stringvar disp;
Save and close the formula editor.
Fourth 4:
Now insert a section below the section in which your crystal report lies.
Now go to section expert by right clicking on the section
and select formula icon right after the "Suppress" and add the following formula:
shared stringvar myvar;
if myvar <> "-" then true
else false
Save and close the formula and select the icon right after "Suppress Blank Section" and add the following formula:
shared stringvar myvar;
if myvar="+" then true
Save and close the formula, Now add the formula created in the main report to the newly created section. Save it.
Fifth 5:
Right click on the sub report and select "format object" and then select "Subreport" tab and check "Supress Blank Subreport"
and then right click on the section where your sub report lies and then goto "section expert" and then check "supress blank section"
Thats all you can do, Now run the report and check if its working or not
Note Note Note: The section containing the sub report should not contain any other text of other fields and the section containing the formula in the subreport has to be supressed and section containing the formula in the main report doest have to be suppressed.
Hope it helps
Thanks and Regards
Srikanth

Retrieve record count of subreport in Crystal Reports

How can I retrieve the record count of a subreport from within the main report?
I think this is a dupe, but I'll answer anyway. :)
You can create a formula field on the subreport which contains a shared variable. In the formula you'll set the shared variable to the value of the rowcount field in the subreport.
Then in your main report you will need to create a formula with a shared variable that has the same name as the subreport and then return the value of the shared variable.
Here are some links that may help.
http://www.datamanagementgroup.com/Resources/TrainersTalk/trainerstalk_howto_share_subreport_data_with_main_report.asp
http://www.ozgrid.com/forum/showthread.php?t=19034
Put this formula in your subreport. You can suppress it from display if you like.
whileprintingrecords;
Shared numbervar SubRecordCount:=(however you want to count the records in the report);
Put this in your main report. Again, this can also be suppressed.
whileprintingrecords;
shared numbervar SubRecordCount;
SubRecordCount
Use the formula name for #2 for whatever calculations you need.
IMPORTANT: Due to the fundamental logic of Crystal, you can only use this field if it is BELOW it's subreport.
Also, as for counting the records in the subreport, I recommend a running total at the bottom of it.
here you can retrive sub report count from code side
===========================
CrystalDecisions.CrystalReports.Engine.ReportDocument RepDoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
RepDoc.Load(ReportName);
int IntRepCount = RepDoc.Subreports.Count;
===========================
pass this count to your report