Browse for Folder - absolute path

rodmc

Registered User.
Local time
Today, 13:47
Joined
Apr 15, 2010
Messages
514
Hi Guys

Ive just used Terry Krefts code to "browse for a folder path" and then store that folder path in a field in a table. This code works absolutely fine apart from one thing. When browsing to network shares, the function stores the relative path rather than the absolute path, so what is being stored is for example is the drive assigned letter on my PC ie M:\Folder, when really what I want is servername\share\folder

any help would be greatly appreciated!
 
Terry Krefts Module

Code:
Option Compare Database

'************** Code Start **************
'This code was originally written by Terry Kreft.
'It is not to be altered or distributed,
'except as part of an application.
'You are free to use it in any application,
'provided the copyright notice is left unchanged.
'
'Code courtesy of
'Terry Kreft

Private Type BROWSEINFO
  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
            
Private Const BIF_RETURNONLYFSDIRS = &H1
Public Function BrowseFolder(szDialogTitle As String) As String
  Dim X As Long, bi As BROWSEINFO, dwIList As Long
  Dim szPath As String, wPos As Integer
  
    With bi
        .hOwner = hWndAccessApp
        .lpszTitle = szDialogTitle
        .ulFlags = BIF_RETURNONLYFSDIRS
    End With
    
    dwIList = SHBrowseForFolder(bi)
    szPath = Space$(512)
    X = SHGetPathFromIDList(ByVal dwIList, ByVal szPath)
    
    If X Then
        wPos = InStr(szPath, Chr(0))
        BrowseFolder = Left$(szPath, wPos - 1)
    Else
        BrowseFolder = vbNullString
    End If
End Function
'*********** Code End *****************

The code in the form

Code:
Private Sub btnBrowseForFolder_Click()

    Dim strFolderName As String

    strFolderName = BrowseFolder("Choose Folder For Import")

    If Len(strFolderName) > 0 Then
        txtFolderPath = strFolderName
    Else
        'No folder chosen, or user canceled
    End If

End Sub

Like I said if Im browsing the local PC for folders it works fine, its when I browse for a network share the function uses the drive letter that is assigned to that share on my PC.
 
The only way I can see is to take the path that it is saving (shared path) and rework it to input the server path (\\server\whatever). While having a condition there to check if it is the mapper drive on your computer (ex S:\).

Code:
If X Then
        wPos = InStr(szPath, Chr(0))
              If(Left$(szPath, 3) = "S:\") Then

               PathWithOutDriveLetter = Right$(szPath, Len(szPath) - 3) 
               BrowserFolder= "\\Server\" + PathWithOutDriveLetter
               Else
           
                BrowseFolder = Left$(szPath, wPos - 1)
                End If

    Else
        BrowseFolder = vbNullString
    End If
Let me go over what I did and hopefully I got it right..hehe

Left$(szPath, 3) = "S:\" //This will read the path starting form the Left 3 spots and compare it to "S:\" or whatever your mapped drive is.

PathWithOutDriveLetter = Right$(szPath, Len(szPath) - 3) // This will read the path starting from the right (the end) and only return the part without the front 3 characters (S:\). So we are left with only the folders path


BrowserFolder= "\\Server\" + PathWithOutDriveLetter

Now we will add the the server path to the front.

BrowerFolder should now read as \\Server\folder\folder\folder.
 
Last edited:
thanks, I'll give it a try later, would do it now but Ive kicked off a few beers :D
 

Users who are viewing this thread

Back
Top Bottom