How to quit and open a different AccessDB

cricketbird

Registered User.
Local time
Today, 13:47
Joined
Jun 17, 2013
Messages
108
I am trying to code a button that will quit the current Access database (A) and open another Access database (B). I can open database B just fine, but the quit command seems to close B instead of A. Database A knows where B is (so I can open it specifically), but B does not know where A is (unless I can pass that through in some way), so I can't hard-code that in the same way.

How can I quit A and open B?


Button Code from Database A:
Code:
dim destDB as string
destDB = "J:/path/to/database/B/"

Dim objAccess As Object
Set objAccess = GetObject(destDB) 'opens other db

'None of these work (they all seem to take action in Database B, not A, even if called from A).
    ' Me.Visible = True
    'CurrentDb.Close
    'DoCmd.Close
    'DoCmd.Quit
 
Try this code - it includes various alternatives for you to choose from

Code:
Public Function RunExternalDatabase() As Boolean
    
    Dim app As Access.Application, strPath As String
    'Start a new MSAccess application
    Set app = New Access.Application
    
    'Open the remote database, then close the current (or remote) database
    With app
        'Syntax: .OpenCurrentDatabase(filepath, Exclusive, strPassword) - the last 2 items are optional
         strPath = CurrentProject.Path & "\MainApp.accdb" 'full file path to your database
         .OpenCurrentDatabase strPath, False 'no db password
         .Visible = True
         .UserControl = True
     '   .OpenCurrentDatabase strPath, True, "password" 'for use if password exists
    
       ' .CloseCurrentDatabase 'closes external database as that is current
    End With
    
    'Quit the spawned app - DISABLED as not wanted here
    'app.Quit acQuitSaveNone
    'Set app = Nothing
    
    'Quit the current app - DISABLED as not wanted here
   Application.Quit acQuitSaveNone
    
End Function

See attached. You may need to click the Access icon in the taskbar to see the second DB
 

Attachments

Try this code - it includes various alternatives for you to choose from

Code:
Public Function RunExternalDatabase() As Boolean
   
    Dim app As Access.Application, strPath As String
    'Start a new MSAccess application
    Set app = New Access.Application
   
    'Open the remote database, then close the current (or remote) database
    With app
        'Syntax: .OpenCurrentDatabase(filepath, Exclusive, strPassword) - the last 2 items are optional
         strPath = CurrentProject.Path & "\MainApp.accdb" 'full file path to your database
         .OpenCurrentDatabase strPath, False 'no db password
         .Visible = True
         .UserControl = True
     '   .OpenCurrentDatabase strPath, True, "password" 'for use if password exists
   
       ' .CloseCurrentDatabase 'closes external database as that is current
    End With
   
    'Quit the spawned app - DISABLED as not wanted here
    'app.Quit acQuitSaveNone
    'Set app = Nothing
   
    'Quit the current app - DISABLED as not wanted here
   Application.Quit acQuitSaveNone
   
End Function

See attached. You may need to click the Access icon in the taskbar to see the second DB
Thank you! This worked perfectly.
 
You’re welcome. I use variations of this code in various apps which is why I gave the alternatives
 
quit the current Access database
Code:
MsgBox CurrentDb.Name  ' you should know where you are
Application.Quit

Application is the application object of the access file in which the code runs.

Jumping between different access files is a more complex task and should not be a playground for beginners who don't know where they are and what they actually do.
 
You’re welcome. I use variations of this code in various apps which is why I gave the alternatives
I spoke too soon. The second database opens, but as a copy of itself, so changes made aren't saved. Is there a way to launch it as its own entity and not a clone?
 
Don't open it as an app.
Try using
Code:
Application.FollowHyperlink "file://" & YourFullPathAndFilename

Which won't involve the current Access app spawning the process.
 
Code:
'Start a new MSAccess application
Set app = New Access.Application
Something like this will fail if you distribute your application and let it run under runtime conditions.
There's a lot to consider.
 
I spoke too soon. The second database opens, but as a copy of itself, so changes made aren't saved. Is there a way to launch it as its own entity and not a clone?

I don't understand. Is the second database already open with unsaved changes? If so why?
Surely changes would be made to the second database after it is opened in this process

You can always use the method @Minty mentioned as an alternative. However, I just use:

Code:
Application.FollowHyperlink "YourFullPathAndFilename"
 

Users who are viewing this thread

Back
Top Bottom