Open Database in directory Code

Posted on 2012-08-24
Last Modified: 2012-08-24
I need help with this code

Problem is with this line "pth = GetFolder("P:\")"

I am trying to allow user to open a directory to open a database.

Sub OpenDB()
 Dim db As DAO.Database
 Dim ws As DAO.Workspace
 Dim rst As DAO.Recordset
 Set ws = DBEngine.Workspaces(0)
pth = GetFolder("P:\")
 Set db = ws.OpenDatabase _
Question by:leezac
    LVL 6

    Expert Comment

    ("P:\*.mdb") has got to be a specific database.  To clarify, are you looking to open a dialog box to allow the user to pick a database?
    LVL 1

    Expert Comment

    do you have a function called getfolder?
    LVL 1

    Accepted Solution

    Public Function getFolder() As String
       Dim fDialog As Office.FileDialog
       Dim varFile As Variant
       Dim PathName As String
       Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
       With fDialog
          ' Allow user to make multiple selections in dialog box in this case i set it to false
          .AllowMultiSelect = False
          ' Set the title of the dialog box.
          .Title = "Please select the file with the data"
          ' Clear out the current filters, and add our own.
          .Filters.Add "excel files ", "*.accdb, *.mdb"
          .Filters.Add "All Files", "*.*"
          ' Show the dialog box. If the .Show method returns True, the
          ' user picked at least one file. If the .Show method returns
          ' False, the user clicked Cancel.
          If .Show = True Then
         'Loop through each file selected and return the pathname.
              For Each varFile In .SelectedItems
                PathName = CStr(varFile)
    getFolder = PathName
             MsgBox "Procedure has been canceled"
          End If
       End With
    End Function

    Open in new window

    LVL 6

    Expert Comment

    If the answer is yes to the FilePicker question that I asked, then the answer would be as follows:

    Set db = ws.OpenDatabase(getFolder())

    I would recommend the following change to MarkVrenken's code:
    Line 21:       .Filters.Add "Access files ", "*.accdb, *.mdb"

    Other than that, code looks good.
    LVL 1

    Expert Comment

    Thanks, you are right about the adjustments, nice one!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
    Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now