ltdanp22
asked on
How to close a specific Access.Application object
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!
Thanks in advance!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
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?
ASKER
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"
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:
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