Search code examples
vbams-accessdirectorypickerfiledialog

MS Access FileDialog results in root folder


I'm using the FileDialog to select a folder for use with OutputTo. I have coded the file name. When I select a folder, I get a properly format string ie: "C:\Documents\export.xls". However, when I select the root folder, I get a improper format ie: "C:\\export.xls". Note the double slashes.

Any thoughts on what is causing this behavior?

Function selectFolder()
Dim fdf As FileDialog, FolderName As String


On Error GoTo ErrorHandler
 

Set fdf = Application.FileDialog(msoFileDialogFolderPicker)

fdf.InitialFileName = getdbpath
 
fdf.AllowMultiSelect = False
 
If fdf.Show = True Then
    If fdf.SelectedItems(1) <> vbNullString Then
        FolderName = fdf.SelectedItems(1)
    End If
Else
    'Exit code if no file is selected
    End
End If
 
'Return Selected FileName

selectFolder = FolderName & "\AccountOutput.xls"
'Debug.Print FolderName

Set fdf = Nothing

Exit Function

ErrorHandler:
Set fdf = Nothing
MsgBox "Error " & Err & ": " & Error(Err)
 
End Function

Solution

  • This is how the folder picker works - you need to handle both cases, e.g.

    If Right$(FolderName, 1) <> "\" Then
        FolderName = FolderName & "\"
    End If
    
    selectFolder = FolderName & "AccountOutput.xls"
    

    See e.g.

    https://superuser.com/questions/270418/check-for-trailing-in-string-returned-from-msofiledialogfolderpicker-excel-vb