Display list of files in a folder on a hard drive in a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have searched and cannot find anything on this. How could I create a form
that searches the contents of a specific folder on a drive on our network and
then have it display all of the files located in the folder on a form. Then
allow the user to select one file at a time to print or view the
document/file in the program that it was created in?
 
Hi,


Basically you can use the VBA function DIR (see help file for discussion
and for an example). You can then recuperate the names in a list box (or
combo box) list of values.

You can use the file name as if it was an hyperlink to open it.


Application.FollowHyperlink "C:\testing.xls"



Hoping it may help,
Vanderghast, Access MVP
 
I am too new to code for that to help. I checked the help and couldn't
figure out how to make it work. What would the code look like?
 
Hi,


The following should do, assuming the combo box is pre-set to get its list
from a list of values, and with only one column of data:

=================
Public Sub demoDirTxt()
Dim concat As String
Dim str As String

' find all the files in C:\ with the txt extension
str = Dir("C:\*.txt")

Do While str <> vbNullString

' as long as there are file name found, append them to
' what we have already found
concat = concat & IIf(0 = Len(concat), "", ";") & str

' get the next file, if any
str = Dir()

Loop

' assign the list of values we have just built as the combo box list
Forms!formNameHere!ComboBoxNameHere.RowSource = concat

End Sub
================



Hoping it may help,
Vanderghast, Access MVP
 
That worked just perfect to display the records...

Now, how do I allow two options (Print or open) based on which file is
selected in the list box? Do I need command buttons and what code would I
use?
 
Hi,


You use a button under which you have a (one) line of code like, for its
Click event handling:



Application.FollowHyperlink Me.ComboBoxControlNameHere


That assumes the combobox control value is a full file name with its
extension.




Hoping it may help,
Vanderghast, Access MVP
 
Michel - I really appreciate your help on this thread!

But I am getting an error on that one line of code you gave it.

first it comes up with a red X error with the critical error sound saying
that if I open this file I could damage my computer. Am I sure - yes or no.
When I say yes, I get error 490 - cannot open specified file.

What am I doing wrong?
 
Hi,


Try with a txt file first. You probably tried with an executable file (exe,
com, bat, ... ) and you got the standard security message under such
circumstances. The second error may be due to the plain fact that "some"
required file cannot be open (NTFS privileges, or otherwise, such as having
a wrong of incomplete path to access it). That is why I strongly suggest to
test with something you are in absolute control, such as with a txt file,
first, to see that the VBA code work.. or not.



Hoping it may help,
Vanderghast, Access MVP
 
The only files in this directory that are being displayed on my form are .doc
or .xls - I have tried to open all 5 files using your code and I get the
critical error each time and then it crashes.

When I click on 'debug' after I get the error on trying to open the .doc
file, it goes to this code that you gave me. Did I copy something wrong (the
control is named 'list')?

Private Sub Comando4_Click()

Application.FollowHyperlink Me.List

End Sub
 
Hi,



List is the name of your control? It is a COMBO BOX, isn't it? a LIST BOX
may not have a value under some circumstances. To be sure, type

? Me.List.Value

in the debug window to see if you get something, or an error.


Hoping it may help,
Vanderghast, Access MVP
 
Back
Top