in VBA use Office.GetFolder to Browse to  folder Ms Access Gurus

Help support this site, thank you.

Get Folder with the Office File Dialog using VBA to Browse…

VBA to open a dialog box to browse to a folder and return the path. Optionally, specify a title for the dialog box. Code uses the Office.FileDialog

In Access, you might use this when a Browse… button is clicked on a form to get a folder.

in VBA use Office.GetFolder to Browse to  folder

Quick Jump

Goto Top  


Download

Download zipped BAS file you can import into any VBA project. mod_Office_GetFolder_s4p.zip

If you have trouble with a download, you may need to unblock the ZIP file, aka remove Mark of the Web, before extracting the file. Here are steps to do that: https://msaccessgurus.com/MOTW_Unblock.htm

Goto Top  

VBA

Standard module

' module name: mod_Office_GetFolder_s4p
'*************** Code Start ***************************************************
' Purpose  : get a folder path using the Office file dialog box
'              browse to a folder, Office.FileDialog
' Author   : crystal (strive4peace)
' Code List: www.msaccessgurus.com/code.htm
' This code: https://msaccessgurus.com/VBA/Office_GetFolder.htm
' LICENSE  :
'   You may freely use and share this code, but not sell it.
'   Keep attribution. Mark your changes. Use at your own risk.
'--------------------------------------------------------------------------------
'                              Constants
'--------------------------------------------------------------------------------
'bind early or late?
Public Const gIsEarly As Boolean = True 
'set compiler directive constant
#Const IsEarly = gIsEarly 
'--------------------------------------------------------------------------------
'                              GetFolder
'--------------------------------------------------------------------------------
Function GetFolder( _ 
   Optional psTitle As String =  "Select Folder" _
   ) As String 
'crystal, strive4peace 220121, 230204
' return folder path or "" if nothing chosen
'     for example, C:\MyPath
' REFERENCE for early binding
'     Microsoft Office #.0 Object Library
'     developed with 16.0

   'initialize return value
   GetFolder =  ""
   
   'dimension FileDialog object
   #If IsEarly Then 
      Dim fDialog As Office.FileDialog 
   #Else 
       Dim fDialog  As Object 
   #End If 

   '   msoFileDialogOpen = 1
   '   msoFileDialogSaveAs = 2
   '   msoFileDialogFilePicker = 3
   '   msoFileDialogFolderPicker = 4
   
   'Set File Dialog. 4=msoFileDialogFolderPicker
   Set fDialog = Application.FileDialog(4) 
   'set Title and GetFolder
   With fDialog 
      .Title = psTitle 
      If .Show Then 
         GetFolder = .SelectedItems(1) 
      End If 
   End With 
   'release object
   Set fDialog = Nothing 
End Function 
'*************** Code End *****************************************************
' Code was generated with colors using the free Color Code add-in for Access.

Goto Top  

Reference

Microsoft

FileDialog object (Office)

FileDialog.Title property (Office)

Const statement

#Const directive

#If...Then...#Else directive

Goto Top  

Backstory

There are more ways to browse to a folder. You can also use APIs, but I prefer this method.

However, this method also locks the folder so it can't be renamed or deleted until Access is closed or you change the current directory (CurDir). Thanks to Geoff Griffith for pointing that out.

If you like this page, please let me know. Donations, big and small, are much appreciated! And they all say thank you.

Share with others

Here's the link for this page in case you want to copy it and share it with someone:

https://msaccessgurus.com/VBA/Office_GetFolder.htm

or in old browsers:
http://www.msaccessgurus.com/VBA/Office_GetFolder.htm

Get Help with Access and VBA

Let's connect and team-develop your application together. I teach you how to do it yourself. My goal is to empower you.

While we build something great together, I'll pull in code and features from my vast libraries as needed, cutting out lots of development time. I'll give you lots of links to good resources.

Access is great at many things, and can use common Office and Windows functionality. Access is access! I'd enjoy helping you if you want to learn more. Email me at training@msAccessGurus.com

~ crystal

the simplest way is best, but usually the hardest to see

Goto Top