首先做的:
1.在工程中添加CRViewer9控件.作为浏览报表的控件 对应CRViewer9.dll文件
2.在"引用..."中添加Crystal Reports 9 ActiveX Designer Run Time Library 对应CRAXDRT9.dll文件
3.在窗口中添加Crviewer9控件,Name为:CRViewer91
先看静态连接:
Private Sub Form_Load()
Private objcrapp As New CRAXDRT.Application
Private objcrreport As New CRAXDRT.Report
'读取水晶报表文件
Set objcrreport = objcrapp.OpenReport("水晶报表文件路径",1)
'设置报表源
CRViewer91.ReportSource = objcrreport
CRViewer91.ViewReport
End Sub
动态连接:
1.要知道水晶报表的推模式,在水晶报表中,数据库专家->选择"仅字段定义"(如果没看到此项,说明水晶报表没有安装完全)->新建文件->增加字段(字段名和类型要与数据库中一致)->保存.ttx文件.这其实就是提供一个框架,数据是通过vb给它的.
2.建立一个模块.连接数据库用的.
'模块------------------------------
Public rs As New ADODB.Recordset
Public objcrapp As New CRAXDRT.Application
Public objcrreport As New CRAXDRT.Report
Public Sub open_db(strsql)
Dim conn As New ADODB.Connection
conn.Provider = "sqloledb"
conn.Properties("data source").Value = "127.0.0.1" '本机
conn.Properties("initial catalog").Value = "gs_get" '数据库名
conn.Properties("integrated security").Value = "SSPI" '登陆类型:集成
conn.Open
With rs
.ActiveConnection = conn
.CursorLocation = adUseClient
.Open strsql
End With
End Sub
Public Sub close_db()
Set rs = Nothing
End Sub
'-------------------------------------------
3.接着就是from了
Private Sub Form_Load()
Dim i As Integer
Dim props As CRAXDRT.ConnectionProperties
Dim prop As CRAXDRT.ConnectionProperty
Dim strsql As String
'查询语句
strsql = "select * from MANL_TBL "
open_db (strsql)
Set objcrreport = objcrapp.OpenReport(App.Path & " report1.rpt",1)
''''''''''加载ttx,可不用
'Set props = objcrreport.Database.Tables(1).ConnectionProperties
'For Each prop In props
'If InStr(prop.Name,"field definition file") > 0 Then
' prop.Value = App.Path & " pro.ttx"
' Exit For
'End If
'Next
'Set props = Nothing
'------------------------
objcrreport.DiscardSavedData
objcrreport.EnableParameterPrompting = False
For i = 1 To 2
objcrreport.ParameterFields(i).ClearCurrentValueAndRange
Next
'给报表中的变量赋值,不是字段
objcrreport.ParameterFields(1).AddCurrentValue ("Global Soft")
objcrreport.ParameterFields(2).AddCurrentValue ("wdtking")
'设置报表数据源
objcrreport.Database.SetDataSource rs
'设置报表的外观
objcrreport.LeftMargin = 10
objcrreport.RightMargin = 10
CRViewer91.EnableExportButton = True
CRViewer91.EnableSelectExpertButton = False
CRViewer91.DisplayGroupTree = False
CRViewer91.EnableAnimationCtrl = False
CRViewer91.EnableCloseButton = False
CRViewer91.EnableGroupTree = False
CRViewer91.EnableHelpButton = False
CRViewer91.EnableRefreshButton = False
CRViewer91.EnableNavigationControls = False
CRViewer91.EnablePopupMenu = False
CRViewer91.EnableSearchControl = False
CRViewer91.EnableSearchExpertButton = False
CRViewer91.EnableSelectExpertButton = False
CRViewer91.DisplayTabs = False
'------------------------------------
CRViewer91.ReportSource = objcrreportCRViewer91.ViewReportEnd Sub