Article ID: 147816
Article Last Modified on 1/19/2007
Dim objAccess As Access.Application
This type of declaration is called early binding, which is fastest.
However, it is not supported by all Automation controllers. For this
reason, the sample code in this article uses late binding instead
of early binding. For, example, it declares a variable as an object
rather than as Access.Application:
Dim objAccess As Object
To find out if an application supports early or late binding, please
see your Automation controller documentation.
Dim objAccess as Object
Set objAccess = GetObject("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")
Note that the objAccess variable can refer to different instances of
Microsoft Access depending on the following conditions when the code is
run:
Dim objAccess as Object Set objAccess = GetObject(,"Access.Application")Note that the objAccess variable can refer to different instances of Microsoft Access depending on the following conditions when the code is run:
Dim objAccess as Object
Set objAccess = CreateObject("Access.Application")
The objAccess variable refers to the Application object of the new
instance.
Dim objAccess as New Access.ApplicationSee your Automation controller documentation to determine if it supports the New keyword.
Method for Activating MS Access UserControl Visible
----------------------------------------------------------
GetObject() function after an True True
instance of Microsoft Access
is opened
Shell() function True True
GetObject() function when no False False
instance of Microsoft Access
is open yet
CreateObject() function False False
UserControl Property
--------------------
Dim objAccess as Object
Set objAccess = CreateObject("Access.Application")
objAccess.Visible = True
This method works because the CreateObject() function always opens a new
instance of the specified application. If you set the Visible property
immediately after running CreateObject(), then the user cannot intervene by
restoring or minimizing the application window, which resets the
UserControl and Visible properties.
'----------------------------------------------------------------------
'DECLARATIONS
'----------------------------------------------------------------------
Option Explicit
Declare Function SetForegroundWindow Lib "User32" _
(ByVal hWnd As Long) As Long
Declare Function IsIconic Lib "User32" _
(ByVal hWnd As Long) As Long
Declare Function ShowWindow Lib "User32" _
(ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Const SW_NORMAL = 1 'Show window in normal size
Const SW_MINIMIZE = 2 'Show window minimized
Const SW_MAXIMIZE = 3 'Show window maximized
Const SW_SHOW = 9 'Show window without changing window size
Dim objAccess As Object 'module-level declaration
'----------------------------------------------------------------------
'This procedure brings the instance of Microsoft Access referred to
'as "instance" into view. The instance's window size can be SW_NORMAL,
'SW_MINIMIZE, SW_MAXIMIZE, or SW_SHOW. If size is omitted, the window is
'not changed (SW_SHOW). To call this function, use this syntax:
' ShowAccess instance:=objAccess, size:=SW_SHOW
'----------------------------------------------------------------------
Sub ShowAccess(instance As Object, Optional size As Variant)
Dim hWnd As Long, temp As Long
If IsMissing(size) Then size = SW_SHOW
On Error Resume Next
If Not instance.UserControl Then instance.Visible = True
On Error GoTo 0 'turn off error handler
hWnd = instance.hWndAccessApp
temp = SetForegroundWindow(hWnd)
If size = SW_SHOW Then 'keep current window size
If IsIconic(hWnd) Then temp = ShowWindow(hWnd, SW_SHOW)
Else
If IsIconic(hWnd) And size = SW_MAXIMIZE Then _
temp = ShowWindow(hWnd, SW_NORMAL)
temp = ShowWindow(hWnd, size)
End If
End Sub
'----------------------------------------------------------------------
'This procedure opens the sample database Northwind.mdb in a new or
'existing instance of Microsoft Access (if one is already open).
'
'NOTE: This procedure uses the ShowAccess() procedure (listed above for
'Method 2). You must enter this procedure into the same module as
'ShowAccess() for the code to run properly.
'----------------------------------------------------------------------
Sub OpenNorthwind()
Dim path as String
On Error Resume Next 'temporary error handling
Set objAccess = GetObject(,"Access.Application")
If Err <> 0 Then 'no existing instances of Access
Set objAccess = CreateObject("Access.Application")
End If
On Error GoTo OpenNorthwind_ErrHandler 'normal error handler
ShowAccess instance:=objAccess, size:=SW_MAXIMIZE
With objAccess
path = .SysCmd(Access.acSysCmdAccessDir) & "Samples\Northwind.mdb"
If .DBEngine.Workspaces(0).Databases.Count = 0 Then
.OpenCurrentDatabase filepath:=path
ElseIf LCase(Right(.CurrentDb.Name, Len("northwind.mdb"))) _
<> "northwind.mdb" Then
.CloseCurrentDatabase
.OpenCurrentDatabase filepath:=path
End If
.DoCmd.OpenForm FormName:="Main SwitchBoard"
End With
Exit Sub
OpenNorthwind_ErrHandler:
MsgBox Error$(), , "Open Northwind"
End Sub
Object View
----------------------
Table Datasheet
Design
Query Datasheet
Form Form
Report Print Preview
When a Microsoft Access object is open, the instance does not close until
the objects are closed and the Application object's UserControl property is
False. You can, however, force an instance to close by using the Quit
method of an Application object. For example, the following sample code
uses the Quit method to close all instances of Microsoft Access.
'----------------------------------------------------------------------
'DECLARATIONS
'----------------------------------------------------------------------
Option Explicit
'----------------------------------------------------------------------
'This procedure closes all open instances of Microsoft Access. Once all
'instances are closed, the error handler is run and the procedure ends.
'----------------------------------------------------------------------
Sub CloseAllAccess()
Dim objAccess As Object
On Error GoTo CloseAllAccess_ErrHandler
Do
Set objAccess = GetObject(,"Access.Application")
objAccess.Quit
Loop
CloseAllAccess_ErrHandler:
Set objAccess = Nothing
End Sub
WARNING: You should not use the Quit method to close an instance created by
directly calling a Microsoft Access function if your code makes additional
calls to Microsoft Access functions. For more information about using
Microsoft Access functions in your Automation code, please see the "Calling
Microsoft Access Functions" section later in this article.
Dim objAccess as object
Set objAccess = CreateObject("Access.Application")
ShowAccess instance:=objAccess, size:=SW_MAXIMIZE
SendKeys "+"
'Simulates holding down the SHIFT key as the database is being opened
objAccess.OpenCurrentDatabase filepath:= _
"C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb"
For the SendKeys statement to work properly, your code should meet the
following conditions:
Dim objAccess as Object
Set objAccess = CreateObject("Access.Application")
You should not use early binding, which declares the variable using an
application-defined object type, for example:
Dim objAccess as Access.Application
Set objAccess = CreateObject("Access.Application")
objAccess.OpenCurrentDatabase filepath:= _
objAccess.SysCmd(Access.acSysCmdAccessDir) &"Samples\Northwind.mdb"
SendKeys "+"
use this sample code:
path = objAccess.SysCmd(Access.acSysCmdAccessDir) & _
"Samples\Northwind.mdb"
SendKeys "+"
objAccess.OpenCurrentDatabase filepath:=path
MsgBox Access.Eval("2+2") 'displays "4"
MsgBox Access.SysCmd(Access.acSysCmdAccessDir) 'displays the path
The first time an Automation controller directly calls a Microsoft Access
function, a new, minimized instance of Microsoft Access is created. The
controller maintains a connection to this instance in case your code makes
additional calls to Microsoft Access functions. This connection remains in
effect until the controller application is closed.
Dim objAccess as Object
On Error Resume Next
Set objAccess = GetObject(,"Access.Application")
If Err <> 0 Then 'no instance of Access is open
Set objAccess = CreateObject("Access.Application")
End If
MsgBox objAccess.Eval("2+2") 'displays 4
MsgBox objAccess.SysCmd(Access.acSysCmdAccessDir) 'displays the path
Public Function MyDateAdd(interval As String, number As Integer, _
startdate As Date) As Date
MyDateAdd = DateAdd(interval, number, startdate)
'Calls the Microsoft Access built-in DateAdd function.
End Function
To run the above function, add the following sample code to your Automation
controller:
Dim objAccess as Object, newdate as Date
Set objAccess = GetObject _
("C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb")
newdate = objAccess.Run("MyDateAdd", "m", 1, Date)
MsgBox newdate,,"MyDateAdd returned"
objAccess.Quit
'----------------------------------------------------------------------
'DECLARATIONS
'----------------------------------------------------------------------
Option Explicit
Dim objAccess as Object
'----------------------------------------------------------------------
'This procedure sets a module-level variable, objAccess, to refer to
'an instance of Microsoft Access. The code first tries to use GetObject
'to refer to an instance that might already be open and contains the
'specified database (dbpath). If the database is not already open in
'an instance of Microsoft Access, a new instance of the full version of
'Microsoft Access is opened. If the full version of Microsoft Access is
'not installed, the Shell() function starts a run-time instance of
'Microsoft Access. Once the instance is opened, you can use the
'CloseCurrentDatabase and OpenCurrentDatabase methods to work with other
'databases.
'----------------------------------------------------------------------
Sub OpenRunTime()
Dim accpath As String, dbpath As String
On Error Resume Next
dbpath = "C:\My Application\MyApp.mdb"
Set objAccess = GetObject(dbpath)
If Err <> 0 Then
If Dir(dbpath) = "" Then 'dbpath is not valid
MsgBox "Couldn't find database."
Exit Sub
Else 'The full version of Microsoft Access is not installed.
accpath = "C:\Program Files\Common Files\Microsoft Shared" & _
"\Microsoft Access Runtime\MSAccess.exe"
If Dir(accpath) = "" Then
MsgBox "Couldn't find Microsoft Access."
Exit Sub
Else
Shell pathname:=accpath & " " & Chr(34) & dbpath & Chr(34), _
windowstyle:=6
Do 'Wait for shelled process to finish
Err = 0
Set objAccess = GetObject(dbpath)
Loop While Err <> 0
End If
End If
End If
End Sub
'----------------------------------------------------------------------
'DECLARATIONS
'----------------------------------------------------------------------
Option Explicit
Dim objAccess as Object
'----------------------------------------------------------------------
'This procedure sets a module-level variable, objAccess, to refer to
'an instance of Microsoft Access. The code first tries to use GetObject
'to refer to an instance that might already be open. If an instance is
'not already open, the Shell() function opens a new instance and
'specifies the user and password, based on the arguments passed to the
'procedure.
'
'Calling example: OpenSecured varUser:="Admin", varPw:=""
'----------------------------------------------------------------------
Sub OpenSecured(Optional varUser As Variant, Optional varPw As Variant)
Dim cmd As String
On Error Resume Next
Set objAccess = GetObject(, "Access.Application")
If Err <> 0 Then 'no instance of Access is open
If IsMissing(varUser) Then varUser = "Admin"
cmd = "C:\Program Files\Microsoft Office\Office\MSAccess.exe"
cmd = cmd & " /nostartup /user " & varUser
If Not IsMissing(varPw) Then cmd = cmd & " /pwd " & varPw
Shell pathname:=cmd, windowstyle:=6
Do 'Wait for shelled process to finish.
Err = 0
Set objAccess = GetObject(, "Access.Application")
Loop While Err <> 0
End If
End Sub
'----------------------------------------------------------------------
'DECLARATIONS
'----------------------------------------------------------------------
Option Explicit
'----------------------------------------------------------------------
'This procedure prints or previews a report, and then closes the current
'instance of Microsoft Access (because objAccess is a procedure-level
'variable). To call this procedure, use the following syntax:
' PrintAccessReport _
' dbname:= _
' "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb",
' rptname:="Sales by Category", preview:=True
'----------------------------------------------------------------------
Sub PrintAccessReport(dbname As String, rptname As String, _
preview As Boolean)
Dim objAccess As Object
On Error GoTo PrintAccessReport_ErrHandler
Set objAccess = CreateObject("Access.Application")
With objAccess
.OpenCurrentDatabase filepath:=dbname
If preview Then 'Preview report on screen.
.Visible = True
.DoCmd.OpenReport reportname:=rptname, _
view:=Access.acPreview
Else 'Print report to printer.
.DoCmd.OpenReport reportname:=rptname, _
view:=Access.acNormal
DoEvents 'Allow report to be sent to printer.
End If
End With
Set objAccess = Nothing
Exit Sub
PrintAccessReport_ErrHandler:
MsgBox Error$(), , "Print Access Report"
End Sub
'----------------------------------------------------------------------
'DECLARATIONS
'----------------------------------------------------------------------
Option Explicit
Dim objAccess as Object
'----------------------------------------------------------------------
'This procedure starts the Report Wizard in Microsoft Access using a
'specified database and table (or query) as the record source. This
'procedure does not close the instance of Microsoft Access because
'objAccess is a module-level variable. To call this procedure, use the
'following syntax:
' CallReportWizard _
' dbname:= _
' "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb",
' sourcetype:="table", sourcename:="Employees"
'----------------------------------------------------------------------
Sub CallReportWizard(dbname As String, sourcetype As String, _
sourcename As String)
Dim objtype As Integer
On Error GoTo CallReportWizard_ErrHandler
Set objAccess = CreateObject("Access.Application")
With objAccess
.Visible = True
.OpenCurrentDatabase filepath:=dbname
If LCase(sourcetype) = "table" Then
objtype = Access.acTable
Else
objtype = Access.acQuery
End If
.DoCmd.SelectObject objecttype:=objtype, _
objectname:=sourcename, inDatabaseWindow:=True
'Although the following line of code works in Microsoft Access 97,
'DoMenuItem exists only for backward compatibility. In Microsoft
'Access 97, you should use the following RunCommand method instead:
'.DoCmd.RunCommand (acCmdNewObjectReport)
.DoCmd.DoMenuItem MenuBar:=1, MenuName:=3, Command:=3, _
Version:=Access.acMenuVer70
'Database menubar, Insert menu, Report command
End With
Exit Sub
CallReportWizard_ErrHandler:
If Err <> 2501 Then 'Error did not occur by canceling Report Wizard.
MsgBox Error$(), , "Call Report Wizard"
End If
End Sub
145707 ACC: How to Use Automation to Print Microsoft Access Reports
192919 HOWTO: Automate a Secured Access Database Using Visual Basic
Additional query words: runtime
Keywords: kbfaq kbinfo kbprogramming KB147816