UtterAccess.com
We have detected that you are using an unsupported web browser. We recommend you to upgrade your web browser to get the most from UtterAccess forums.
Why should and how can I upgrade?
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Populate A Listbox With Files From A Directory, Any Version    
 
   
dallr
post Jul 5 2006, 09:51 AM
Post #1

UtterAccess VIP
Posts: 3,075
From: The Land of the Access Ninja's



Hi Guys just trying to add to the archives again.
The following code pulls all the files from a directory and displays it in a listbox then allows the user to double click on the file in the list box to open it.
The user can also filter the function to only show the files that they want in the listbox. You filter by including the file extension (eg. xls, doc, txt)in the PopListBox Function. Otherwise leave it out to show all the files.
To populate your listbox you can use the forms ON OPEN event and include the following.
CODE

With Me.MyListBox    'the name of your listbox  
    .RowSource = PopListBox("c:\",'txt")   ' You can leave the ext. out to show all files  
    .RowSourceType = "value list"
End With

To allow you to double click and open the file from the listbox. In the listbox DOUBLE CLICK event place the following code.
CODE

'Form1=Name of your form the listbox is on
'MyListBox = Name of your list box.
'C:\ =  The name of your directory. Note this must be the same as in the PopListBox Function.
    
On Error Resume Next
Application.FollowHyperlink OpenListItem("Form1", "MyListBox", "c:\")

These are the function you can include in one module.
CODE
  
Function PopListBox(strPath As String, Optional FileExtension As String) As String
Dim MyFso As Object
Dim MainDir As Object
Dim MyFile As Object
Dim LstItems As String, FileProp As String
Dim NameLength As Integer, InitialNameLength As Integer, TypeLength As Integer, InitialTypeLength As Integer
Dim x As Integer, y As Integer
Dim NameSep As Integer, TypeSep As Integer
Dim Placer As Variant
On Error Resume Next
Set MyFso = CreateObject("scripting.FileSystemObject")
Set MainDir = MyFso.GetFolder(strPath)
InitialNameLength = 1
InitialTypeLength = 1
For Each MyFile In MainDir.Files ' all this in side this loop is to help try and format the columns a bit more uniformly.
        NameLength = Len(MyFile.Name)
        x = IIf(NameLength > InitialNameLength, NameLength, InitialNameLength)
        InitialNameLength = x
        
        TypeLength = Len(MyFile.Type)
        y = IIf(TypeLength > InitialTypeLength, TypeLength, InitialTypeLength)
        InitialTypeLength = y
Next
For Each MyFile In MainDir.Files
    Placer = InStr(1, MyFile.Name, ".") - 1
    NameSep = x - Len(MyFile.Name) + 4
    TypeSep = y - Len(MyFile.Type) + 4
    
    If Mid(MyFile.Name, InStr(1, MyFile.Name, ".") + 1) = FileExtension Then
        FileProp = Mid(MyFile.Name, 1, Placer) & String(NameSep, "_") & MyFile.Type & String(TypeSep, "_") & MyFile.DateCreated
        LstItems = LstItems & FileProp & ";"
    ElseIf FileExtension = "" Then
        FileProp = Mid(MyFile.Name, 1, Placer) & String(NameSep, "_") & MyFile.Type & String(TypeSep, "_") & MyFile.DateCreated
        LstItems = LstItems & FileProp & ";"
    End If
    
Next
Debug.Print LstItems
'Clean up
Set MyFso = Nothing
Set MainDir = Nothing
PopListBox = UCase(LstItems)
End Function

CODE

Function OpenListItem(FormName As String, ControlName As String, strPath As String) As String
Dim frm As Form
Dim ctl As Control
Dim intI As Integer
Dim MyVar As Variant
Dim strFileName As String
Dim MyFso As Object
Dim MainDir As Object
Dim MyFile As Object
On Error Resume Next
Set MyFso = CreateObject("scripting.FileSystemObject")
Set MainDir = MyFso.GetFolder(strPath)
Set frm = Forms(FormName)
Set ctl = frm.Controls(ControlName)
For Each MyVar In ctl.ItemsSelected
    strFileName = ctl.ItemData(MyVar)
    strFileName = Mid(strFileName, 1, InStr(1, strFileName, "_") - 1)
    
    For Each MyFile In MainDir.Files
        If Mid(MyFile.Name, 1, InStr(1, MyFile.Name, ".") - 1) = strFileName Then
            OpenListItem = MyFile.Path
            Exit For
        End If
    Next
Next
End Function

Hope you find it helpful. I have also upload a DB for those who want to download.
Dallr
Attached File(s)
Attached File  Populate a Listbox with Files from a Directory.zip ( 22.42K ) Number of downloads: 861
 
Go to the top of the page
 
+
clynch3385
post Sep 12 2012, 02:28 PM
Post #2

New Member
Posts: 1



I have down loaded the demo ListBox and it does work great in access 2003. But, it will only open word.doc from 2003. Will not open and reports in word 2010 or any PDF files. I need to be able to open word dos from word 2010 if possible and any PDF files. If not possible all can be convert to a PDF file and open them. What must be added to this code in order to open these fills. (NOTE) My applaction was created in access 2003, but is used in access 2010 at work.
Can any one help?
Go to the top of the page
 
+
CyberCow
post Sep 19 2012, 04:53 PM
Post #3

UdderAccess Admin + UA Ruler
Posts: 19,059
From: Upper MI



welcome2UA.gif
You will have to modify the code (in the PopListBox) to include ".docx" files - did you do that part? Or did you leave out the extention to show all files?
Also, if you do not have the OfficeFileConverter, you will not be able to open .docx files on an Office 2003 system.
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Search  ·  Go to Top  ·  Lo-Fi Version Time is now: 25th October 2014 - 05:08 AM

Tag cloud: