Populate combobox in access form with filenames from folder.

Posted on 2012-08-31
Last Modified: 2012-09-04
Just trying to see if there is an easy way to have the list in a combobox be populated from all the filenames in a specific folder?

Thanks in advance for the help!
Question by:SeyerIT
    LVL 47

    Expert Comment

    by:Dale Fye (Access MVP)
    You could use a function to get the names of all the files on a path:
    Public Function fnGetFilenames(Folder As String) As String
        Dim strPath As String
        Dim strFileName As String
        strPath = Replace(Folder & "\", "\\", "\")
        strFileName = Dir(strPath & "*")
            fnGetFilenames = fnGetFilenames & ";" & strFileName
            strFileName = Dir()
        Loop While Len(strFileName) > 0
        If Len(fnGetFilenames) > 0 Then fnGetFilenames = Mid(fnGetFilenames, 2)
    End Function

    Open in new window

    Then, change the RowSourceType property of the combo box to "Value List" and use the forms Load event to populate the RowSource of the combo box, something like:

    me.comboFilenames.RowSource = fnGetFilenames("C:\")
    LVL 2

    Author Comment

    thanks for the prompt response!

    I'm hung up though;

    I think the replace code is doing something to my path.

    Here is my path;
    Me.cboFiles.RowSource = fnGetFilenames("\\sfile0\e\cnc\HURCO Tool Lists\Tool List\")

    Is it changing that "\\" to a single "\" and can that be changed somehow?
    LVL 47

    Accepted Solution

    Yes,  I explicitly added a "\" to the end of the folder name, then used the Replace function to replace dups of "\\" on the off chance that the user were to add the "\" to the end of the path.

    Try changing:

        strPath = Replace(Folder & "\", "\\", "\")


        strPath = Replace(Folder & "\", "\\", "\", 3)

    That should start the replace at the 3rd character, which should resolve the issue you are encountering by using the fully qualified network path.
    LVL 2

    Author Closing Comment

    Thanks fyed!
    LVL 47

    Expert Comment

    by:Dale Fye (Access MVP)
    glad I could help.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now