You are on page 1of 6

Lab 6_4 Creating Reports by Using Crystal Reports

I. Creating Project
1. Creating a new Visual Basic Windows Application project named UsingCrystalReport.
2. Rename Form1.vb to frmMain.vb.
3. Design frmMain.vb look like:

Trang 1
Lab 6_4 Creating Reports by Using Crystal Reports

4. Add new form to project then name it to frmStatus.vb


5. Design frmStatus.vb look like:

II. Writing code:


1. Right click frmMain.vb then choose “View Code”
2. Type the following code on the top of the form:

Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine

3. Declare these variables:

Protected Const SQL_CONNECTION_STRING As String = "Persist Security _


Info=False;Database=DN_IAS;Server=Softdev01;User=dn_iassa;Password=sa;Connect _
Timeout=30"

Private ConnectionString As String = SQL_CONNECTION_STRING


Private ServerName As String = "Softdev01"
Trang 2
Lab 6_4 Creating Reports by Using Crystal Reports
Private HasConnected As Boolean = False
Private Const TABLE_NAME As String = "PSTM"

Dim dsPhieu As New DataSet


Dim dvPhieu As DataView

4. Write the following sub to create a connection to database :

Private Sub KetNoi()


crvDanhmuc.DisplayToolbar = True
crvThamso.DisplayToolbar = True
crvPhuc.DisplayToolbar = True
Dim cnSQL As SqlConnection

Dim frmStatusMessage As New frmStatus


If Not HasConnected Then
frmStatusMessage.Show("Connecting to SQL Server")
End If

Dim IsConnecting As Boolean = True


While IsConnecting
cnSQL = New SqlConnection(ConnectionString)
Try
cnSQL.Open()
Catch ex As SqlException
MsgBox("Lỗi kết nối CSDL! Chi tiết lỗi: " & vbNewLine & ex.Message, _
MsgBoxStyle.Critical, "Thông báo lỗi")
Throw ex
End Try
IsConnecting = False
frmStatusMessage.Close()
cnSQL.Close()
cnSQL.Dispose()
End While
End Sub

5. Write the following sub to bind data to combobox:

Private Sub BindToDataSet()


Try
LoadData()
cbPhieu.DataSource = dvPhieu
cbPhieu.ValueMember = "Idkey"
Catch exp As Exception
MessageBox.Show(exp.Message, Me.Text)
End Try
End Sub

Trang 3
Lab 6_4 Creating Reports by Using Crystal Reports

Private Sub LoadData()


Dim PhieuConnection As New SqlConnection(SQL_CONNECTION_STRING)
Dim PhieuAdapter As New SqlDataAdapter("SELECT Idkey FROM PSTM", _
PhieuConnection)
PhieuAdapter.Fill(dsPhieu, TABLE_NAME)
dvPhieu = New DataView(dsPhieu.Tables("PSTM"), "", "Idkey Asc", _
DataViewRowState.OriginalRows)
End Sub

6. On the frmMain_load even type the following code:

BindToDataSet()

7. Open “Windows Form Designer generated code”, in Sub New type the following code:

KetNoi()

8. Double click “Danh mục đối tượng” then type the following code:

Dim tbCurrent As CrystalDecisions.CrystalReports.Engine.Table


Dim tliCurrent As CrystalDecisions.Shared.TableLogOnInfo
Dim rptDanhmuc As New ReportDocument
Try
rptDanhmuc.Load("..\DMTK.rpt")
For Each tbCurrent In rptDanhmuc.Database.Tables
tliCurrent = tbCurrent.LogOnInfo
With tliCurrent.ConnectionInfo
.ServerName = ServerName
.UserID = "dn_iassa"
.Password = "sa"
.DatabaseName = "DN_IAS"
End With
tbCurrent.ApplyLogOnInfo(tliCurrent)
Next tbCurrent

rptDanhmuc.SetParameterValue("sCompanyName", "Trung Tâm Tin Học- DHKHTN")


