Selecting folders

RichardMGreen

Well-known Member
Joined
Feb 20, 2006
Messages
2,177
Hi all

I've got the following code:-
Code:
Function GetFolderName(Optional OpenAt As String) As String
    Dim lCount As Long
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = OpenAt
        .Show
            For lCount = 1 To .SelectedItems.Count
                GetFolderName = .SelectedItems(lCount)
            Next lCount
    End With
End Function

which allows users to select a folder which is used further on in the code. If you select Cancel when the folder picker appears, I get a null string returned and it causes problems further on.

How would I modify it so that if a user selects Cancel it keeps the current folder?

I've tried checking if the selected.items.count is more than 0 (placed around the for/next statement) but that's not helped.

Any help greatly appreciated.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi

Something like this maybe?

Code:
Function GetFolderName(Optional OpenAt As String) As String
    Dim lCount As Long
    Dim sCurrentFolder As String
    Dim sSelectedFolder As String
    
    sCurrentFolder = CurDir
    
    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = OpenAt
        .Show
            For lCount = 1 To .SelectedItems.Count
                sSelectedFolder = .SelectedItems(lCount)
            Next lCount
    End With
    
    If Len(sSelectedFolder) = 0 Then
        GetFolderName = sCurrentFolder
    Else
        GetFolderName = sSelectedFolder
    End If
    
    
End Function

The function will return either the folder chosen or the current folder if the user cancels (i.e. the folder that would be displayed if you chose File, Open from the application.

HTH
DK
 
Upvote 0
Thanks for that, but it's not quite what I wanted.
I was trying to get it read the contents of a field in a table where the current one is stored.

I tried reading it into a variable using docmd.runsql but it highlights the runsql section and gives me an "expected function or procedure" error.
 
Upvote 0
Richard

That's different question, if you want to grab a single value for the folder name you could try DLookUp.

Then you can use DK's code for selecting a file and dealing with the user pressing Cancel.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top