跪求,VB中,水晶报表子报表数据的显示问题。
VB工具:VB6.0(中文版)打了SP6补丁。
水晶报表:Crystal Reports 9.2.0.448 简体中文版(没打任何补丁)
数据库:Access2000
情况介绍:我做的是采购入库单,有主表和子表(入库单明细),子表放在主表的详细资料节里,子报表连接已设置,在水晶报表设计器这边打印预览正常。创建报表时用的数据源连接是 OLE DB (ADO) ,听取网上相关贴子的意见,试过将数据源连接的文件类型改为ttx 格式的。
问题是:穷尽我之所能,Google上搜索了N多的帖子,就是无法实现在VB代码中赋数据源给子报表这一目的。主报表显示很正常,运行时不报错。还有一个现象,其中一张单子明细比较多,需两页,VB运行时,这张单子也是显示两页,可就是子表里总是为空,没数据。我猜是不是水晶报表的这个版本有问题,是不是要打什么补丁呢?
尝试的代码之一:
Dim Report As New sjq_cgrkd
Dim carsubp As New CRAXDRT.Report '报表集合对象中的子报表对象
Dim Rs As ADODB.Recordset
Dim Rs1 As ADODB.Recordset
Private Sub Form_Load()
Set Rs = New ADODB.Recordset
Rs.Open "Select s.商品采购表ID,s.采购表编号,s.轻松编号,s.发票号码,s.与发票是否相符,s.登记相符的人,s.登记相符时间,c.名称 as 仓库,w.名称 as 我司抬头,g.名称 as 供应商,s.日期,s.合计金额,s.合计金额大写,s.备注,s.制单人 from 商品采购表 as s,仓库资料 as c,我司抬头资料 as w,供应商资料 as g where s.仓库资料ID=c.仓库资料ID and s.我司抬头资料ID=w.我司抬头资料ID and s.供应商资料ID=g.供应商资料ID order by s.采购表编号 asc ", Cnn, adOpenDynamic, adLockBatchOptimistic
Report.Database.SetDataSource Rs '(动态设置主报表数据源)
Set Rs1 = New ADODB.Recordset
Rs1.Open "Select c.商品采购表ID,s.名称,s.单位,s.基价,c.商品采购从表ID,c.商品资料ID,c.数量,c.价格属性,c.原价,c.扣率,(c.原价*c.扣率) as 折后价,c.金额,c.摘要,c.批价人,c.批价时间 from 商品采购从表 as c,商品资料 as s where c.商品资料ID=s.商品资料ID order by c.商品采购从表ID asc ", Cnn, adOpenDynamic, adLockBatchOptimistic
Set carsubp = Report.OpenSubreport( "cgrkdmx ") '取子报表对象
carsubp.Database.SetDataSource Rs1 '(动态设置子报表数据源)
Screen.MousePointer = vbHourglass '调用水晶报表时置鼠标为沙漏状
CRViewer91.ReportSource = Report
CRViewer91.ViewReport '(显示报表)
Screen.MousePointer = vbDefault '调用水晶报表完成后置鼠标为默认形状
End Sub
尝试的代码之二:
Option Explicit
Dim objCRApp As New CRAXDRT.Application '水晶报表应用程序对象
Dim objCRReport As New CRAXDRT.Report '报表对象
Private Sub CRViewer91_PrintButtonClicked(UseDefault As Boolean)
UseDefault = False
objCRReport.PrinterSetup Me.hwnd
objCRReport.PrintOutEx False
End Sub
Private Sub Form_Load()
Dim i As Integer
Dim rst1 As New ADODB.Recordset
Dim rst2 As New ADODB.Recordset
Dim rst3 As New ADODB.Recordset
Dim Obj As Object '对象型
Dim props As CRAXDRT.ConnectionProperties '报表数据源参数集
Dim prop As CRAXDRT.ConnectionProperty '报表数据源参数
Dim oSection As CRAXDRT.Section '报表节对象
Dim CRXSubReport As CRAXDRT.SubreportObject '子报表对象
Dim CRXSubReportTemp As CRAXDRT.Report '子报表
Dim bSubreport As Boolean '布尔型。True ,False
rst1.CursorLocation = adUseClient
rst2.CursorLocation = adUseClient
Set rst1 = Cnn.Execute( "Select s.商品采购表ID,s.采购表编号,s.轻松编号,s.发票号码,s.与发票是否相符,s.登记相符的人,s.登记相符时间,c.名称 as 仓库,w.名称 as 我司抬头,g.名称 as 供应商,s.日期,s.合计金额,s.合计金额大写,s.备注,s.制单人 from 商品采购表 as s,仓库资料 as c,我司抬头资料 as w,供应商资料 as g where s.仓库资料ID=c.仓库资料ID and s.我司抬头资料ID=w.我司抬头资料ID and s.供应商资料ID=g.供应商资料ID and s.商品采购表ID= " + Trim(Form_cgrkd.Text6.Text) + " order by s.采购表编号 asc ")
Set rst2 = Cnn.Execute( "Select c.商品采购表ID,s.名称,s.单位,s.基价,c.商品采购从表ID,c.商品资料ID,c.数量,c.价格属性,c.原价,c.扣率,(c.原价*c.扣率) as 折后价,c.金额,c.摘要,c.批价人,c.批价时间 from 商品采购从表 as c,商品资料 as s where c.商品资料ID=s.商品资料ID order by c.商品采购从表ID asc ")
'--------------------------------------------------
'创建报表
'--------------------------------------------------
'加载报表模板
Set objCRReport = objCRApp.OpenReport(App.Path & "\cgrkd.rpt ", 1)
'加载主报表字段定义文件
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 & "\Students.ttx "
Exit For
End If
Next
Set props = Nothing
'给主报表赋值
objCRReport.DiscardSavedData
objCRReport.Database.Tables(1).SetDataSource rst1
'第一个子报表
bSubreport = False
'循环报表的每个节
For Each oSection In objCRReport.Sections
'循环该节中的每个报表对象
For Each Obj In oSection.ReportObjects
'如果该报表对象是子报表
If Obj.Kind = crSubreportObject Then
'如果是第一个子报表,根据名称匹配,注意大小写有区别的
'获取这个报表对象,跳出循环,准备操作 subreport1
If LCase(Obj.Name) = "cgrkdmx " Then
Set CRXSubReport = Obj
bSubreport = True
End If
Exit For
End If
Next Obj
If bSubreport Then Exit For
Next oSection
'设定该版子报表的ttx Set carsubp = carp.OpenSubreport( "cgrkdmx ") '取子报表对象
Set CRXSubReportTemp = objCRReport.OpenSubreport( "cgrkdmx ")
Set props = CRXSubReportTemp.Database.Tables(1).ConnectionProperties
For Each prop In props
If InStr(prop.Name, "Field Definition File ") > 0 Then
'设置子报表的字段定义文件(TTX)
prop.Value = App.Path & "\books.ttx "
Exit For
End If
Next
'数据填充
CRXSubReportTemp.DiscardSavedData
CRXSubReportTemp.Database.Tables(1).SetDataSource rst2
Set props = Nothing
Set prop = Nothing
objCRReport.EnableParameterPrompting = False '不进行报表参数提示
'指定报表来源
CRViewer91.ReportSource = objCRReport
'显示报表
CRViewer91.ViewReport
Set rst1 = Nothing
Set rst2 = Nothing
End Sub
Private Sub Form_Resize()
On Error Resume Next
CRViewer91.Left = 200
CRViewer91.Width = Me.Width - 500
CRViewer91.Height = Me.Height
End Sub
这两段代码都是我参考网上的贴子的,虽然后面这段比前面那段复杂,但运行的结果都是一样的。
[解决办法]
汗,不是参考我的代码吧。
水晶报表改用TTX设计的时候,每个字段都要设计正确,否则会出现空白出错。
因为环境和数据库的问题,我没有办法测试的你的问题。
如果实在没办法,你可以把子报表用ADO去才设计看看是否运行正常。
水晶报表这东西,虽然能设计出好的报表,但是要想设计好,是要下很大功夫的,反正我就被折磨怕了,现在不想碰了,