rptDanhmuc.SetParameterValue("sUnitName", "CSC - TATA")
crvDanhmuc.ReportSource = rptDanhmuc
crvDanhmuc.Zoom(4)

Catch Exp As LoadSaveReportException


MsgBox("Không đúng đường dẫn", MsgBoxStyle.Critical, "Không tìm thấy báo cáo")
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
End Try

Trang 4
Lab 6_4 Creating Reports by Using Crystal Reports
9. Double click “Liệt kê chứng từ theo phiếu” then type the following code:

Dim tbCurrent As CrystalDecisions.CrystalReports.Engine.Table


Dim tliCurrent As CrystalDecisions.Shared.TableLogOnInfo
Dim rptPhieu As New ReportDocument
Try
rptPhieu.Load("..\TMVP01_THUCHI.rpt")
For Each tbCurrent In rptPhieu.Database.Tables
tliCurrent = tbCurrent.LogOnInfo
With tliCurrent.ConnectionInfo
.ServerName = ServerName
.UserID = "dn_iassa"
.Password = "sa"
.DatabaseName = "DN_IAS"
End With
tbCurrent.ApplyLogOnInfo(tliCurrent)
Next tbCurrent

rptPhieu.SetParameterValue("@pidkey", cbPhieu.Text)
rptPhieu.SetParameterValue("sCompanyName", "Trung Tâm Tin Học - DHKHTN")
rptPhieu.SetParameterValue("sUnitName", "CSC - TATA")

crvThamso.DisplayGroupTree = False
crvThamso.ReportSource = rptPhieu
crvThamso.Zoom(4)

Catch Exp As LoadSaveReportException


MsgBox("Không đúng đường dẫn", MsgBoxStyle.Critical, "Không tìm thấy báo cáo")
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
End Try

10. Double click “Liệt kê chứng từ” then type the following code:

Dim tbCurrent As CrystalDecisions.CrystalReports.Engine.Table


Dim tliCurrent As CrystalDecisions.Shared.TableLogOnInfo
Dim rptLietke As New ReportDocument
Try
rptLietke.Load("..\THVP06_LIETKECTTM.rpt")
For Each tbCurrent In rptLietke.Database.Tables
tliCurrent = tbCurrent.LogOnInfo
With tliCurrent.ConnectionInfo
.ServerName = ServerName
.UserID = "dn_iassa"
.Password = "sa"
.DatabaseName = "DN_IAS"
End With
Trang 5
Lab 6_4 Creating Reports by Using Crystal Reports
tbCurrent.ApplyLogOnInfo(tliCurrent)
Next tbCurrent

rptLietke.SetParameterValue("vngaythang", CDate(Tungay.Text))
rptLietke.SetParameterValue("@Tungay", CDate(Tungay.Text))
rptLietke.SetParameterValue("@Denngay", CDate(Denngay.Text))
rptLietke.SetParameterValue("vtongcongty", "Trung Tâm Tin Học- DHKHTN")
rptLietke.SetParameterValue("vtencty", "CSC - TATA")
rptLietke.SetParameterValue("@dsdonvi", "*")
rptLietke.SetParameterValue("@loaibc", "CT")
rptLietke.SetParameterValue("@Loaict", "TH")
crvPhuc.DisplayGroupTree = True
crvPhuc.ReportSource = rptLietke
crvPhuc.Zoom(4)

Catch Exp As LoadSaveReportException


MsgBox("Không đúng đường dẫn", MsgBoxStyle.Critical, "Không tìm thấy báo cáo")
Catch Exp As Exception
MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
End Try

11. Right click frmStatus.vb then choose “View Code”


12. Type the following code:

Public Overloads Sub Show(ByVal Message As String)


lblStatus.Text = Message
Me.Show()
Application.DoEvents()
End Sub

13. Build and Run the project.

Trang 6

You might also like