我用的外部文件打印. 装完水晶报表后 引用控件Crystal Report Viewer Control 9 新建一个窗口 把控件放上,控件名CRViewer91 加入以下代码: Private Sub Form_Load() printf.WindowState = 2 Dim oApp As New CRAXDRT.Application Dim oRpt As CRAXDRT.Report Dim reportName As String Screen.MousePointer = vbHourglass reportName = printName Set oRpt = oApp.OpenReport(App.Path & reportName, 1) oRpt.Database.SetDataSource op oRpt.ReadRecords CRViewer91.ReportSource = oRpt CRViewer91.ViewReport Screen.MousePointer = vbDefault End Sub Private Sub Form_Resize() CRViewer91.Top = 0 CRViewer91.Left = 0 CRViewer91.Height = ScaleHeight CRViewer91.Width = ScaleWidth End Sub '--------------------------------- 打印的代码放在另一个窗口 我的打印代码是: list1SQL = "select * from dwbak where 自动编号= " + List1.TextMatrix(row1, 8) Dim connstr As String printName = "\grbakg.rpt" If conn.State = adStateOpen Then conn.Close connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Storehouse.mdb;Persist Security Info=False" 'prtest.mdb是程序当前目录的测试Access数据库 conn.ConnectionString = connstr conn.Open conn.CursorLocation = adUseClient If op.State = adStateOpen Then op.Close op.Open list1SQL, conn, adOpenKeyset, adLockReadOnly If op.RecordCount > 0 Then printf.Show Else MsgBox ("没有数据可以打印!") End If End If End If注意 把底下的两句 放在一个模块中 Public op As New ADODB.Recordset Global printName As String "\grbakg.rpt" 是要打印的水晶报表文档
Private Sub Command1_Click() Dim CrxApp As CRAXDRT.Application Dim CrxRep As CRAXDRT.Report Dim crxDatabase As CRAXDRT.Database Dim crxDatabaseTables As CRAXDRT.DatabaseTables Dim crxDatabaseTable As CRAXDRT.DatabaseTable Set CrxApp = New CRAXDRT.Application Set CrxRep = CrxApp.OpenReport("c:\Report1.rpt")
Set crxDatabase = CrxRep.Database
Set crxDatabaseTables = crxDatabase.Tables
'Loop through each DatabaseTable object in the DatabaseTables collection and then set the location 'of the database file for each table
For Each crxDatabaseTable In crxDatabaseTables 如果是Access數據庫 crxDatabaseTable.Location = App.Path & "\xtremelite.mdb" 如果是 SQL database crxDatabaseTable.SetLogOnInfo "servername", "databasename", "userid", "password" 如果用ODBC與SQL數據庫 crxDatabaseTable.SetLogOnInfo "ODBC_DSN", "databasename", "userid", "password" Next crxDatabaseTable
Set crxDatabase = Nothing Set crxDatabaseTable = Nothing Set crxDatabaseTables = Nothing Set crxReport = Nothing Set crxApplication = Nothing End Sub記錄集的話becausemylove() 的代碼應再加一個判斷條件可以得到所要自己打印的數據
这个是你要的不?
装完水晶报表后 引用控件Crystal Report Viewer Control 9
新建一个窗口 把控件放上,控件名CRViewer91
加入以下代码:
Private Sub Form_Load()
printf.WindowState = 2
Dim oApp As New CRAXDRT.Application
Dim oRpt As CRAXDRT.Report
Dim reportName As String
Screen.MousePointer = vbHourglass
reportName = printName
Set oRpt = oApp.OpenReport(App.Path & reportName, 1)
oRpt.Database.SetDataSource op
oRpt.ReadRecords
CRViewer91.ReportSource = oRpt
CRViewer91.ViewReport
Screen.MousePointer = vbDefault
End Sub
Private Sub Form_Resize()
CRViewer91.Top = 0
CRViewer91.Left = 0
CRViewer91.Height = ScaleHeight
CRViewer91.Width = ScaleWidth
End Sub
'---------------------------------
打印的代码放在另一个窗口 我的打印代码是:
list1SQL = "select * from dwbak where 自动编号= " + List1.TextMatrix(row1, 8)
Dim connstr As String
printName = "\grbakg.rpt"
If conn.State = adStateOpen Then conn.Close
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Storehouse.mdb;Persist Security Info=False" 'prtest.mdb是程序当前目录的测试Access数据库
conn.ConnectionString = connstr
conn.Open
conn.CursorLocation = adUseClient
If op.State = adStateOpen Then op.Close
op.Open list1SQL, conn, adOpenKeyset, adLockReadOnly
If op.RecordCount > 0 Then
printf.Show
Else
MsgBox ("没有数据可以打印!")
End If
End If
End If注意 把底下的两句 放在一个模块中
Public op As New ADODB.Recordset
Global printName As String
"\grbakg.rpt" 是要打印的水晶报表文档
Dim CrxApp As CRAXDRT.Application
Dim CrxRep As CRAXDRT.Report
Dim crxDatabase As CRAXDRT.Database
Dim crxDatabaseTables As CRAXDRT.DatabaseTables
Dim crxDatabaseTable As CRAXDRT.DatabaseTable Set CrxApp = New CRAXDRT.Application
Set CrxRep = CrxApp.OpenReport("c:\Report1.rpt")
Set crxDatabase = CrxRep.Database
Set crxDatabaseTables = crxDatabase.Tables
'Loop through each DatabaseTable object in the DatabaseTables collection and then set the location
'of the database file for each table
For Each crxDatabaseTable In crxDatabaseTables
如果是Access數據庫
crxDatabaseTable.Location = App.Path & "\xtremelite.mdb" 如果是 SQL database
crxDatabaseTable.SetLogOnInfo "servername", "databasename", "userid", "password" 如果用ODBC與SQL數據庫
crxDatabaseTable.SetLogOnInfo "ODBC_DSN", "databasename", "userid", "password"
Next crxDatabaseTable
CRViewer91.ReportSource = CrxRep
CRViewer91.ViewReport
Set crxDatabase = Nothing
Set crxDatabaseTable = Nothing
Set crxDatabaseTables = Nothing
Set crxReport = Nothing
Set crxApplication = Nothing
End Sub記錄集的話becausemylove() 的代碼應再加一個判斷條件可以得到所要自己打印的數據