Wednesday, January 30, 2013

Crystal Reports: Suppress a subreport, if it has no data

 

A. Introduction

In this article, I would like to explain how to hide / suppress a subreport, if it contains no data. This could be the case when you would like to avoid scenarios like having the header of a subreport displayed announcing some details to follow, but then there is NOTHING there to show. Or another inconvenient output is – for instance - having the subreport reserving a certain space on the main report's sheet that is simply BLANK.

Well, don’t worry… The fix is very quick and simple :)
The example used in this article is based on a simple database table called BRANCHES consisting of three columns, namely Branch ID, Branch Name and Branch Address. Our subreport just displays the content of that table.

So, let us see what happens, when the DB table gets empty? How would the subreport as well as the main report (the parent) behave?

 

B. Database preparations

Create the example database table and fill it with data using the following script:

create table BRANCHES(
Branch_ID varchar(6) not null,
Branch_Name varchar(20) not null,
Branch_Address varchar(100) not null,
constraint pk_branches primary key (Branch_ID)
);

insert into BRANCHES values
('1111', 'Sydney', 'xxx - yyy, Sydney'),
('2222', 'Perth', 'aaa - bbb, Perth'),
('3333', 'Canberra', 'kkk - lll, Canberra');

 

C. Report Creation

  1. Create a new blank report and add 2 new Report Header sections.
  2. In Report Header a, add a Text Object and write any text that marks that you are BEFORE the subreport section.
  3. In Report Header b, add a Subreport and call it – for instance – Branches.
  4. In Report Header c, add a Text Object and write any text that marks that you are AFTER the subreport section.

    pic_01
  5. Now double-click the Branches subreport and edit it by writing any text in the Report Header section and adding the 3 columns of the Branches table to the Details section.

    pic_02
  6. The report is ready for testing. Click the Preview button and you should see the data of the three branches.

    pic_03
  7. So far so good. Now delete all the data of the Branches table from the database using this command: delete * from Branches;
  8. And now regenerate the report’s Preview. Can you see the problem?

    pic_04

 

D. Let’s fix it

  1. Double-click the Branches subreport.
  2. Go to File > Report Options.
  3. Select the option Suppress Printing if No Records.

    pic_05
  4. Now preview the report. As there are no records to display, the subreport has suppressed itself. BUT the main report is still reserving a space for the subreport.

    pic_06
  5. Right-click the subreport object and select the command Format Subreport.
  6. Go to the Subreport tab and select the option Suppress Blank Subreport.

    pic_07
  7. Now preview the report. The main report realizes that the subreport is empty and therefore it suppresses the whole subreport object. BUT still the section containing the subreport is showing on the screen!!!

    pic_08
  8. Go to Report > Section Expert.
  9. Select the section containing the subreport (in our example, it is Report Header b) and select the option Suppress Blank Section.

    pic_09
  10. Now preview the report. Finally all the traces of the subreport has been removed Smile

    pic_10

