Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Displaying Crystal Report 8.0 reports in Visual C++ 6.0

0.00/5 (No votes)
19 Apr 2005 1  
Displaying Crystal Report 8.0 reports in Visual C++ 6.0

Introduction

In this tutorial, we are going to create a simple dialog based application. We will write a handler for a button to display our Crystal Reports report for us. I am going to assume that you have Crystal Report 8.0 installed in your system and that you have created a report named Report1.rpt and has copied it into your working folder. I use an ODBC connection to my database: DSN = (CRyS). And I also assume that you have a good knowledge in C++ and you are an absolute beginner to Visual C++. The first thing you need to do is load up your Visual C++ 6 software and create a new project.

Creating a new project

To create a new project, start by clicking on the File menu of VS 6 window, then on New. The view should look like the image below:

Specify Display_Crystal_RPT as the project name and choose an MFC AppWizard (EXE) from the list of the available Wizards, then click OK. The MFC Application Wizard will start.

  • In step 1 of the wizard, click on Dialog based option, then click Next.
  • In steps 2 and 3, accept the default settings, just click Next to do this.
  • In step 4 click Finish, then you will get the project information. Click OK.

The Dialog based application will be created for you as shown below:

On the Controls toolbar, select the Button control. Click the dialog box near the upper left corner. The Button appears on the dialog box, with the default text Button1.

Change the Caption of the new button to Display and its ID to IDC_DISPLAY, then arrange the controls on the dialog box nicely. Now go to the Project menu on the menu bar, Add to Project, and to Component as shown below:

Change the caption for each static text control as shown below. Change the ID in Edit Properties for the first edit box control to IDC_FIRSTNAME and the second edit control to IDC_LASTNAME.

This will bring the Component and Control Gallery dialog box. Double click Registered ActiveX Controls. Scroll through to select the Crystal Report Control and click Insert. A message box will appear, click on OK and you will see a dialog box as follows:

Accept the default CCrystalCtrl and CRowCursor and click OK and then Close. This will add the Crystal Report control to your toolbar. Click on this icon and click the dialog. Now double click on the button on the dialog and accept the member function OnDisplay().

  1. First and foremost, #include �CrystalCtrl.h� to the .CPP file and implement the function as follows:
    Void CDisplay_Crystal_RPTDly::OnDisplay()
    {
      // by default the ID of the CCrystalCtrl  is  IDC_CRYSTALREPORT1
    
      CCrystalCtrl *m_cryscontrol = 
                   ( CCrystalCtrl*)( GetDlgItem(IDC_CRYSTALREPORT1));
      CString str;
      str = "{Table1.ID} = 1";
      // where Table1 and ID is a ID and a field in your Database
    
    
      m_cryscontrol->SetReportFileName(�Report1.rpt�);
      m_cryscontrol->SetSelectionFormula(str);
      m_cryscontrol->SetDiscardSavedData(TRUE);
      m_cryscontrol ->SetAction(TRUE); 
    }
  2. You can also select a record at a time by putting an edit box on the form and giving it a member variable name of type that corresponds to the data type of the field in the database, say m_StudentName which is of type CString because the data type of name in the database is Text.

    And then replace str with:

    str = "{Table1.Name} = \"" +m_StudentName+"\"";

    as follows:

    Void CDisplay_Crystal_RPTDly::OnDisplay()
    {
      UpdateData(TRUE);
      // by default the ID of the CCrystalCtrl  is  IDC_CRYSTALREPORT1
    
      CCrystalCtrl *m_cryscontrol = 
                   ( CCrystalCtrl*)( GetDlgItem(IDC_CRYSTALREPORT1));
      CString str;
      str = "{Table1.Name} = \"" +m_StudentName+"\"";
      // where Table1 and Name is a name and a field
    
      //in your Database, m_StudentName is a member variable of an edit box.
    
     
      m_cryscontrol->SetReportFileName(�C:\\Display_CrystalRPT\\Report1.rpt�);
      m_cryscontrol->SetSelectionFormula(str);
      m_cryscontrol->SetDiscardSavedData(TRUE);
      m_cryscontrol ->SetAction(TRUE); 
      UpdateData(FALSE);
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here