Link to home
Start Free TrialLog in
Avatar of Rob4077
Rob4077Flag for Australia

asked on

Access equivalent of Excel: strVarName = Application.GetOpenFilename( fileFilter:="Excel Files (*.xls), *.xls")

In VBA for Excel, when I want to get the user to select a file and assign its path and name to a variable I use the command:
strVarName = Application.GetOpenFilename( fileFilter:="Excel Files (*.xls), *.xls")
Is there an Access equivalent?
SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or you can give this a whirl (from Excel VBA,so you may have to modify it slightly)...

    'Declare a variable as a FileDialog object.
    Dim fd As Office.FileDialog
   
    Dim xla As Excel.Application
    Set xla = GetObject(, "Excel.Application")

    'Create a FileDialog object as a File Picker dialog box.
    Set fd = xla.FileDialog(Office.msoFileDialogOpen)
   
    fd.AllowMultiSelect = True
   
    fd.Title = strTitle
   
    fd.InitialView = Office.msoFileDialogViewList
   
    fd.Filters.Clear
    fd.Filters.Add "Access databases", "*.mdb"
    fd.Filters.Add "Excel Files (*.xls), "*.xls")
    fd.Filters.Add "Whatever Files (*.wha), "*.wha")
   
    fd.Show
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yeah - that's the standard solution (courtesy of the Access Web)... but it's not the "equivalent"  ;-)

The above fGetFileName should work in an MDE though.
And indeed - in runtime (assuming there are some Office components present with it).

What you might struggle with is needing a reference to the Office library - but that'd be due to the constant used...

Set objFD = Application.FileDialog(1)

should do that...
(FWIW I use the API though ;-)
Avatar of stevbe
stevbe

we have Office on all our PCs but only Access Runtime, distributed mde, and the above (non-API) does not work.
Does it work on your machine with the Runtime startup command line switch Steve?
Avatar of Rob4077

ASKER

Thanks for all the suggestions. It seems there are many solutions to my question. I have chosen the code supplied by stevebe but I am sure that all the other solutions have their merits so I will share the points around. Thanks for your help.