22 comments:

  1. Hi,

    I've just started a new project with Crystal Reports. It is a new tool for me, however, your articles made the start-up easy.
    Especially this one, I liked the simplified step by step screen-shots that make things clearer.

    THANK YOU SO MUCH. Looking forward to reading extra articles about Crystal Reports.

    ReplyDelete
    Replies
    1. Hello,

      Thank you very much for your nice comment. I am glad to know that the article was of help to you.

      Please feel free to send any problems or tricky issues you face with Crystal Reports and I would be glad to try and resolve it together with you.

      Good luck with your new project :)
      Samar

      Delete
  2. Good article. I faced the same problem and took me so long to fix it. Solution couldn't be explained any easier than that.

    ReplyDelete
    Replies
    1. Hi there,

      Thank you very much for your nice comment. I am glad to know that you like the article.

      Have a nice day,
      Samar

      Delete
  3. Hi, I am wondering how I would be able to put a filter on, so that if my second group header is suppressed, to also suppress the header above it?
    I'm writing a report that has a list of parts, I've put a filter in my subreport so that it will only display if certain aspects are in the part. I then placed the subreport into my overall report which is organized by parts. But the issue I run into, is even with the subreport suppressed, it won't also suppress the part in the first group header. Any suggestions.

    I greatly appreciate your help!

    Natalie

    ReplyDelete
    Replies
    1. Hi Nathalie,

      If I understand your problem correctly, it should be very easy to fix it by following these steps:
      1 - Take the filter that you have applied on your subreport, in order to show/hide it.
      2 - Now go to Report > Section Expert.
      3 - Select the section that you would like to conditionally show/hide. ( I think in your example, it is the first group header )
      4 - Now select the option "Suppress", then click the "Formula" button right beside it. This opens the "Formula Editor" window.
      5 - Here you should add the filter that you have applied before on your subreport, in order to show/hide it.
      6 - What this "Suppress" option does is the following: If the formula in this editor results in TRUE, the section will be suppressed. Otherwise, the section will be displayed.
      7 - Now save the formula and save the changes in the Section Expert and test.

      I hope this helps.

      Best regards,
      Samar

      Delete
  4. hi Samar

    Good Article very helpful for me

    thanks

    Regards

    R.S.Saravanan

    ReplyDelete
    Replies
    1. Hi R.S. Saravanan,

      I'm very glad to know that the article was helpful for you.

      Thanks for your nice comment,
      Samar

      Delete
  5. thanks Samar very useful suggestion !tHANKS TO U AND YOUR HELPFUL BLOG!

    ReplyDelete
    Replies
    1. Hi Abhishek,

      Thank you very much for your nice comment. I am glad my article was of help to you.

      Best regards,
      Samar

      Delete
  6. Very good instructions but I still have an issue with my subreport. The only other option I have for the section where the subreport exists is new page before. Even though the subreport is blank a new page is created. Any ideas?

    ReplyDelete
    Replies
    1. Hi there,

      Well, here are my thoughts, although they still don't fix the problem:
      - Create a formula field in the subreport to count the records:
      WhilePrintingRecords;
      Shared NumberVar RecordsCount;
      RecordsCount := RecordsCount + 1;
      - Place this field in the Details section and suppress it
      - In the main report, create this formula field
      Shared NumberVar RecordsCount;
      RecordsCount
      - Note that this field should be placed in a section beneath the section where the subreport was placed. This makes sure that Crystal Report reads the formula in the subreport first before it reads the formula in the main report.
      - In the Section Expert, add a formula to the "New Page Before" option:
      Shared NumberVar RecordsCount;
      RecordsCount > 0

      This solution fixes the prblem of the extra blank page, in case the subreport has no data.
      But the problem is that in case the subreport has data, the blank page is printed after the subreport and not before it, because we had to place the formula field after the section containing the subreport.

      Anyway, I hope this idea could be a good start for you. If you find a solution, it would be nice of you to post it back.

      Thanks and my best regards,
      Samar

      Delete
  7. Hi, Is there anyway to suppress the WHOLE Report in total and not print a blank page if there is no data returned. i.e. Suppress everything, and do not send a blank page to the printer, create no output file, and no print job !!... is it possible?

    ReplyDelete
    Replies
    1. Hi Alan,
      I don't think, it is possible to create no output, in case of no data.

      If the Crystal Report is called from within an application, I suggest that you check the results on the application level first. In case, there is no data, you can - for instance - display a warning message and don't call the Crystal engine. Otherwise, Crystal is called and the report is generated and printed.

      What do you think?

      Best regards,
      Samar

      Delete
  8. Thank you for posting this very useful trick miss Samar, much appreciated

    Frank Sito, Sacramento CA.

    ReplyDelete
    Replies
    1. Hi Frank,
      I am glad to know that it was of help to you.
      Have a great day,
      Samar

      Delete
  9. Hello Samar,

    Can I know how to Suppress Data IF user click on print button for the first time?

    http://stackoverflow.com/questions/26666957/how-to-suppress-data-if-user-click-on-print-button-for-the-first-time

    ReplyDelete
    Replies
    1. Hello,

      I have checked your question on stackoverflow and first I would like to comment on the use of "print state" then I would like to suggest a solution for your problem.

      Print State:
      Print State is not a function. It is a category containing a variety of functions that generally pertain to the current state of the report when being formatted or printed. Examples of thes functions are: OnFirstRecord, Next, Previous, PageNumber....etc

      As for your question concerning the signature fields:
      - The Crystal Report being printed wouldn't know that it is the first time it is being printed.
      - This piece of information would usually come from the application that is calling the Crystal Report engine.
      - So you have to pass a parameter to your Crystal Report informing it whether this is the first time it is being printed or not.

      Here are the steps:
      1. Create a report parameter that indicates whether it is the first time to print the report or not (example: isFirstPrint)
      2. Add the signature fields in a separate section
      3. Go to the Section expert, select the section containing the signature fields.
      4. For this section set the checkbox "Suppress" and add the formula "isFirstPrint". This way, if isFirstPrint is TRUE, this section gets suppressed. Otherwise, this section is displayed.

      Does that answer your question?

      Best regards,
      Samar

      Delete
    2. <--Here are the steps:
      1. Create a report parameter that indicates whether it is the first time to print the report or not (example: isFirstPrint)-->

      How to set this report parameter? from CR itself or from database?

      Delete
    3. This parameter should be set externally not from within the report.

      I assume there is an application that calls the Crystal Report. Is that right?
      If not, how do the end users print the CR that you are developing?

      Delete
  10. Very helpful! Thank you!

    ReplyDelete