# Re: 请问如何列印在DBGRID中的资料?

Posted by 老邱 on August 31, 1998 at 15:04:54:

In Reply to: 请问如何列印在DBGRID中的资料? posted by Jonny on August 25, 1998 at 09:41:48:

: 我将data show 在dbgrid中之后想从此grid中列印出来?
: 如有sample code可否提供

:
: 感激不尽

Hi! Mr. Jonny:
我是在DBGrid的form中放入Crystal Report控制项(程式中的QReport)
和Data控制项(datDataCtl),到Cry.Rep.设计好表格存档(.rpt),
在程式中RepotFileName指到此档,并以其查询语法筛选资料,当按了
cmdPrint的CommandButton后QReport.Action=1会送出预览列印
画面,按印表机小图示就可印出了.希望对您有用.
老邱 '98/08/31
程式码:
Private Sub Form_Load()
On Error Resume Next
'调整萤幕
Width = Me.Width
Height = Me.Height
Left = (Screen.Width - Width) / 2
Top = (Screen.Height - Height) / 2
grdDataGrid.DefColWidth = 850 '调整Grid的行宽
datDataCtl.RecordSource = TotalSQLString '要显示在DBGrid上的资料记录来源
datDataCtl.Refresh
datDataCtl.Recordset.MoveLast
recNum = datDataCtl.Recordset.RecordCount
datDataCtl.Recordset.MoveFirst
currentRec = 1
If recNum = 0 Then
datDataCtl.ForeColor = &HFF&
datDataCtl.Caption = "没有查到您要的资料!请按[离开]回原画面。"
cmdPrint.Enabled = False
Exit Sub
End If
datDataCtl.Caption = "总共" & Str(recNum) & "笔，这是第 1笔资料"
End Sub
-----------------------------
Private Sub cmdPrint_Click()
Dim Pstr As String
Pstr = "C:\(路径)\" & TableName & ".rpt" 'Crystal Report的档名
QReport.ReportFileName = Pstr
QReport.SelectionFormula = PrintString
'字串变数PrintString是类似SQL的查询语法,如上的TotalSQLString,可看Crystal说明
QReport.Action = 1
End Sub

Follow Ups: