×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Using the VBA Common Control returning a folder+path

Using the VBA Common Control returning a folder+path

Using the VBA Common Control returning a folder+path

(OP)
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
http://www.weldinginspectionsvcs.com

RE: Using the VBA Common Control returning a folder+path

I have used this in some Excel VBA code:

strPath = ActiveWorkbook.Path

RE: Using the VBA Common Control returning a folder+path



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

RE: Using the VBA Common Control returning a folder+path

(OP)
Thanks for the info but the curdir function does not return the directory that the fille control is in unless I select a file  in it.

Gerald Austin
Iuka, Mississippi
http://www.weldinginspectionsvcs.com

RE: Using the VBA Common Control returning a folder+path

Yes there is a way - I found it on Joseph Rubin's ExcelTip.com page:
http://www.exceltip.com/show_tip/Files,_Workbook,_...

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

RE: Using the VBA Common Control returning a folder+path

(OP)
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
http://www.weldinginspectionsvcs.com

RE: Using the VBA Common Control returning a folder+path

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources