Click here to Skip to main content
15,906,816 members
Articles / Programming Languages / C++
Article

Displaying Crystal Report 8.0 reports in Visual C++ 6.0

Rate me:
Please Sign up or sign in to vote.
3.08/5 (17 votes)
19 Apr 20053 min read 167.9K   2.2K   30   39
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:

Image 1

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:

Image 2

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:

Image 3

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:

Image 4

Accept the default CCrystalCtrl and CRowCursor and click OK and then Close. This will add the Crystal Report control to your toolbar. Image 5 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


Written By
Web Developer
Kenya Kenya
Peter is a fresh graduate from University of Ghana Accra West Africa. He is currently on the verge of Ending his contract with Advanced Information Technology Institute (Ghana-India Kofi Annan centre of Excellence in ICT)He was once a teaching asist. of University of Ghana West Africa.

His programming knowledges includes C/C++, MFC, SDK/Win32 .Net Platform and a little COM and c#.

Peter was born in Ghana, a village called Agbozume in the Volta Region. He is a guy who believes in making Impossibilities Posible

Comments and Discussions

 
Generalcrystal report Pin
lallaba10-Nov-05 0:52
lallaba10-Nov-05 0:52 
QuestionHow to choose certain field in mysql input by user in visual C++/MFC and display in Crystal report? Pin
bryan52321-Sep-05 22:11
bryan52321-Sep-05 22:11 
QuestionCannot open the database Pin
bryan52314-Sep-05 22:13
bryan52314-Sep-05 22:13 
AnswerRe: Cannot open the database Pin
Peter Mensah17-Sep-05 6:11
Peter Mensah17-Sep-05 6:11 
QuestionRe: Cannot open the database Pin
bryan52318-Sep-05 19:00
bryan52318-Sep-05 19:00 
GeneralSimple Question Pin
Member 201604313-Sep-05 23:39
Member 201604313-Sep-05 23:39 
GeneralRe: Simple Question Pin
Peter Mensah14-Sep-05 9:22
Peter Mensah14-Sep-05 9:22 
Generalinclude the Crystal report control in my Registered ActiveX Control wizard Pin
shinyhui24-Aug-05 17:06
shinyhui24-Aug-05 17:06 
How can i include the Crystal report control in my Registered ActiveX Control wizard?
GeneralRe: include the Crystal report control in my Registered ActiveX Control wizard Pin
Peter Mensah25-Aug-05 8:07
Peter Mensah25-Aug-05 8:07 
GeneralRe: include the Crystal report control in my Registered ActiveX Control wizard Pin
song thuong29-Aug-05 22:26
song thuong29-Aug-05 22:26 
GeneralRe: include the Crystal report control in my Registered ActiveX Control wizard Pin
Peter Mensah30-Aug-05 2:37
Peter Mensah30-Aug-05 2:37 
GeneralError 'E' :confused: Pin
M AZZAAAM19-Apr-05 21:12
M AZZAAAM19-Apr-05 21:12 
GeneralRe: Error 'E' :confused: Pin
Peter Mensah17-May-05 10:06
Peter Mensah17-May-05 10:06 
GeneralRe: Error 'E' :confused: Pin
GusD14-Feb-06 9:41
GusD14-Feb-06 9:41 
GeneralRe: Error 'E' :confused: Pin
Ashish Ranjan Mishra13-Mar-12 9:31
Ashish Ranjan Mishra13-Mar-12 9:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.