Solved

How to close a specific Access.Application object

Posted on 2010-08-22
6
547 Views
Last Modified: 2012-05-10
I'm trying to make sure that a specific Access.Application object is closed before running an Excel macro. Right now I just call Access.Application.Quit when the Excel macro starts. That works except that it closes every instance of Access instead of one. Is there a way to close an Application.Access object by file name?

Thanks in advance!
0
Comment
Question by:ltdanp22
6 Comments
 
LVL 31

Expert Comment

by:Helen_Feddema
ID: 33501622
If you assign the Application object to a variable, say appAccess, then you can close that specific instance using the variable.  If you are talking about a file name, that would be a database.  Here is some code for working with the Access Application object and a specific database:
Public Sub OpenAnotherDatabase()
'Created by Helen Feddema 14-Feb-2010
'Last modified by Helen Feddema 14-Feb-2010

   Dim appAccess As New Access.Application
   Dim strDBNameAndPath As String
   Dim dbs As DAO.Database
   Dim rst As DAO.Recordset
   Dim dbe As DAO.DBEngine
   
   'Change to your db name and path
   strDBNameAndPath = "G:\Documents\Access 2002-2003 Databases\General.mdb"
   appAccess.Visible = True
   appAccess.OpenCurrentDatabase filepath:=strDBNameAndPath, _
      exclusive:=False
      
   'Run a procedure
   'appAccess.Run "PrintOrdersReport"
   
   'Run a macro
   'appAccess.DoCmd.RunMacro "mcrPrintOrdersReport"
   
   'Run an action query
   'appAccess.DoCmd.OpenQuery "qryDeleteSomeOrders"
   
   'Run SQL code
   strSQL = "DELETE tblOrders.ShippedDate FROM tblOrders WHERE ShippedDate = #8/4/1994#;"
   Debug.Print "SQL string: " & strSQL
   'appAccess.DoCmd.RunSQL strSQL
   
   'Iterate through a recordset
   Set dbe = appAccess.DBEngine
   Set dbs = dbe.OpenDatabase(strDBNameAndPath)
    
   Set rst = dbs.OpenRecordset("tblCategories")
   Do Until rst.EOF
      Debug.Print rst![CategoryName]
      rst.MoveNext
   Loop
   rst.Close
    
   Set dbs = Nothing
   Set appAccess = Nothing
   
End Sub

Open in new window

0
 
LVL 38

Accepted Solution

by:
puppydogbuddy earned 250 total points
ID: 33501798
The trick is to determine if there is an open instance of the automation object before creating a new instance.  This applies equally to any automation object. The way it works is thru the use of the GetObject method to attempt to grab an open instance of the object (e.g. Access). If there is no open instance, the application will return an error #429; at which point the code will resume and create a new instance using the CreateObject method.  You therefore, can limit the number of instances opened to one, and can close the open instance before exiting the procedure.
                 See   http://support.microsoft.com/kb/288902 
     
0
 
LVL 15

Assisted Solution

by:dbbishop
dbbishop earned 250 total points
ID: 33502729
When you execute Access.Application.Quit, in this context 'Access' is referring to the object you set up tp reference a specific Access database. Not sure what you mean by closing an application without closing all instances of Access.

Unless you used GetObject to grab an already open instance of Access, CreateObject shoud create a new instance of Access and your Quit method should only work against the new instance, and not any existing instances.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:ltdanp22
ID: 33581143
To recap, I have an Excel macro that opens an Access database. If the code breaks, this instance of Access is left open (it's also invisible which is why I'm trying to close it for the user).
The file will always have the same name: "ISCM Database.accdb". Unfortunately, the path to the Access file is not always. I've only seen examples that pass the full file path as a parameter.
Is it possible to use GetObject with only the file name and not the full file path? If so, what's the correct syntax?
0
 

Author Comment

by:ltdanp22
ID: 33581174
Is it possible to loop through all the open Access objects and check the filename, and then get the file path if the file name matches "ISCM Database.accdb"
0
 
LVL 1

Expert Comment

by:JamesCronshaw1
ID: 33626169
If your code breaks for any reason, you shouldn't need to know which/what databases are currently open, since you have created a specific object/connection to the database you are working within("ISCM Database.accdb").

Upon a break, the error handling you should have in place would clean up the issue for you.

E.g. (note: borrowing some of Helen's code posted) this would be a very basic way of handling things:
Public Sub OpenDatabase()

Dim appAccess As Access.Application
Dim strDBName As String

On Error Goto ErrHandler

    Set appAccess = New Access.Application

'-- Set the filepath/name
    strDBName = "...ISCM Database.accdb"

'-- Open the database
    appAccess.OpenCurrentDatabase filepath:=strDBName, _
                                  exclusive:=False

'-- Display (or hide) the app window
    appAccess.Visible = True

'-- Do stuff......


'-- Close the database
    Set appAccess = Nothing
    
Exit Sub

ErrHandler:

'-- Do something with the error
    msgbox Err.Number & " - " & Err.Description

'-- Close the database
    Set appAccess = Nothing

End Sub

Open in new window

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Modal form 11 29
User Level Security 6 38
Access Open Report with SQL Parameter 11 29
SQL Group on First occurrence 9 24
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question