Click here to Skip to main content
15,891,762 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.4K   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

 
QuestionThank you! Pin
Member 107160287-Apr-14 13:06
Member 107160287-Apr-14 13:06 
QuestionHow can I pass int value in CEdit. Pin
gilberto.t00118-May-12 5:53
gilberto.t00118-May-12 5:53 
GeneralError "Unable to load report" in Displaying Crystal Report [modified] Pin
tranthuyet14-Dec-10 16:41
tranthuyet14-Dec-10 16:41 
QuestionCrystal Report Pin
Member 83208616-Feb-10 4:04
Member 83208616-Feb-10 4:04 
Questionhow should i add records in database Pin
relston1234527-Dec-09 0:32
relston1234527-Dec-09 0:32 
Questionhow should i create crystal reports in VC++ Pin
relston1234527-Dec-09 0:29
relston1234527-Dec-09 0:29 
Questionwhich version of crystal report is supported by windows 2000 professional Pin
kokilag16-Jul-09 19:28
kokilag16-Jul-09 19:28 
GeneralDump data on crystal report Pin
Aamir Khan Jadoon27-Feb-09 8:01
Aamir Khan Jadoon27-Feb-09 8:01 
Generalpush technology Pin
jalsa G16-Jul-08 23:48
jalsa G16-Jul-08 23:48 
QuestionHow to exoprt crystal report? Pin
m_kurkute26-Jun-08 2:00
m_kurkute26-Jun-08 2:00 
AnswerRe: How to exoprt crystal report? Pin
Hwang, Kyeong-Min3-Feb-09 23:24
professionalHwang, Kyeong-Min3-Feb-09 23:24 
QuestionCan Not Find Database DLL Pin
Nalima Banu2-Apr-08 23:30
Nalima Banu2-Apr-08 23:30 
Questiondevelopment of Crystal Report Application in VC++6 Pin
itsrash4-Mar-08 19:56
itsrash4-Mar-08 19:56 
NewsReportMAX Beta Pin
emadns17-Aug-07 1:02
emadns17-Aug-07 1:02 
QuestionVC++ application by using Crystal Reports 11 Pin
rameshbabu_peddapalli20-Jun-07 12:36
rameshbabu_peddapalli20-Jun-07 12:36 
Generalplease help me how to download crystal reports 8.0 for vc++6.0 Pin
rameshbabu_peddapalli19-Jun-07 11:15
rameshbabu_peddapalli19-Jun-07 11:15 
GeneralDump data on crystal report Pin
blacklife_858-Mar-07 2:09
blacklife_858-Mar-07 2:09 
QuestionDump data on crystal report Pin
Aamir Khan Jadoon9-Apr-07 0:36
Aamir Khan Jadoon9-Apr-07 0:36 
GeneralI've got an error pls help me Pin
KronoX14-Feb-06 3:32
KronoX14-Feb-06 3:32 
GeneralRe: I've got an error pls help me Pin
KronoX14-Feb-06 8:57
KronoX14-Feb-06 8:57 
Questionhow about Crystal Report 11? Pin
ICE_WIZARD20-Dec-05 14:57
ICE_WIZARD20-Dec-05 14:57 
Generalcrystal report in vc++ Pin
lallaba29-Nov-05 0:34
lallaba29-Nov-05 0:34 
GeneralRe: crystal report in vc++ Pin
Peter Mensah1-Dec-05 4:48
Peter Mensah1-Dec-05 4:48 
GeneralRe: crystal report in vc++ Pin
apurv14615-Dec-10 0:56
apurv14615-Dec-10 0:56 
How did you integrate Crystal report 11 with MFC application? I am not able to display my crystal report11 in MFC app.

Pl help me out...!!
Generalcrystal report Pin
lallaba10-Nov-05 0:52
lallaba10-Nov-05 0:52 

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.