How to close Access from Excel VBA

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I'm programming a macro in Excel and would like the code to first shut down
MS Access if it is already open before proceeding throught the script.

How can I close Access from Excel in that manner?
 
Generic code:

Dim accAccess As Object
On Error Resume Next
Set accAccess = GetObject(, "Access.Application")
If Err.Number = 0 Then
accAccess.Quit
Else
Err.Clear
End If


Are you sure you want to close ACCESS so abruptly, though? What if the user
is in the middle of doing something?
 
Hi Steve,

As Ken says, closing an application like that can cause problems.
Maybe do something like this:

Dim accAccess As Object

On Error Resume Next
Set accAccess = GetObject(, "Access.Application")
If Err.Number = 0 Then
If accAccess.UserControl Then
'This instance of Access was launched by the user.
'Maybe we shouldn't close it.
...
Else
'This instance was launched by an automation process
'Maybe we can close it safely
accAccess.Quit
End If
Else
Err.Clear
End If

But in general I'd be inclined
(1) if possible to leave Access out of the picture. Most data
extraction and manipulation can be done by using the DAO or ADODB
libraries from Excel VBA without having to automate Access.
(2) when necessary, have the Excel code use CreateObject to open its
own instance of Access independent of anything else that might be
running.

See http://support.microsoft.com/?id=210111 for more information on
automating Access.
 
Back
Top