Results 1 to 5 of 5
  1. #1
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250

    Windows folder as a record source for a subform

    Hi,

    I want to work with Windows Files in MS Access. For example I have a Purchase Order that has associated attachments with it. Those are stored on my company server. I want to display and be able to open/add/remove attachments directly in MS Access.

    I'm able to do this using list boxes like so:

    Code:
    Private Sub RefreshListBox()
    
        Dim FileName As String
        Dim Path As String
        
        Me.lboAttachments.RowSource = ""
        
        Path = "\attachments\purchase_orders\" & Me.PurchaseOrderID & "\"
        
        FileName = Dir(AppSharedFolder & Path & "*", vbNormal)
        
        Do While Len(FileName) > 0
            Me.lboAttachments.AddItem FileName
            FileName = Dir()
        Loop
    
    End Sub
    
    Private Sub Form_Current()
    
        RefreshListBox
    
    End Sub
    (Note: AppSharedFolder is a constant with a folder where all files for the application are stored)

    I also have lots of other code for uploading, opening and deleting attachments.



    I am wondering, however, if this can be accomplished using a subform instead of a list box, purely for esthetical purposes.

    Thanks in advance.
    Tomas

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    Yes, but you would need to put the filenames into a table?

    I seem to recall @Davegri? uploaded a DB to do just that.?

    Have a search here.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You could simply add the the filenames to a temporary table, from the Dir() function, and display that in your form.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    Thomasso is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Nov 2016
    Location
    Czech Republic
    Posts
    250
    Yep, that did the trick, thanks!

  5. #5
    moke123's Avatar
    moke123 is online now Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Personally I prefer FSO when working with windows folders and files.

    To list files to a listbox I use:

    Code:
    Public Sub ListFilesListBox(SourceF As String, Lbx As ListBox, Optional IncSubFolders As Boolean = True)
    
        'Dim fso As New FileSystemObject                            'early binding
        'Dim fol As Folder, sfol As Folder, fil As File    
    
        Dim fso As Object                                          'late binding
        Set fso = CreateObject("Scripting.FileSystemObject")
        Dim fol As Object, sfol As Object, fil As Object 
    
        Set fol = fso.GetFolder(SourceF)
    
        For Each fil In fol.Files
            Lbx.AddItem fil.Name & ";" & fil.Path & ";" & fil.DateLastModified & ";" & fil.Type
        Next
    
        If IncSubFolders Then
            For Each sfol In fol.SubFolders
                ListFilesListBox sfol.Path, Lbx, IncSubFolders
            Next
        End If
    
    End Sub
    When writing files to a table I use the below code. I use a dictionary to insure that no duplicate entries are added to the table.

    Code:
    'Dim dict As Scripting.Dictionary   'early binding
    Dim dict As Object                  'Late binding
    
    
    Sub ListMyFilesToTable(SourceF As String, Optional IncSubFolders As Boolean = True)
    
    
    PopulateDictionary
    
    
    sListFilesTableNoDups SourceF, IncSubFolders
    
    
    End Sub
    
    
    Sub PopulateDictionary()
    
    
         'Set dict = New Scripting.Dictionary                 'early binding
         Set dict = CreateObject("Scripting.Dictionary")      'Late binding
    
    
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim strSql As String
        Dim strOut As String
        
        strSql = "Select * from tblFiles"
    
    
        Set db = CurrentDb()
        Set rs = db.OpenRecordset(strSql)
    
    
        If rs.BOF And rs.EOF Then
            GoTo MyExit
        End If
        
        Do Until rs.EOF
        
        dict.Add CStr(rs!fpath), CStr(rs!fpath)
        
        rs.MoveNext
        Loop
    
    
       
    MyExit:
        rs.Close
        Set rs = Nothing
        Set db = Nothing
    
    
    End Sub
    
    
    Sub sListFilesTableNoDups(SourceF As String, Optional IncSubFolders As Boolean = True)
    
    
        Dim strSql As String
        Dim db As DAO.Database
        Set db = CurrentDb
       
        'Dim fso As New FileSystemObject                            'early binding
        'Dim fol As Folder, sfol As Folder, fil As File
        
            Dim fso As Object                                          'late binding
            Set fso = CreateObject("Scripting.FileSystemObject")
            Dim fol As Object, sfol As Object, fil As Object
        
        Set fol = fso.GetFolder(SourceF)
    
    
        For Each fil In fol.Files
            If Not dict.Exists(fil.Path) Then
                strSql = "Insert into tblFiles(FName,FPath,dteCreated,dteModified) Values(""" & fil.Name & """,""" & fil.Path & """,#" & fil.DateCreated & "#,#" & fil.DateLastModified & "#)"
                db.Execute strSql, dbFailOnError
            End If
        Next
    
    
        If IncSubFolders Then
            For Each sfol In fol.SubFolders
                sListFilesTableNoDups sfol.Path, IncSubFolders
            Next
        End If
    
    
        Set db = Nothing
    
    
    End Sub
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 09-08-2020, 01:15 AM
  2. Replies: 9
    Last Post: 04-16-2018, 06:41 AM
  3. Replies: 5
    Last Post: 10-14-2014, 04:08 PM
  4. Replies: 14
    Last Post: 05-25-2012, 02:40 AM
  5. Replies: 6
    Last Post: 05-05-2012, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums