Re: Simple VBA code to open other access database

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: JasonS (some_address_at_microsoft.com)
Date: 09/27/04


Date: Tue, 28 Sep 2004 00:15:23 +0200

This was inclided in Access 2003 help, how to open another database using
VBA

You can use this method to open a database from another application that is
controlling Microsoft Access through Automation, formerly called OLE
Automation. For example, you can use the OpenCurrentDatabase method from
Microsoft Excel to open the Northwind.mdb sample database in the Microsoft
Access window. Once you have created an instance of Microsoft Access from
another application, you must also create a new database or specify a
particular database to open. This database opens in the Microsoft Access
window.

Note Use the OpenAccessProject method to open an existing Microsoft Access
project (.adp) as the current database.

If you have already opened a database and wish to open another database in
the Microsoft Access window, you can use the CloseCurrentDatabase method to
close the first database before opening another.

Set the Exclusive argument to True to open the database in exclusive mode.
If you omit this argument, the database will open in shared mode.

Note Don't confuse the OpenCurrentDatabase method with the ActiveX Data
Objects (ADO) Open method or the Data Access Object (DAO) OpenDatabase
method. The OpenCurrentDatabase method opens a database in the Microsoft
Access window. The ADO Open method returns a Connection object variable, and
the DAO OpenDatabase method returns a Database object variable, both of
which represent a particular database but don't actually open that database
in the Microsoft Access window.

Example
The following example opens a Microsoft Access database from another
application through Automation and then opens a form in that database.

You can enter this code in a Visual Basic module in any application that can
act as a COM component. For example, you might run the following code from
Microsoft Excel, Microsoft Visual Basic, or Microsoft Access.

When the variable pointing to the Application object goes out of scope, the
instance of Microsoft Access that it represents closes as well. Therefore,
you should declare this variable at the module level.

' Include the following in Declarations section of module.
Dim appAccess As Access.Application

Sub DisplayForm()

    Dim strDB as String

    ' Initialize string to database path.
    Const strConPathToSamples = "C:\Program " _
        & "Files\Microsoft Office\Office11\Samples\"

    strDB = strConPathToSamples & "Northwind.mdb"
    ' Create new instance of Microsoft Access.
    Set appAccess = _
        CreateObject("Access.Application")
    ' Open database in Microsoft Access window.
    appAccess.OpenCurrentDatabase strDB
    ' Open Orders form.
    appAccess.DoCmd.OpenForm "Orders"
End Sub

"Berlin Brown" <berlin.brown@gmail.com> wrote in message
news:1096298537.752411.291050@k17g2000odb.googlegroups.com...
> I am trying to open another access database from a form, what is the
> simplest way to do that.
>



Relevant Pages

  • Re: Access 2002 command line options.
    ... > database Opens the specified database. ... > /excl Opens the specified database for exclusive access. ... > /user user name Starts Microsoft Access using the specified user name. ... To compact to a different name, specify a target ...
    (microsoft.public.access.developers.toolkitode)
  • Re: Converting from Access 2000 to Access 2002
    ... If you can still open these forms in design view and view their code window, ... then close this database, and open the new one that lacks the code. ... with the message "Microsoft Access has encountered a problem..." ... converted database, it initially opens but, when I try to use it, I ...
    (microsoft.public.access.conversion)
  • Re: Converting from Access 2000 to Access 2002
    ... I wonder how Access 2002 got through the beta program with such a ... then close this database, and open the new one that lacks the code. ... with the message "Microsoft Access has encountered a problem..." ... converted database, it initially opens but, when I try to use it, I ...
    (microsoft.public.access.conversion)
  • Re: Tracking Log In and Log Out times of Users
    ... The fact that the database is on the server should be irrelevant. ... Type mismatch and then highlights Set rst = ... On my startup form, (that opens every time the database is open, ... I then went to the load ...
    (microsoft.public.access.modulesdaovba)
  • RE: Compacting MDB help
    ... The autoexec macro opens a ... The FrontEnd.mdb's main switchboard has a cbo to switch back end ... FECompact.mdb runs the few lines of code to compact the FrontEnd.mdb ... I'm quite certain that you can't Compact an open database from within itself ...
    (microsoft.public.access.modulesdaovba)