Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

Using the VBA Common Control returning a folder+path 1

Status
Not open for further replies.

pipewelder1999

Industrial
Mar 17, 2002
305
0
0
US
Is there a way to browse to a folder using access/vba and store that path in a field. I can currently get the filename fine but was wondering if the same can be done with a folder.

I have stripped the filename off the complete file path but that requires the user to select a file within a folder before the path to the folder is stored.

Thanks

Gerald Austin
Iuka, Mississippi
 
Replies continue below

Recommended for you



I'm not sure if this is what you are looking for, but the access function CurDir should give you the Current browsed path.

HTH.

_LF
 
Yes there is a way - I found it on Joseph Rubin's ExcelTip.com page:

I am reproducing the code with slight changes to meet your requirements - copy the code to a module and run the Sub GetFolderName - you can store the returned folder name wherever you like.

Code:
Private Type BROWSEINFO ' used by the function GetFolderName
    hOwner As Long
    pidlRoot As Long
    pszDisplayName As String
    lpszTitle As String
    ulFlags As Long
    lpfn As Long
    lParam As Long
    iImage As Long
End Type


Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
    Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
    Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Sub GetFolderName()
Dim FolderName As String
    FolderName = GetFolderName("Select a folder")
    If FolderName <> "" Then
      ActiveCell.Value = FolderName
    End If
End Sub


Function FuncGetFolderName(Msg As String) As String
' returns the name of the folder selected by the user
Dim bInfo As BROWSEINFO, path As String, r As Long
Dim X As Long, pos As Integer
    bInfo.pidlRoot = 0& ' Root folder = Desktop
    If IsMissing(Msg) Then
        bInfo.lpszTitle = "Select a folder."
        ' the dialog title
    Else
        bInfo.lpszTitle = Msg ' the dialog title
    End If
    bInfo.ulFlags = &H1 ' Type of directory to return
    X = SHBrowseForFolder(bInfo) ' display the dialog
    ' Parse the result
    path = Space$(512)
    r = SHGetPathFromIDList(ByVal X, ByVal path)
    If r Then
        pos = InStr(path, Chr$(0))
        GetFolderName = Left(path, pos - 1)
    Else
        GetFolderName = ""
    End If
End Function

'Dare to Imagine'
Mala Singh
 
Thanks Mala and all others. The code had to be modified as the sub called itself instead of the function.

Here is the modified code.

Private Type BROWSEINFO ' used by the function GetFolderName
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type


Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long


Sub GetFolderName()
Dim FolderName As String
FolderName = FuncGetFolderName("Select a folder")
If FolderName <> "" Then
MsgBox (FolderName)
End If
End Sub


Gerald Austin
Iuka, Mississippi
 
Sorry, it was an oversight - after testing, I modified the original Function name and the Sub name, forgot to update the reference to the Function within the sub and then didn't test again before posting it!

Thanks for posting the correction.

'Dare to Imagine'
Mala Singh
 
Status
Not open for further replies.
Back
Top