Page 1 of 3 123 LastLast
Results 1 to 15 of 43
  1. #1
    Join Date
    Feb 2010
    Posts
    20

    Unanswered: populate combo box with files in a folder

    I have a folder on my desktop that has many files that are stored as PDF files.
    I have a form with a combo box that I want to grab those files names and put them in the drop down. When the user selects a file the file will open up.

    I dont want to make a table with attachment fields because there are a lot of files and they will be added to and removed from the folder on a regular basis, it would be a tedius job of keeping up with.

    Basically,
    Folder1 has files:
    file1.pdf
    file2.pdf
    file3.pdf

    I want the combo box to show
    file1.pdf
    file2.pdf
    ....ect

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You could use:
    Code:
    Function GetFileList(Optional ByVal FolderName As String, _
                         Optional ByVal FileMask As String = "*.*", _
                         Optional ListSeparator As String = ",") _
        As String
    
        Dim strList As String
        Dim strfilename As String
        
        If Len(FolderName) > 0 Then
            If Right(FolderName, 1) <> "\" Then FolderName = FolderName & "\"
        End If
        strfilename = Dir(FolderName & FileMask)
        Do While Len(strfilename)
            If Len(strList) > 0 Then strList = strList & ListSeparator
            strList = strList & strfilename
            strfilename = Dir
        Loop
        GetFileList = strList
        
    End Function
    
    Private Sub Form_Load()
    
        Dim strFolderName As String
        Dim strFileMask As String
        Dim strSeparator As String
        
        strFolderName = "C:\Documents and Settings\SinnDHo\My documents\Access"
        strFileMask = "*.mdb"
        strSeparator = ";"
    
        Me.Combo_Files.RowSourceType = "Value List"
        Me.Combo_Files.RowSource = GetFileList(strFolderName, strFileMask, strSeparator)
        
    End Sub
    Have a nice day!

  3. #3
    Join Date
    Feb 2010
    Posts
    20

    works

    Thanks for the post. Everything works good except their is one problem.
    The combo box does not list every file. It lists every other file, only half of the files in the folder.

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    It depends on what you specify for the FileMask parameter. What do you mean by "It lists every other file" ? Can you explain what the combo lists and does not?
    Have a nice day!

  5. #5
    Join Date
    Feb 2010
    Posts
    20
    Thanks again,
    The folder on my desktop has 28 pdf files in it.
    I inserted your code and the combo box populates with the file names in the folder.
    Problem,
    instead of listing every file name in the folder it lists every other file, literally every other file.
    instead of listing all 28 pdf files it only list 14 in the drop down.

    all files are the same type there is nothing different about any of them except the file name.

  6. #6
    Join Date
    Feb 2004
    Location
    New Zealand
    Posts
    1,418
    Provided Answers: 7
    Not a good place to store the PDF on you desktop

    If it's is only showing some of the files. Mite be case sentertive

    I would use the ucase() or lcase().
    Last edited by myle; 05-24-11 at 19:12.
    hope this help

    See clear as mud


    StePhan McKillen
    the aim is store once, not store multiple times
    Remember... Optimize 'til you die!
    Progaming environment:
    Access based on my own environment: DAO3.6/A97/A2000/A2003/A2007/A2010
    VB based on my own environment: vb6 sp5
    ASP based on my own environment: 5.6
    VB-NET based on my own environment started 2007
    SQL-2005 based on my own environment started 2008
    MYLE
    YOUR PASSWORD IS JUST LIKE YOUR TOOTHBRUSH DON'T SHARE IT.

  7. #7
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    If you want to list .pdf files only you have to specify it:
    Code:
    strFileMask = "*.pdf"
    Have a nice day!

  8. #8
    Join Date
    Feb 2010
    Posts
    20
    yes I put the mask as "*.pdf"
    all of the file names are numbers. The actual files are scanned files of certificates in pdf.
    they are stored with the certificate number as the file name.
    The program works great in populating the drop box and then I put the afterUpdate code to open the file, which works fine.
    for some reason only every other file populates the drop box, very weird.
    they are numbers so there is no issue with the proper case.

  9. #9
    Join Date
    Feb 2010
    Posts
    20
    ok, I figured it out.
    The combo box had a column count of 2
    the width was set to 0,1
    the combo box was listing everything in two columns and i could only see one column
    I changed to columns to 1 and the width to 1,0 and it lists everything

    thanks for your help

  10. #10
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You're welcome!
    Have a nice day!

  11. #11
    Join Date
    Aug 2011
    Location
    Indianapolis, IN, USA
    Posts
    41

    The Code

    I have been looking for some code like this so thank you very much. However, in the function there is a line of code that says "If Len(strList) > 0 ...". Shouldn't this be "If Len(strFileName) >0 ..."? I changed it and it worked for me. I don't understand the Do While Loop. Is "Do While Len(strFileName)" the same as "Do While Len(strFileName) > 0"?

  12. #12
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Quote Originally Posted by Harmanj1 View Post
    there is a line of code that says "If Len(strList) > 0 ...". Shouldn't this be "If Len(strFileName) >0 ..."?
    If Len(strList) > 0 is correct: this is used to add the list separator between every file name, except for the first one. If you do not perform the test, the first element of the list will be empty because the string list will begin with a separator. You perform the test and only add a separator if there is already something into the string strList (i.e. it is not empty).

    Quote Originally Posted by Harmanj1 View Post
    I don't understand the Do While Loop. Is "Do While Len(strFileName)" the same as "Do While Len(strFileName) > 0"?
    Yes both are equivalent because zero is equivalent to False while any non-zero value evaluates to True (not False), it's just an abbreviation, but the correct form should be Do While Len(strFileName) > 0. I'm just lazy sometimes.
    Have a nice day!

  13. #13
    Join Date
    Aug 2011
    Location
    Indianapolis, IN, USA
    Posts
    41

    My face is red.

    Thanks for the quick reply. I don't know what happened. I ran it as you wrote it and got nothing. I checked it out and thought maybe I should change a line of code and I did and it worked. Now, I've changed it back to your code and it works. Not sure why it didn't work the first time. Oh well. Live and learn.

    Thanks for the info on the Do While Len(strList).

  14. #14
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    You're welcome!
    Have a nice day!

  15. #15
    Join Date
    Oct 2012
    Posts
    2
    Hello everyone can anybody send me a link for this complete file as a vb.
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •