VBA to Open Access Database

jdawg_1989

New Member
Joined
Jun 24, 2011
Messages
27
Afternoon All,

I have some VBA code that looks to see if an Access Database is open, if it is it switch's to the database.

If not it opens up the Access DB

The problem is, it opens the access DB and then it closes straight away, and I can't understand why.

Here is my code:

Code:
Global oApp As Object

    Sub OpenAccess()
    On Error Resume Next

        Dim LPath As String
        Dim LCategoryID As Long
        Dim DATABASE As String
        Dim oApp As Access.Application
        
        DATABASE = "C:\Users\Joe Bloggs\Desktop\Databases\DB1\DB1.mdb"

        Set oApp = GetObject(, "Access.Application")
        If (Err.Number <> 0) _
        Or (oApp.CurrentDb.Name <> DATABASE) Then
        
        Set oApp = Nothing
        Set oApp = CreateObject("Access.Application")
            oApp.Visible = True
            oApp.OpenCurrentDatabase DATABASE
            
            Set oApp = CreateObject("Access.Application")
            oApp.Visible = True
            oApp.OpenCurrentDatabase DATABASE
            
        End If

        'Open form called Categories filtering by CategoryID
        LLocation = Range("A2").Value
        oApp.DoCmd.OpenForm "ReviewEBDOrdersFrm", , , "[Circuit]=" & "'" & LLocation & "'"

    End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You've correctly declared oApp as Global, but then you created another variable called oApp inside OpenAccess() and used that. As it's local to OpenAccess() it gets destroyed when OpenAccess() ends.

Remove the Dim statement for oApp.
 
Upvote 0
Or remove the Global declaration and change Dim to Static where you declare oApp.
 
Upvote 0
We make a good pair then, because I'm making it up as I go along! :)
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top