ADO记录集和水晶报表

 

在使用水晶报表时,经常会感到数据不是很好控制,最后只好使用这个终极绝招咯。那就是使用数据定义文件(TTX),把得到的ADO记录集传送给水晶报表。

通常情况下,水晶报表是从物理的数据库上创建出来的,但是现在有了32位的Active Data Driver-- P2smon.dll,水晶就可以不用再事先连接到一个数据库上咯。

首先,需要创建一个TTX文件,进入数据源选择窗体后,如下图选中

再单击了前面的“+”后弹出数据源窗体

 

此时,我们使用“New”按钮创建一个新的数据定义文件(TTX),格式如下

保存后,我们打开这个文本文件,就会发现TTX文件的格式,原来中间是TAB分割符来的。

接着,我们就按照水晶报表的老套路画式样,剩下的就是传递记录机集了。

下面,我们要声明好传递ADO记录集需要的Api

Public Declare Function PEOpenEngine Lib "crpe32.dll" () As Integer

 

Public Declare Function PEGetErrorCode Lib "crpe32.dll" (ByVal printJob As Integer) As Integer

 

Public Declare Function PEOpenPrintJob Lib "crpe32.dll" (ByVal RptName As String) As Integer

 

Public Declare Function PEOutputToWindow Lib "crpe32.dll" ( _

                                                            ByVal printJob As Integer, _

                                                            ByVal Title As String, _

                                                            ByVal Left As Long, _

                                                            ByVal Top As Long, _

                                                            ByVal Width As Long, _

                                                            ByVal Height As Long, _

                                                            ByVal style As Long, _

                                                            ByVal PWindow As Long) As Integer

 

Public Declare Function PEOutputToPrinter Lib "crpe32.dll" ( _

                                                            ByVal printJob As Integer, _

                                                            ByVal nCopies As Integer) As Integer

                                                           

Public Declare Function PEStartPrintJob Lib "crpe32.dll" ( _

                                                            ByVal printJob As Integer, _

                                                            ByVal WaitOrNot As Integer) As Integer

                                                           

Public Declare Function PEClosePrintJob Lib "crpe32.dll" (ByVal printJob As Integer) As Integer

 

Public Declare Sub PECloseEngine Lib "crpe32.dll" ()

 

Public Declare Function CreateFieldDefFile Lib "p2smon.dll" ( _

                                                            lpUnk As Object, ByVal _

                                                            fileName As String, _

                                                            ByVal bOverWriteExistingFile As Long) As Long

       

Public Declare Function vbEncodelPtr Lib "p2smon.dll" (x As Object) As String

 

Public Declare Function SetActiveDataSource Lib "p2smon.dll" ( _

                                                                ByVal printJob As Integer, _

                                                                ByVal tableNum As Integer, _

                                                                x As Object) As Long

 

最后给出的是报表的打印代码

Dim Job As Integer

Dim Handle As Integer

'打开打印引擎

Handle = PEOpenEngine

'水晶的错误处理

If Handle = 0 Then

    ErrorNum = PEGetErrorCode(Handle)

    MsgBox "打印引擎出错!"

    MsgBox "错误代号:" & ErrorNum

End If

 

'打开打印作业

Job = PEOpenPrintJob(App.Path & "/New.rpt")

'水晶的错误处理

If Job = 0 Then

    ErrorNum = PEGetErrorCode(Job)

    MsgBox "打开作业New.rpt 失败!"

    MsgBox "错误代号:" & ErrorNum

End If

'给水晶报表传送ADO记录集,AdoRecordset的生成就不再多说了

Handle = SetActiveDataSource(Job, 0, ADOrs)

'输出到打印预览窗口

Handle = PEOutputToWindow(Job, "New.rpt", 0, 0, 520, 520, WS_MAXIMIZE, 0)

直接输出到打印机

Handle = PEOutputToPrinter(Job,1)

'水晶的错误处理

If Handle <> 0 Then

Handle = PEStartPrintJob(Job, True)

    If Handle <> 0 Then

        MsgBox "打印完毕"

    Else

        ErrorNum = PEGetErrorCode(Job)

        MsgBox "打印失败!"

        MsgBox "错误代号:" & ErrorNum

    End If

Else

    ErrorNum = PEGetErrorCode(Job)

    MsgBox "无法输出到窗体或打印机"

    MsgBox "错误代号:" & ErrorNum

End If

'关闭打印作业

PEClosePrintJob (Job)

'关闭打印引擎

PECloseEngine

 

我使用的是VB6+水晶报表8.5,感觉这种方式比较灵活好用,同时解决了水晶报表补空行的问题。

你可能感兴趣的:(Visual,Basic,6,报表,integer,function,string,object,作业)