sam
sam

Reputation: 277

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

Upvotes: 1

Views: 13273

Answers (5)

jac
jac

Reputation: 9726

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.

Upvotes: 5

user5049245
user5049245

Reputation:

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

Upvotes: 0

user359040
user359040

Reputation:

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

Upvotes: 1

RThomas
RThomas

Reputation: 10880

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.

Upvotes: 0

Related Questions