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
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
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
Gerald Austin
Iuka, Mississippi
http://www.weldinginspectionsvcs.com
RE: Using the VBA Common Control returning a folder+path
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
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
Thanks for posting the correction.
'Dare to Imagine'
Mala Singh