Consulting

Results 1 to 6 of 6

Thread: Open a database using vba

  1. #1
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location

    Open a database using vba

    I keep getting a runtime error: 7866

    The debug flags "oApp.OpenCurrentDatabase LPath"

    any ideas?


    [VBA]
    Private Sub CommandButton1_Click()

    Dim LPath As String

    'Path to Access database
    LPath = CurDir & "\QA.mdb"

    'Open Access and make visible
    Set oApp = CreateObject("Access.Application")
    oApp.Visible = True

    'Open Access database as defined by LPath variable
    oApp.OpenCurrentDatabase LPath


    End Sub[/VBA]

  2. #2
    Quote Originally Posted by sassora
    I keep getting a runtime error: 7866

    The debug flags "oApp.OpenCurrentDatabase LPath"

    any ideas?


    [vba]
    Private Sub CommandButton1_Click()

    Dim LPath As String

    'Path to Access database
    LPath = CurDir & "\QA.mdb"

    'Open Access and make visible
    Set oApp = CreateObject("Access.Application")
    oApp.Visible = True

    'Open Access database as defined by LPath variable
    oApp.OpenCurrentDatabase LPath


    End Sub[/vba]
    I am very new to Excel VB, but I think you might need a with statement.

    with oApp
    .opencurrentdb LPath

    'do stuff

    End with

    I am assuming that you are going to do something in Access and then pass control back to Excel. This also assumes the rest of your code is correct and that oApp is a public variable as you don't have it dim'd here.

    HTH
    Roger

  3. #3
    VBAX Contributor
    Joined
    Aug 2006
    Location
    Hampshire, UK
    Posts
    140
    Location
    Hi

    Do you actually have a database named "QA.mdb" at the given location?

    Richard

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Also, are you sure CurDir ios pointig where you think it is?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Yes the QA database is there and the code works if you use a string path for example "C:\Windows" but I want it variable so that the code will work from any folder I choose.

  6. #6
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    msgbox(CurDir) gives the correct location but after running it seems to point to My dcuments. I have placed a copy of it in there too but no luck.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •