MSAccess - List folders in a directory VBA

Anonymous
2025-05-14T07:47:45+00:00

Dear All,

I need to to list all the Folders for a given folder in a VBA ListBox like this:

IT

UK

ZA

This is my folder and I need to show what I have highlighted:

I built the below code

Sub GetFolders(MyListBox As Object) 

    Dim objFSO As Object 

    Dim objFolders As Object 

    Dim objFolder As Object 

    Set objFSO = CreateObject("Scripting.FileSystemObject") 

    Set objFolders = objFSO.GetFolder("C:\Users\admin\Desktop\COUNTRY\") 

    For Each objFolder In objFolders.SubFolders 

        MyListBox.AddItem objFolder.Name 

    Next objFolder 

    Set objFSO = Nothing 

    Set objFolders = Nothing 

    Set objFolder = Nothing 

End Sub

but it seems to get a duplicated folder name like this:

Please, could you help me?

Thanks

Microsoft 365 and Office | Access | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

9 answers

Sort by: Most helpful
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2025-05-14T09:21:56+00:00

    Insert the following line above the For ... Next loop:

    MyListBox.RowSource = ""
    

    This clears the list box before adding the subfolder names.

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2025-05-14T13:58:51+00:00

    I have added the above code you suggested but I have the same issue

        For Each objFolder In objFolders.SubFolders 
    
            MyListBox.RowSource = "" 
    
            MyListBox.AddItem objFolder.Name 
    
        Next objFolder
    

    I have added the above code you suggested but I have the same issue.

    I don't understand this code goes in loop for a long even if it has to read only 3 subfolders

    Thanks

    0 comments No comments
  3. Anonymous
    2025-05-14T14:20:40+00:00

    No, he said 'above' the line

        MyListBox.RowSource = "" 
    
        For Each objFolder In objFolders.SubFolders 
    
            MyListBox.AddItem objFolder.Name 
    
        Next objFolder
    

    I'd do something more like

    Me.MyListboxName.RowSource = ""
    Call GetFolders(Me.MyListboxName)
    
    0 comments No comments
  4. Anonymous
    2025-05-14T14:57:48+00:00

    No, he said 'above' the line

        MyListBox.RowSource = "" 
    
    
    
        For Each objFolder In objFolders.SubFolders 
    
    
    
            MyListBox.AddItem objFolder.Name 
    
    
    
        Next objFolder
    

    I'd do something more like

    Me.MyListboxName.RowSource = ""
    Call GetFolders(Me.MyListboxName)
    

    Same issue:

    I really don't understand why this code goes in loop for a long time even if it has to read only 3 subfolders

    0 comments No comments
  5. HansV 462.4K Reputation points MVP Volunteer Moderator
    2025-05-14T15:51:44+00:00

    Could you create a copy of the database, strip away everything that isn't relevant to the problem, as well as all sensitive information, then make the copy available through one of the websites that let you upload and share a file, such as OneDrive, Google Drive, FileDropper or DropBox. Post a link to the uploaded and shared file in a reply here.

    0 comments No comments