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()
.
- First and foremost,
#include �CrystalCtrl.h�
to the .CPP file and implement the function as follows: Void CDisplay_Crystal_RPTDly::OnDisplay()
{
CCrystalCtrl *m_cryscontrol =
( CCrystalCtrl*)( GetDlgItem(IDC_CRYSTALREPORT1));
CString str;
str = "{Table1.ID} = 1";
m_cryscontrol->SetReportFileName(�Report1.rpt�);
m_cryscontrol->SetSelectionFormula(str);
m_cryscontrol->SetDiscardSavedData(TRUE);
m_cryscontrol ->SetAction(TRUE);
}
- 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);
CCrystalCtrl *m_cryscontrol =
( CCrystalCtrl*)( GetDlgItem(IDC_CRYSTALREPORT1));
CString str;
str = "{Table1.Name} = \"" +m_StudentName+"\"";
m_cryscontrol->SetReportFileName(�C:\\Display_CrystalRPT\\Report1.rpt�);
m_cryscontrol->SetSelectionFormula(str);
m_cryscontrol->SetDiscardSavedData(TRUE);
m_cryscontrol ->SetAction(TRUE);
UpdateData(FALSE);
}