Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code to choose directory

Status
Not open for further replies.

KeyserSoze

Programmer
May 18, 2000
79
US
Is there code in Microsoft Access 2003 that will display a directory tree dialog box that will allow a user to choose a directory and populate a text box with the name of the chosen directory?

Thanks!
 
set refrance to microsoft Shell controls and automation

c:\windows\system32\shell32.dll
Code:
Function SelectAFolder(Title, TopFolder) As String
Dim objShell As Shell
Dim objFolder As Shell32.Folder
Dim objFolderItem As Shell32.FolderItem

Const WINDOW_HANDLE = 0
Const OPTIONS = 1

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, Title, OPTIONS, TopFolder)

If objFolder Is Nothing Then
   Exit Function
End If

Set objFolderItem = objFolder.Self
SelectAFolder = objFolderItem.Path

'Debug.Print objPath

End Function

call with

Code:
DirStr = SelectAFolder("title", top folder name)
 
I've already posted the following little function:
Code:
Function PickFolder(strStartDir As Variant) As String
Dim SA As Object, f As Object
Set SA = CreateObject("Shell.Application")
Set f = SA.BrowseForFolder(0, "Choose a folder", 16 + 32 + 64, strStartDir)
If (Not f Is Nothing) Then
  PickFolder = f.Items.Item.path
End If
Set f = Nothing
Set SA = Nothing
End Function

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I've tried both methods and they work great. However, the code does not give you the ability to choose a disk drive letter. Can the code be tweaked to accomplish this?

Thanks!
 
in the top folder prameater pass the diskdrive that you want to Select
Code:
A=SelectAFolder("Select Z Drive", "z:\")
 
pwise:

I was looking for something that would display all of the available drives in the folder tree. If I can load the drives into a drop-down box and allow the user to choose the drive before calling the routine, that would work, too.
 
Code:
Dim objShell As Shell
Dim objFolder As Shell32.Folder
Dim objFolderItem As Shell32.FolderItem

Const WINDOW_HANDLE = 0
Const OPTIONS = 0

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(17)
Set objFolderItem = objFolder.Self
SelectAFolder = objFolderItem.Path

Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, Title, OPTIONS, SelectAFolder)
If objFolder Is Nothing Then
   Exit Function
End If

Set objFolderItem = objFolder.Self
SelectAFolder = objFolderItem.Path
 
On PHV's code you can type:

Code:
Debug.Print PickFolder("My Computer")

to get all drives available.

-Neema
 
PWise:
I've used your SelectAFolder function and like how it only displays the desired folder and its subdirectories, unlike some of the other options out there. I have an application where users are externally storing images that are referenced in my database and I want them to only be able to select the folder or its subfolders.

However, I am using Access 2003 on Windows Vista and now my client is having trouble using it on his XP machine. (I recently upgraded to Vista and my previous version of the db worked fine for the client. The code worked as it should. I made modifications to the same form so originally thought the problem was something else I'd done.) I put the db on my laptop (running XP) and encountered the same issue -- an "Error 13: Type mismatch".

First, the error occurred on the line:
Set objShell = CreateObject("Shell.Application")
I checked the references, compiled the code again, and then the error occurred on the line:
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, Title, OPTIONS, TopFolder)

Any suggestions? Thanks!

Diana
VBA Princess
-- I'm hoping to grow up to be a Goddess!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top