Click here to Skip to main content
Licence 
First Posted 19 Apr 2005
Views 92,236
Bookmarked 25 times

Displaying Crystal Report 8.0 reports in Visual C++ 6.0

By | 19 Apr 2005 | Article
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

About the Author

Peter Mensah

Web Developer

Kenya Kenya

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralError "Unable to load report" in Displaying Crystal Report [modified] Pinmembertranthuyet16:41 14 Dec '10  
QuestionCrystal Report PinmemberMember 8320864:04 16 Feb '10  
Questionhow should i add records in database Pinmemberrelston123450:32 27 Dec '09  
Questionhow should i create crystal reports in VC++ Pinmemberrelston123450:29 27 Dec '09  
Questionwhich version of crystal report is supported by windows 2000 professional Pinmemberkokilag19:28 16 Jul '09  
GeneralDump data on crystal report PinmemberAamir Khan Jadoon8:01 27 Feb '09  
Generalpush technology Pinmemberjalsa G23:48 16 Jul '08  
QuestionHow to exoprt crystal report? Pinmemberm_kurkute2:00 26 Jun '08  
AnswerRe: How to exoprt crystal report? PinmemberMember 375611123:24 3 Feb '09  
QuestionCan Not Find Database DLL PinmemberNalima Banu23:30 2 Apr '08  
Questiondevelopment of Crystal Report Application in VC++6 Pinmemberitsrash19:56 4 Mar '08  
NewsReportMAX Beta Pinmemberemadns1:02 17 Aug '07  
QuestionVC++ application by using Crystal Reports 11 Pinmemberrameshbabu_peddapalli12:36 20 Jun '07  
Generalplease help me how to download crystal reports 8.0 for vc++6.0 Pinmemberrameshbabu_peddapalli11:15 19 Jun '07  
GeneralDump data on crystal report Pinmemberblacklife_852:09 8 Mar '07  
QuestionDump data on crystal report PinmemberAamir Khan Jadoon0:36 9 Apr '07  
GeneralI've got an error pls help me PinmemberKronoX™3:32 14 Feb '06  
GeneralRe: I've got an error pls help me PinmemberKronoX™8:57 14 Feb '06  
Questionhow about Crystal Report 11? PinmemberICE_WIZARD14:57 20 Dec '05  
Generalcrystal report in vc++ Pinmemberlallaba0:34 29 Nov '05  
GeneralRe: crystal report in vc++ PinmemberPeter Mensah4:48 1 Dec '05  
GeneralRe: crystal report in vc++ Pinmemberapurv1460:56 15 Dec '10  
Generalcrystal report Pinmemberlallaba0:52 10 Nov '05  
QuestionHow to choose certain field in mysql input by user in visual C++/MFC and display in Crystal report? Pinmemberbryan52322:11 21 Sep '05  
QuestionCannot open the database Pinmemberbryan52322:13 14 Sep '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120515.1 | Last Updated 19 Apr 2005
Article Copyright 2005 by Peter Mensah
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid