江湖救急!!!关于水晶报表的问题!!!高手请进!!

xjgf 2005-01-02 10:12:44
请高手指教!!!!!!!

报表文件已经做好!

如何用VB程序调用水晶报表打印出来!请详细告之!!!这里小弟先行谢过!

我是这样做的!!可是每次都出错!!!
Private Sub Command1_Click()
If rstemp.RecordCount > 0 Then
cReport1.Connect = cnn_cgs
cReport1.ReportFileName = App.Path & "\Report1.rpt"
cReport1.DataSource = DataGrid1
cReport1.Action = 1
end if
End Sub
...全文
79 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoxiaoP 2005-01-03
  • 打赏
  • 举报
回复
学习学习
myhgyp 2005-01-03
  • 打赏
  • 举报
回复
不好意思,上面贴了9.0的代码,下面是4.6的代码:
Option Explicit
Dim adocon As ADODB.Connection


Private Sub Command1_Click(Index As Integer)
With cr1
.PrinterDriver = Printer.DeviceName
'---------------------------------
'连接方式一
.Connect = "DSN = drugdb;UID =sa;PWD =;"
'-------------------------------------------------
.Destination = crptToWindow
.ReportSource = crptReport
.WindowHeight = Screen.Height
.WindowWidth = Screen.Width
.ReportFileName = App.Path + "\crystaltest46.rpt"
.WindowTitle = "满足条件的记录"
.WindowState = crptMaximized
.WindowLeft = 0
.WindowTop = 0

.Formulas(0) = ""
.Formulas(1) = ""
.Formulas(0) = "我这个是参数!"
.Formulas(1) = "我这个是参数!" & Chr(13) & Chr(10) & "是报表你的,我有了"
.FetchSelectionFormula
.Action = 1
'.SQLQuery = sSql
'.Destination = crptToWindow
'.WindowShowPrintBtn = True
'.WindowShowExportBtn = True
'.WindowShowPrintSetupBtn = True
'
'.WindowMaxButton = True
'.WindowState = crptMaximized
'.WindowTitle = "Print view"
'.PrintReport

End With

End Sub

Private Sub Command2_Click()
With cr1
.PrinterDriver = Printer.DeviceName
'---------------------------------
'连接方式二
'.Connect = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=drugdb;Data Source=SAMUNCLE"
'-------------------------------------------------
.Destination = crptToWindow
.ReportSource = crptReport
.WindowHeight = Screen.Height
.WindowWidth = Screen.Width
.ReportFileName = App.Path + "\crystaltest45.rpt"
.WindowTitle = "满足条件的记录"
.WindowState = crptMaximized
.WindowLeft = 0
.WindowTop = 0
.Action = 1
End With
End Sub

Private Sub Command3_Click()
With cr1
.PrinterDriver = Printer.DeviceName
'-------------------------------------------------
'连接方式三
Set adocon = New ADODB.Connection
adocon.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=drugdb;Data Source=SAMUNCLE"
adocon.ConnectionTimeout = 120
adocon.Open
'.Connect = adocon
'-------------------------------------------------
.Destination = crptToWindow
.ReportSource = crptReport
.WindowHeight = Screen.Height
.WindowWidth = Screen.Width
.ReportFileName = App.Path + "\crystaltest45.rpt"
.WindowTitle = "满足条件的记录"
.WindowState = crptMaximized
.WindowLeft = 0
.WindowTop = 0
.Action = 1
End With
adocon.Close
Set adocon = Nothing

End Sub
myhgyp 2005-01-03
  • 打赏
  • 举报
回复
'*****************************************************************
'*crystal report 的使用示例
'*适用版本:9.0
'*制作人:马亚红
'*制作时间:2004年12月28日
'*如要引用该代码,请注出处
'*好多朋友有这个问题:报表显示后数据没有刷新
'*解决方法如下:在报表的设计界面,菜单“文件”--》“选项”--》“建立报表”选项卡--》把“将数据与报表一起保存”前的勾去掉后保存即可
'*****************************************************************
Option Explicit
Dim Report As New CrystalReport1
Dim wjfilesys As FileSystemObject 'FSO 对象,用于文件操作
Dim WithEvents CrSecRH As Section '报表显示区对象
Dim capp As New CRAXDDRT.Application '报表集合对象
Dim carp As New CRAXDRT.Report '报表集合中的报表对象
Dim carsubp As New CRAXDRT.Report '报表集合对象中的子报表对象
Dim Pic2 As OLEObject '报表对象中的Ole对象
Dim mytxt As CRAXDRT.TextObject '报表对象中的Textbox对象


Private Sub Combo1_Change() '放大(缩小)比例
CRViewer91.Zoom (CInt(Combo1.Text))
End Sub



Private Sub Command1_Click() '显示第一页
CRViewer91.ShowFirstPage
End Sub



Private Sub Command10_Click() '打印报表
CRViewer91.PrintReport
End Sub

Private Sub Command2_Click() '刷新报表
CRViewer91.Refresh
End Sub

Private Sub Command3_Click() '显示上一页
CRViewer91.ShowPreviousPage
End Sub
Private Sub Command4_Click() '显示下一页
CRViewer91.ShowNextPage
End Sub

Private Sub Command5_Click() '显示最后一页
CRViewer91.ShowLastPage
End Sub

Private Sub Command6_Click() '在报表内容内搜索
If (txtSearch.Text <> "") Then
CRViewer91.SearchForText (txtSearch.Text)
End If
txtSearch.Text = ""
End Sub

Private Sub Command7_Click() '导入出PDF

Dim myExportFile As String
myExportFile = App.Path + "\temp.pdf"
If wjfilesys.FileExists(myExportFile) Then

wjfilesys.DeleteFile (myExportFile)
End If
Report.ExportOptions.DiskFileName = myExportFile
Report.ExportOptions.FormatType = crEFTPortableDocFormat
Report.ExportOptions.DestinationType = crEDTDiskFile
Report.ExportOptions.PDFExportAllPages = True
Report.Export (False)

End Sub

Private Sub Command8_Click() '导出到Excel
Dim myExportFile As String
myExportFile = App.Path + "\temp.xls"
If wjfilesys.FileExists(myExportFile) Then

wjfilesys.DeleteFile (myExportFile)
End If
Report.ExportOptions.DiskFileName = myExportFile
Report.ExportOptions.FormatType = crEFTExcel97
Report.ExportOptions.DestinationType = crEDTDiskFile
Report.ExportOptions.PDFExportAllPages = True
Report.Export (False)

End Sub

Private Sub Command9_Click() '导出到WORD
Dim myExportFile As String
myExportFile = App.Path + "\temp.doc"
If wjfilesys.FileExists(myExportFile) Then

wjfilesys.DeleteFile (myExportFile)
End If
Report.ExportOptions.DiskFileName = myExportFile
Report.ExportOptions.FormatType = crEFTWordForWindows
Report.ExportOptions.DestinationType = crEDTDiskFile
Report.ExportOptions.PDFExportAllPages = True
Report.Export (False)
End Sub

Private Sub Form_Load()
Screen.MousePointer = vbHourglass
'''显示方式一:情况为报表直接在工程设计器中制作(Dsr文件)
'---------------------
''CRViewer91.ReportSource = Report
''CRViewer91.ViewReport
'----------------------
'显示方式二:情况为报表在水晶报表软件中制作(rpt文件)
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim i As Integer
If cn.State = adStateOpen Then cn.Close
''SQL SERVER连接方式
'With cn
'.Provider = "sqloledb"
'.ConnectionString = "data source=dd;initial catalog=dfd;user id=sa;password=aaa"
'.Open
'End With
'Mdb的连接方式
With cn
.Provider = "Microsoft.Jet.OLEDB.4.0"
.ConnectionString = "Data Source=" + App.Path + "\Test.mdb;Persist Security Info=False"
.Open
End With

strsql = "select * from carsort"
If rs.State = adStateOpen Then rs.Close
With rs
.ActiveConnection = cn
.CursorLocation = adUseClient
.Open strsql, cn, adOpenDynamic, adLockOptimistic
End With

Set carp = capp.OpenReport(App.Path + "\CryStalTest.rpt") '取报表对象
Set carsubp = carp.OpenSubreport("myhgyp") '取子报表对象
Set CrSecRH = carp.Sections("PageHeaderSection1") '取报表头显示区对象
Set Pic2 = carp.Sections("PageHeaderSection1").ReportObjects("picture1") '取报表头显示区中的Ole对象
Set mytxt = CrSecRH.ReportObjects("mytxt")

For i = 1 To carp.Database.Tables.Count '设置报表的数据源
If carp.Database.Tables.Item(i).Name = "carsort" Then
carp.Database.Tables(i).SetDataSource rs
End If
Next
For i = 1 To carsubp.Database.Tables.Count '设置子报表的数据源
If carp.Database.Tables.Item(i).Name = "carsort" Then
carsubp.Database.Tables(i).SetDataSource rs
End If
Next
Screen.MousePointer = vbHourglass
'第一种设置参数的方式
''carp.ParameterFields(1).ClearCurrentValueAndRange
''carp.ParameterFields(1).AddCurrentValue ("myhgyp")
''carp.ParameterFields(2).ClearCurrentValueAndRange
''carp.ParameterFields(2).AddCurrentValue (CInt("3"))
'第二种设置参数的方式
carp.ParameterFields.GetItemByName("gg").ClearCurrentValueAndRange
carp.ParameterFields.GetItemByName("gg").AddCurrentValue ("myh11" & vbCrLf & "-2gyp")
carp.ParameterFields.GetItemByName("ff").ClearCurrentValueAndRange
carp.ParameterFields.GetItemByName("ff").AddCurrentValue (CInt("673"))
CRViewer91.ReportSource = carp
CRViewer91.ViewReport
Screen.MousePointer = vbDefault
CRViewer91.Zoom 100
'----------------------
Screen.MousePointer = vbDefault
Set wjfilesys = CreateObject("Scripting.FileSystemObject")
End Sub
Private Sub CrSecRH_Format(ByVal pFormattingInfo As Object) '格式化报表头显示区中的对象
Set Pic2.FormattedPicture = LoadPicture(App.Path & "\tsconfig.bmp") 'changes the pic in the Report Header
mytxt.SetText Format(mytxt.Text, "####年##月##日")
'mytxt.CanGrow = True
'mytxt.SetText "NO.1" & Chr(13) & Chr(10) & "NO.2"
End Sub

Private Sub Form_Resize() '设置报表显示区域
CRViewer91.Top = 0
CRViewer91.Left = 0
CRViewer91.Height = ScaleHeight
CRViewer91.Width = ScaleWidth
End Sub

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