在access中使用自动化打印报告
时 间:2009-08-13 14:21:43
作 者:   ID:4070  城市:杭州
摘 要:在Access中使用自动化打印报告
正 文:
通过执行 DoCmd 对象的该 OpenReport 方法,可以从任何自动化客户端应用程序,如 Microsoft Visual Basic 或 Microsoft Visual Basic Environment (VBE) 来打印 Access 报表。 这使您以实现 Access 作为应用程序解决方案中报告的组件。 Microsoft 提供仅,用于说明的编程示例不附带任何明示或默示的保证。 这包括,但不限于适销性或针对特定用途的适用性的默示保证。 本文假定您熟悉所演示的编程语言和用于创建和调试过程的该工具。 Microsoft 支持工程师可以帮助解释某个特定的过程的功能,但是它们不会修改这些示例以提供额外的功能或构建过程以满足您的特殊需求。 警告 : 如果您按照此示例,则修改示例数据库 Northwind.mdb。 可以备份 Northwind.mdb 文件,并在数据库的副本上执行这些步骤。
打开 Microsoft Access。 
创建新的空白数据库。 
创建模块,然后声明部分中键入以下: 
Option Explicit
' In other applications like Microsoft Visual Basic,
' you can include a reference to Microsoft Access to
' gain the use of Access constants. or, use the following
' constant values...
' Global Const acNormal = 0
' Global Const acDesign = 1
' Global Const acPreview = 2
' -----------------------------------------------------
' Application Quit options...
' saves all objects without displaying a dialog box:
' Global Const acSaveYes = 0
' displays a dialog box that asks whether you want to save any
' database objects that have been changed but not saved:
' Global Const acPrompt = 1
' quits Microsoft Access without saving any objects:
' Global Const acExit = 2
     
键入下面的过程: 
Function OLEOpenReport(strDBName As String, _
                       strRptName As String, _
                       Optional ByVal intDisplay As Variant, _
                       Optional ByVal strFilter As Variant, _
                       Optional ByVal strWhere As Variant) As Boolean
   On Error GoTo OLEOpenReport_Err
   ' Create Automation object.
   Dim objAccess As Object
   Set objAccess = CreateObject("Access.Application")
   ' Open the supplied database.
   ' Optional parameter at the end of statement
   ' indicates exclusive mode if set to True...
   objAccess.OpenCurrentDatabase strDBName, False
   ' The OpenReport method uses the following arguments...
   ' Report Name - Name of the report object.
   ' View - Display in Print Preview or send to printer.
   '        acNormal - Print report
   '        acDesign - open report in design (n/a in runtime)
   '        acPreview - open in preview window
   ' Filter Name - Name of a saved filter query.
   ' Where Condition = valid SQL where condition.
   If IsMissing(intDisplay) Then intDisplay = acNormal
   If IsMissing(strFilter) Then strFilter = ""
   If IsMissing(strWhere) Then strWhere = ""
   objAccess.DoCmd.OpenReport strRptName, intDisplay, strFilter, _
      strWhere
   ' Close Microsoft Access session instance...
   objAccess.Quit acExit
   Set objAccess = Nothing
   OLEOpenReport = True
OLEOpenReport_End:
   Exit Function
OLEOpenReport_Err:
   MsgBox Error$(), vbInformation, "Automation"
   Resume OLEOpenReport_End
End Function
要测试此函数,在立即的窗口中键入下面一行,,然后按 ENTER 键: 
?OLEOpenReport("c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", "Invoice", strWhere:="OrderId = 10251")
     
函数打开罗斯文数据库,打开发票报表,将记录集设置为 orderId # 10251,然后打印到打印机报表。 
注意: 请确保提供您的系统上文件的相应路径。 
在运行版本的 Access 只支持自动化 GetObject () 函数。 
打开 Microsoft Access。 
创建新的数据库。 
创建模块,然后声明部分中键入以下: 
Option Explicit
' In other applications like Microsoft Visual Basic,
' you can include a reference to Microsoft Access to
' gain the use of Microsoft Access constants. or, use the following
' constant values...
' Global Const acNormal = 0
' Global Const acDesign = 1
' Global Const acPreview = 2
' -----------------------------------------------------
' Application Quit options...
' saves all objects without displaying a dialog box:
' Global Const acSaveYes = 0
' displays a dialog box that asks whether you want to save any
' database objects that have been changed but not saved:
' Global Const acPrompt = 1
' quits Microsoft Access without saving any objects:
' Global Const acExit = 2
                                        
键入下面的过程: 
Function OLEOpenReportRuntime(strDBName As String, _
strRptName As String, _
Optional ByVal intDisplay As Variant, _
Optional ByVal strFilter As Variant, _
Optional ByVal strWhere As Variant _
) As Boolean
On Error GoTo OLEOpenReportRuntime_Err
Dim x As Long
Dim objAccess As Object
' Open the run-time instance and database...
' ------------------------------------------
' The use of the Chr$(34) function supplies
' quotation marks around the database name which is
' required by Shell when the optional command
' line parameter contains spaces...
x = Shell("c:\myapp\Office\msaccess.exe " &_
Chr$(34) & strDBName & Chr$(34) & _
"/Runtime /Wrkgrp " & Chr$(34) & _
"c:\myapp\system.mdw" & Chr$(34))
Set objAccess = GetObject(strDBName)
' The OpenReport method uses the following arguments...
' Report Name - Name of the report object.
' View - Display in Print Preview or send to printer.
'        acNormal - Print report
'        acDesign - open report in design (n/a in runtime)
'        acPreview - open in preview window
' Filter Name - Name of a saved filter query.
' Where Condition = valid SQL where condition.
If IsMissing(intDisplay) Then intDisplay = acNormal
If IsMissing(strFilter) Then strFilter = ""
If IsMissing(strWhere) Then strWhere = ""
objAccess.DoCmd.OpenReport strRptName, intDisplay, strFilter, _
strWhere
' Close Microsoft Access session instance...
objAccess.Quit acExit
Set objAccess = Nothing
OLEOpenReportRuntime = True
OLEOpenReportRuntime_End:
Exit Function
OLEOpenReportRuntime_Err:
MsgBox Error$(), vbInformation, "Automation"
Resume OLEOpenReportRuntime_End
End Function
                                        
要测试此函数,在立即的窗口中键入下面一行,,然后按 ENTER 键: 
?OLEOpenReportRuntime("c:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb", "Invoice", strWhere:="OrderId = 10251")
                                        
函数打开罗斯文数据库,打开发票报表,将记录集设置为 orderId # 10251,然后打印到打印机报表。 
注意: 请确保提供您的系统上文件的相应路径。 
Access软件网官方交流QQ群 (群号:54525238) Access源码网店
常见问答:
技术分类:
源码示例
- 【源码QQ群号19834647...(12.17)
- 【Access高效办公】上一年...(10.30)
- Access制作的RGB转CM...(09.22)
- Access制作的RGB调色板...(09.15)
- Access制作的快速车牌输入...(09.13)
- 【Access高效办公】统计当...(06.30)
- 【Access高效办公】用复选...(06.24)
- 根据变化的日期来自动编号的示例...(06.20)
- 【Access高效办公】按日期...(06.12)
- 合并列数据到一个文本框的示例;...(05.06)
 
  学习心得
最新文章
- 【Access高效办公】上一年度累...(10.30)
- Access做的一个《中华经典论语...(10.25)
- Access快速开发平台--加载事...(10.20)
- 【Access有效性规则示例】两种...(10.10)
- EXCEL表格扫描枪数据录入智能处...(10.09)
- Access快速开发平台--多行文...(09.28)
- 关于从Excel导入长文本数据到A...(09.24)
- Access制作的RGB转CMYK...(09.22)
- 关于重装系统后Access开发的软...(09.17)
- Access制作的RGB调色板示例(09.15)
 

 
  
.gif)

 
            