Unit 8: Crystal Reports For Visual Studio 2008
Unit 8: Crystal Reports For Visual Studio 2008
Unit 8
Report
Introduction
When you install visual studio 2008 or later version, crystal reports is
automatically installed on your computer. You will find the crystal
reports application under report sub menu, when you want to create a new
project. Crystal Reports is a business intelligence application. It was
marketed to small businesses by SAP SE. It is used to design and generate
reports from a wide range of data sources. Crystal Reports has been part
of Visual Studio since 1993. It is standard tool for reporting in Visual
Studio 2008 and later version.
Lesson 8.1
Report
Upon completion of this unit you will be able to:
Outcomes
126
Visual Programming
127
Report
128
Visual Programming
Introduction
When you install visual studio 2008 or later version, crystal reports is
automatically installed on your computer. So developers can write
applications that have reports seamlessly integrated into them. You will
find the crystal reports application under report sub menu, when you want
to create a new project. Crystal Report is standard tool for reporting in
Visual Studio 2008 and later version. Visual Studio .NET is the first
Windows development environment that gives developers a fully
integrated and robust reporting solution
Outcomes
3. Add New Item dialog box appear, then from the left side categories
129
Working with Crystal Report
items, select reporting menu, then select crystal report from right side
templates, which will look like the following:
4. Now give crystal report‘s name as you want. By default crystal report
name will be CrystalReport1 and its extension will be .rpt and then
click Add button. Now Crystal Report Gallery‘s window will appear:
Create a New Crystal Report Document part consist of three options such
as
i. Using the Report Wizard
130
Visual Programming
In this book we will explain how to create standard types of report, this
types of report is standard for all software development.
Creating New Crystal Report Document using the Report wizard method
To create a New Crystal Report Document using the report wizard
method follows the following steps:
From crystal report gallery window select using the Report Wizard radio
button and also select standard option from choose expert part and then
click ok button.
Standard report creation wizard dialog will appear, which look like the
following:
131
Working with Crystal Report
Summaries Screen
Group Sorting Screen
Chart Screen
Record Selection Screen
Report Style Screen
These screens descriptions discussed in later lesson due to use of
database. In the above screen only shows the data screen which consist of
available data source option and selected tables options. This option is
required database to shows data on report, so in this section we omit these
options.
2. Now click on finish button and then you will look like the following
report window:
3. Here you will see the field explorer window, solution explorer
window, properties window and report body.
4. Report body contains five section such as :
i. Section 1(Report Header): Any data like text, image and so on
inserted in this section will be displayed in the report for one time
in the top of the first page only.
ii. Section 2(Page Header): Any data like text, image and so on
inserted in this section will be displayed in the header of each
page in the report.
iii. Section 3(Details): This section displays the data from the data
source (which is described latter lesson) containing multiple rows
of returned results like data base tables.
iv. Section 4(Report Footer): Any data like text, image and so on
inserted in this section will be displayed in the report for one time
in the end of the last page only.
v. Section 5(Page Footer): Any data like text, image and so on
inserted in this section will be displayed in the footer of each
page in the report.
132
Visual Programming
5. Now click on Main Report Preview option from the bottom of report
body you will see only your computer current date and page number,
because we did not load database information‘s.
6. Here, Field explorer window contains some fields such as
i. Database fields
ii. Formulae fields
iii. Parameter fields
iv. Group name fields
v. Running total fields
vi. Special fields
vii. Unbound fields
7. Now again click on Main Report option from bottom of report body,
so you will go back report edit option.
8. If you want to see the special fields, just click on + sign of special
fields or double click on special fields. here you will see the
following special fields:
i. Print date
ii. Print time
iii. Modification date
iv. Modification time
v. Data date
vi. Data time
vii. Record number
viii. Page number
ix. Group number
x. Total page count
xi. Report title
xii. Report comments
xiii. Report selection formulae
xiv. Group selection formulae
xv. File path and name
xvi. File author
xvii. File creation date
xviii. Page N of M
9. If you want to see the unbound fields, just click on + sign of unbound
fields or double click on unbound fields. Here you will see the
following fields:
i. Boolean
ii. Currency
iii. Date
iv. Date time
v. Number
vi. String
133
Working with Crystal Report
vii. Time
10. Now right click on section 2(Page header), and then select insert and
then select Text Object. Now you will see a rectangle box on mouse
pointer. Just click on anywhere of Section 2.
11. Now type text like ―Bangladesh Open University‖ in the rectangle
box. If the text is greater than rectangle box, just increase it using mouse
pointer. Now you will look like the following:
12. Now right click on the rectangle object and then select Format object,
Format Editor will be appeared. Now from the Format Editor window
select border tab and set the line style and color as following window, and
click ok button.
13. Now click on Main report preview option from the bottom of the
report , a border will be appeared around the text object like the
following:
134
Visual Programming
Click on Toggle Group tree menu if you want to full page preview.
If you want to reload or refresh the report just click on refresh menu and
if you want to cancel or stop loading the report, just click on stop loading
menu.
If you want to edit report, just click on Main Report option from the
bottom of report body. In this option, you can edit text, text object,
shapes, lines etc. If you want insert line, text object, box object, picture,
special field etc. just click right button on report body then select insert
option and then go to various option as you want. According to this
procedure you can design your report as your choices.
135
Working with Microsoft (MS) access Database
Create a database.
Create tables in database
Outcomes
MS Access Database
Database is a collection of data contains information‘s in a relevant
enterprise. Microsoft Access (MS access) is also a database creation
program that allows for anyone to easily maintain and edit a database. It
is suitable for anything from small projects to large businesses, and is a
very visual program. Microsoft Access has the look and feel of other
Microsoft Office products, including its layout and navigational aspects.
First of all, Microsoft released Access version 1.0 on 13 November 1992,
and then an Access 1.1 release in May 1993 to improve compatibility
with supplementary Microsoft products and to include the Access Basic
programming language. In Office 95, Microsoft Access 7.0 became a part
of the Microsoft Office Professional Suite (MOPS), joining Microsoft
Excel, Microsoft Word, and Microsoft PowerPoint and transitioning from
Access Basic to Visual Basic for Applications (VBA). After then,
Microsoft has released new versions of Microsoft Access with each
release of Microsoft Office software. This includes Access 97 (version
8.0), Access 2000 (version 9.0), Access 2002 (version 10.0), Access 2003
(version 11.5), Access 2007 (version 12.0), and Access 2010 (version
14.0).
Microsoft Access is used as the 'front-end' of a program while other
products act as the 'back-end' tables, such as Microsoft SQL Server and
136
Visual Programming
i. Tables
ii. Relationships
iii. Queries
137
Working with Microsoft (MS) access Database
iv. Forms
v. Reports
vi. Macros
vii. Modules
Tables
The tables of MS access database are the backbone and the storage
container of the data entered into the database. If the tables are not set up
correctly, with the correct relationships, then the database may be slow,
give you the wrong results or not react the way you expect. Queries,
forms, reports etc. are usually based on a table. A table contains columns
and rows. Each of the columns will have a field name at the top and each
of the rows will represent a record.
Relationships
Relationships are the bonds you build between the tables. They join tables
that have associated elements. To do this there is a field in each table,
which is linked to each other, and have the same values.
Queries
Query means of manipulating the data to display in a form or a report.
Queries can sort, group, filter, join tables, update data, delete data, sum,
average, count etc. Their power is immense. The MS access database
query language is SQL (Structured Query Language).
Forms
A form is a database object that you can use to create a user interface for
a database application. The user who enters the data will interact with
forms regularly. The programmer can set the forms to show only the data
required. A form is one that is directly connected to a data source such as
a table or query, and can be used to enter, edit, or display data from that
data source. By using queries, properties, macros and VBA as well as
visual studio applications the ability to add, edit and delete data can also
be set.
Reports
Reports are an enormous way to organize and present data from your
Microsoft Access database. Reports enable you to format your data in an
attractive and informative layout for printing or viewing on screen.
Reports are the results of the manipulation of the data you have entered
into the database. You can print report using the printer.
Macros
A macro is a tool that allows you to automate tasks and add functionality
to your forms, reports, and controls. Macro is an automatic way for
Access to carry out a series of actions for the database. Access gives you
a selection of actions that are carried out in the order you enter. In Access,
it is helpful to think of macros as a simplified programming language that
you write by building a list of actions to perform. When you build a
macro, you select each action from a drop-down list and then fill in the
required information for each action
138
Visual Programming
Modules
Modules are the basis of the programming language that supports
Microsoft Access. The module window is where you can write and store
Visual Basic for Applications (VBA) as well as visual studio applications.
Advanced users of Microsoft Access tend to use VBA or visual studio
applications instead of Macros.
3. After you click on Blank Database Template, you will see the
following option on the right bottom side of the above window:
139
Working with Microsoft (MS) access Database
4. Now click on option then, you will see the following File New
Database dialog:
5. Now select your directory from your computer, where you want save
your file, and then type your database name on the File name text
box. Here BOU is the database name, and then select Microsoft office
access database (2002-2003 format) from the save as type drop down
box, and then click ok button.
6. After completion the step 5, then click create button , and
your database file saves to the location that you specified, and opens
for you to work on.
1. Open your database ―BOU‖ from your computer directory. When you
open your database file ―BOU‖ you will look like the following:
140
Visual Programming
2. Now Select the CREATE tab on the Access ribbon. Next select the
TABLE DESIGN icon from the TABLES group. This creates a new
table, which will look like the following:
141
Working with Microsoft (MS) access Database
4. Now click on Emp_id row and then click primary key icon to
set this row as primary key. The table design grid should now look like
this:
5. Now click on save icon then save as dialog box appeared. Now put
the table name in table name text box, then click ok button. Here we put
the table name is bou_emp.
142
Visual Programming
icon from the left corner of the access ribbon, then you will look like
the following :
7. Now you enter the all fields‘ information as follows, and then click
on save icon.
8. If you want to delete any record, just click on delete option from the
ribbon and if you want to enter new record, just click on right button and
then click new record. If you want to edit a record, just click on particular
filed as well as particular record and then edit as you want. After
completion edit, delete, insert a new record, must click on save icon.
143
Connecting Crystal Report with MS Access Database
Outcomes
144
Visual Programming
After then Crystal Report Gallery window is appeared which will look
like the following:
145
Connecting Crystal Report with MS Access Database
3. Now from left side Field Explorer window, click right button of
mouse on Database Fields then select Database Expert option, which will
look like the following:
146
Visual Programming
6. Now select the OLE DB provider from the list box, that is select
Microsoft Jet 4.0 OLE DB Provider, and then click Next button, the
following window will be appeared:
7. Now in the above window, give the connection information. So put the
Database Name location or browse your database from your computer
director in the Database Name text filed. For this work you can click
147
Connecting Crystal Report with MS Access Database
button and select your database file or file location. Here we used
―BOU.mdb‖ database, which is created previous lesson. On the other
hand Database Type is selected as Access. After completion all
information‘s just click on finish button. After clicking finish button the
database tables is shown in under OLE DB (ADO) option. Here bou_emp
table is shown, which will look like the following:
8. Now double click on bou_emp table or select bou_emp table then click
just click on particular table and then click on button. Now you
will look like the following window
148
Visual Programming
11. So now, we can say that crystal report is connected with the MS
access database. In the next lesson, we have discussed that how the report
is designed with this fields of bou_emp table. Finally your project
window will look like the following:
149
Design Crystal Report Using Database Table’s fields
Outcomes
150
Visual Programming
2.Now from the Field Explorer window, drag Emp_id field and
drop it on under Section 3(Details), drag Emp_name field and drop
it on under section3 (Details) of report body and the remaining
fields are also drag and drop them on under Section3(Details)
respectively. So your project window should look like the
following:
3.Now click on Main Report Preview option from the bottom of the
report, then you will look like the following:
151
Design Crystal Report Using Database Table’s fields
5. Now click on save option for save the report. If you want to edit the
report just click on Main Report option from the bottom of report.
6.In the above screen it is shown that, Emp_id values are displayed with a
comma ( ,) like 1,001, 1,002, 1,003 and so on. Actually Emp_id field
does not contains a comma (,).So if you want to omit the comma (,)
from the Emp_id fields. Just right click on Emp_id field under
Section3(Details), then select Format Object, Format Editor will be
appeared, which will look like the following:
7.Now from the Format Editor, select (1123) option then click ok
button. After completing this task, click on save option and then
152
Visual Programming
Click on Main Report Preview, your report screen will look like
the following:
153
Design Crystal Report Using Database Table’s fields
13. After completing the above task, the windows form will look
like the following:
14. Now click on save all option from file menu and
save this project on your computer directory. Here we saved this
project as following location:
D:\BOU-book-example\report
15. Now single click on CrystalReportViewer from windows form
and select report source as like as follows:
154
Visual Programming
16. Now click on drop down menu of report source and click on
browse option, and then select your report location. Actually
CrystalReport1.rpt location is selected.
18. Now save and then click on run icon from tools bar, then
the report will be run. So you will look like the following screen:
Now if you want to print this report click on option from the
report menu.
155
Design Crystal Report Using Database Table’s fields
Outcomes
156
Visual Programming
iii. Field Screen: Use the Fields screen to select the fields you want
included in your report. It has following option:
a. Available Fields: This list displays all of the fields available for use in
your report. The list is based on the tables you selected on the Data
Screen.
b.Browse Data: Click this button to browse the data of the field that is
selected in the Available Fields list.
c. Find Field: Click this button to enter the name of a field you'd like to
locate in the Available Fields list.
d.Add or remove arrow buttons: Use the > arrow adjacent to the Fields
to Display list to add fields from the Available Fields list. Use the <
arrows to remove fields you've already added. Double arrows add or
remove all fields.
e. Fields to Display: This list displays all of the report fields presently
active in the report.
f. Up or down arrow buttons: Use the up and down arrows adjacent to
the Fields to display list to change the order of fields you've added from
the Available Fields list.
iv. Grouping Screen: Use the Group screen to specify how fields will
be grouped on the report. Creating groups is an optional step in the
wizard. It has following options:
a. Available Fields: This list displays all of the fields available for use
in your report. The list is based on the fields you chose on the Fields
screen (Report Creation Wizard) and the tables you selected on the Data
screen (Report Creation Wizard).
b. Browse Data: Click this button to browse the data of the field that is
selected in the Available Fields list.
c. Find Field: Click this button to enter the name of a field you'd like to
locate in the Available Fields list.
d. Add or remove arrow buttons: Use the > arrow adjacent to the
Group By list to add fields from the Available Fields list. Use the <
arrows to remove fields you've already added. Double arrows add or
remove all fields.
e. Group By: This list displays all of the fields you've selected for
grouping. As you add fields, they are added in a hierarchy. This hierarchy
establishes the order the groups will appear in the report. You can change
the order of the hierarchy by using the up and down arrow buttons. You
can also set the sorting order for each field you add to the Group by list.
Select a sort order from the adjacent list:
Click in ascending order to sort the values for the field selected in
the Group by list in ascending (A to Z, 1 to 9) order.
Click in descending order to sort the values for the field selected
in the Group by list in descending (Z to A, 9 to 1) order.
157
Design Crystal Report Using Database Table’s fields
the Group By list to change the order of fields you've added from the
Available Fields list.
a. Available Fields: This list displays the report fields available for
totaling in the report.
b. Browse Data: Click this button to browse the data of the field that is
selected in the Available Fields list.
c. Find Field: Click this button to enter the name of a field you'd like to
locate in the Available Fields list.
d. Summarized Fields: This list displays the total fields currently in use
in the report.
e. Add or remove arrow buttons: Use the > arrow adjacent to the
Summarized Fields list to add fields from the Available Fields list. Use
the < arrows to remove fields you've already added. Double arrows add or
remove all fields.
f. Summary options: Click a summary option for the field selected in
the Summarized list. You can choose options to total a field, or to count
its entries, and so on.
vi. Group Sorting Screen: Use the Group Sorting screen to sort groups
you created on the Grouping Screen. You can sort all groups, or you
can choose the top or bottom 5 groups. When sorting by the top or
bottom 5 groups, you can also choose the summarized field you
want to base the sorting on. This screen appears only if you've
specified a group on the Grouping Screen and a summary on the
Summaries Screen. Creating a group sort is an optional step in the
wizard. It has following options:
vii. Chart Screen: Use the Chart screen to insert a graph or chart into a
report. This screen appears with slight variations in all Report
Creation Wizards except the Mailing Labels wizard. In the Standard
Report Creation Wizard, the Chart screen appears only if you've
specified a group on the Grouping Screen and a summary on the
Summaries Screen. It has following options:
158
Visual Programming
a. Chart types: Click the type of chart you'd like to add to your
report. There are three types of chart such as Bar Chart, Line
Chart, and Pie Chart.
b. Chart title: Add a title for your chart.
c. On change of: Select the group field you want to use as a
condition for plotting values in a graph or chart. Points will be
plotted whenever the value in the field changes. You can
choose from the groups you created on the Grouping Screen.
d. Subdivided by: Use this list to choose the secondary row or
column to base your chart on. Clicking this list displays the
row/columns fields in your report that you can choose from.
e. Show summary: Use this list to select the summary field
whose values you want to display in your graph. You can
choose from the groups you created on the Summaries Screen.
159
Design Crystal Report Using Database Table’s fields
5. Now double click on bou_emp table then click next, the following
window will be appeared:
6. Now Click on + sign of bou_emp table from left side Available Fields
option, the all fields are shown in the list, and then click button
to display all fields on report, or individually double click on fields which
you want to display on report from the left side available fields option,
you will look like the following window:
160
Visual Programming
8. Here actually select that field, in which you want to create group. So
here we used dept field to create group. Now select dept field from
bou_emp table from left side Available Fields list, then click
button. Here by default the group values will be shown as ascending
order. If you want to descending order, just select in descending order
161
Design Crystal Report Using Database Table’s fields
from drop down menu of bottom in this window, the window would be
look like this:
162
Visual Programming
11. Now click Next button, the group sorting screen window will be
appeared:
12. Now select None radio button and click next button, the chart screen
163
Design Crystal Report Using Database Table’s fields
will be appeared:
13. Here, you can select any one of the three types of chart like Bar chart,
Line chart, Pie chart, if you want. Now select no Chart option and then
click next button, then record selection screen will be appeared, and then
click next. Now Report Style screen will be appeared:
164
Visual Programming
14. Now select Standard style from the Available Styles option list from
the left side, and then click finish button. Now the following report screen
will be appeared:
15. Now click on Main Report Preview option from the bottom of the
report, then you will look like the following output or report:
16. From the report screen, it is shown that, some fields information did
not display fully like Emp_id, Total salary etc. now just click on Main
report option from the bottom of the report and edit (increase the object
size) those fields which did not display fully. After edit the field object
165
Design Crystal Report Using Database Table’s fields
17. Here, the group values are shown in the Left side of the report screen
such as Exam, open school and SST. When you click particular group
option from the left side, the corresponding group values will be appeared
on the report.
18. Finally save all the documents.
166
Visual Programming
167
Parameter in Crystal Report
168
Visual Programming
9. Brows table and field: Here you can select database table as well as
corresponding fields as default values.
10. Length limit: You cans set the minimum and maximum length limits
for the parameter field.
11. Edit Mask: Used to enter an Edit Mask for string data types rather
than specifying a range of values. The Edit Mask can be any of a set
of masking characters(such as A, a, 0, 9, #, L, &, C, <or >, \ etc.) used
to restrict the values you can enter as parameter values
12. Sort Order: Enables specification of the sort order (such as
Alphabetical ascending or descending, numerical ascending or
descending, Date Time ascending or descending) and the sort field
that the parameter values are sorted on.
13. Import and Export: You can import or export the defaults values
169
Parameter in Crystal Report
2. Now from left side Field Explorer window, select Parameter Field,
and click right button on Parameter field, then click New option
which will look like the following:
4. Now put the Parameter Field Name in the Name text box, here we put
the name is ―empid‖, then put the name of Prompting text field in the
prompting text box, here we put the name is ―empid‖, then select the
value type from Value type dropdown menu, here we selected number
type value and select the Discrete values radio button from the options,
170
Visual Programming
and then click ok. Here we selected number type, because our table
bou_emp‗s field emp_id is number type value, you can also set the other
types according to table‘s field. Now create parameter field window will
look like the following:
171
Parameter in Crystal Report
172
Visual Programming
173
Parameter in Crystal Report
10. Now from the above window, Expand the Report Fields option by
clinking on + sign, and then double click on ― bou_emp.Emp_id ‖ option
and then expand the operators option by clicking on + sign, and then
expand the Comparisons option, and then double click on ― Equal(x=y)‖
option, from the right side list box,and finally double click on [?]empid
option from the report fields. The sequence of this process is shown by
using number (1,2,3,4,5,6,7) respectively. So the following formula will
be created on the text editor:
{bou_emp.Emp_id} = {?empid}
The pictorial process is shown in below:
11. Now click on Save and close option from the menu bar.
12. Save the entire Project.
13. Now click on ―parameterform.vb‖ from solution explorer, and then
double click on ―Show Report‖ button of the form.
14. Now type the following code at top of the page:
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
174
Visual Programming
crParameterFieldDefinition.ApplyCurrentValues(crParame
terValues)
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
16. Here you put your report path between ― ‖, from your computer
directory, where you saved your project as well as your reports. Here
we use our report location as follows as an example:
cryRpt.Load("D:\BOU-book-
example\report\crystal report
project\crystal report
project\CrystalReport1.rpt")
175
Parameter in Crystal Report
22. If you enter the id 1005 in the text field , and then click Show Report,
so the informations of id 1005 will be dispalyed on the report and so on.
176
Visual Programming
Assessment
Exercise
177