Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

Prompt a user to select location for an access database to import to excel

Latest post Fri, May 9 2008 5:53 PM by Nick Hodge. 2 replies.
  • Fri, May 9 2008 1:17 PM

    • torre751
    • Top 75 Contributor
    • Joined on Fri, May 9 2008
    • Posts 4
    • Points 100

    Prompt a user to select location for an access database to import to excel

    Is there an easy way when running this macro to prompt the user to path to the database of interest? The query will remain the same for every case; only the database that it's pulling information from will change.  I've been opening the VB editor and manually changing the location name, but it would be nice to be able to have a message box pop up asking for the location.  The things that I change manually are bold and italicized below.  This is just a part of a macro that works with the data imported.

    Thanks!

     With ActiveSheet.QueryTables.Add(Connection:= _
            "ODBC;DSN=Pittman_3_3;DBQ=I:\Sharea\Brown\Pittman_3_3.mdb;DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;" _
            , Destination:=Range("A1"))
            .CommandText = Array( _
            "SELECT AC_PROPERTY.APINUM, AC_PROPERTY.PROPNAME, AC_PROPERTY.PUDNAME, AC_PROPERTY.LOC_SEC, AC_PROPERTY.LOC_TN, AC_PROPERTY.LOC_RN, AC_PROPERTY.OPERNAME, AC_PROPERTY.RESERVOIR, AC_ONELINE.M25 AS 'Gas C" _
            , _
            "UM', AC_ONELINE.M22 AS 'Gas EUR', AC_ONELINE.M23 AS 'Oil ', AC_ONELINE.M21 AS 'Oil EUR'" & Chr(13) & "" & Chr(10) & "FROM `I:\Sharea\Brown\Pittman_3_3.mdb`.AC_ONELINE AC_ONELINE, `I:\Sharea\Brown\Pittman_3_3.mdb`.AC_PROPERTY AC_P" _
            , "ROPERTY" & Chr(13) & "" & Chr(10) & "WHERE AC_PROPERTY.PROPNUM = AC_ONELINE.PROPNUM")
            .Name = "Query for Pittman_3_3.mdb"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = True
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .PreserveColumnInfo = True
            .Refresh BackgroundQuery:=False
        End With

    Filed under: ,
    • Post Points: 37
  • Fri, May 9 2008 3:30 PM In reply to

    Re: Prompt a user to select location for an access database to import to excel

    Take a look at FileDialog in VBA help. It opnes a browser in VBA and allows the user to select a folder/file, which value can be captured and used in your code. It has a folder option.

    Regards

    Bob

    • Post Points: 5
  • Fri, May 9 2008 5:53 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 234
    • Points 3,866
    • MVP

    Re: Prompt a user to select location for an access database to import to excel

     torre751

    You can use

    Sub test()
    Dim fname As String
    fname = Application.GetOpenFilename("Access Files, *.mdb")
    'Do something with fName here
    End Sub

    Hopefully you can work it out from there

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    • Post Points: 5
Page 1 of 1 (3 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.