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.
Folder1 has files:
I want the combo box to show
Function GetFileList(Optional ByVal FolderName As String, _
Optional ByVal FileMask As String = "*.*", _
Optional ListSeparator As String = ",") _
Dim strList As String
Dim strfilename As String
If Len(FolderName) > 0 Then
If Right(FolderName, 1) <> "\" Then FolderName = FolderName & "\"
strfilename = Dir(FolderName & FileMask)
Do While Len(strfilename)
If Len(strList) > 0 Then strList = strList & ListSeparator
strList = strList & strfilename
strfilename = Dir
GetFileList = strList
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)
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.
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.
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 18:12.
hope this help
See clear as mud
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.
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.
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
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"?
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).
Originally Posted by Harmanj1
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.
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.