Article ID: 145707
Article Last Modified on 1/19/2007
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
?OLEOpenReport("c:\MSOffice\Access\Samples\Northwind.mdb", _
"Invoice", strWhere:="OrderId = 10251")
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))
' If you are using Microsoft Access 97, msaccess.exe will be
' in the Office subfolder of the folder in which you
' installed your runtime application.
' If you are using Microsoft Access 7.0, use the following:
' x = Shell("c:\Program Files\Common Files\Microsoft Shared\" &_
' "Microsoft Access Runtime\msaccess.exe " & _
' Chr$(34) & strDBName & 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
?OLEOpenReportRuntime("c:\MSOffice\Access\Samples\Northwind.mdb", _
"Invoice", strWhere:="OrderId = 10251")
147816 ACC: Using Microsoft Access as an Automation Server
Additional query words: VB OLE Controlling Server
Keywords: kbinfo kbprogramming kbusage KB145707