Populate lstBox dynamically on a report -

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • katia
    New Member
    • Feb 2011
    • 13

    Populate lstBox dynamically on a report -

    two questions: 1 - i am using this on my form to populate the lst box showing sub folders, works fine but i need to show only the information after txt in the lstBox and 2- i would like to use this code in creating the report in ms access but report won't accept add function because it is in print mode - is it possible to work around this? many thanks

    Code:
    Dim Fso As Object       'to hold FSO
        Dim fmain As Folder     'to hold reference to top level subfolders
        Dim flevel1 As Folder   'to hold reference to 1st level subfolders
        Dim flevel2 As Folder   'to hold reference to 2nd level subfolders
        Dim aFile As File       'to hold reference to Files
        Dim lst As ListBox
    Dim txt As TextBox
    Dim strFileName As String
    Dim strPath As String
    
    
    Set lst = Me![lstFilesInSubDirectory]
    Set txt = Me![txtFilePath]
    
    
    
        
        Set Fso = CreateObject("Scripting.FileSystemObject")
        'Assign the folder to loop
        Set fmain = Fso.GetFolder(txt)   'Pass your folder path here
        
        'this code loops through the folders,subfolders and files
        'this works only upto 2 levels of subfolders
          For Each flevel1 In fmain.SubFolders
              For Each aFile In flevel1.files
                lst.AddItem aFile.Path
                
                
                
                
              Next
             'loops second level subfolders
              If flevel1.SubFolders.Count > 0 Then
                  For Each flevel2 In flevel1.SubFolders
                    For Each aFile In flevel2.files
                      lst.AddItem aFile.Path
                    Next
                  Next
              End If
          Next
        
        Set fmain = Nothing
        Set flevel1 = Nothing
        Set flevel2 = Nothing
        Set aFile = Nothing
        Set Fso = Nothing
        
    
    End Sub
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    I am not sure if I understand your request completely, but if you are asking to use the existing Code but only for the Folder specified in the Text Box txt, and not any Sub-Folders:
    Code:
    Dim Fso As Object       'to hold FSO
    Dim fmain As Folder     'to hold reference to top level subfolders
    Dim flevel1 As Folder   'to hold reference to 1st level subfolders
    Dim aFile As File       'to hold reference to Files
    Dim lst As ListBox
    Dim txt As TextBox
      
    Set lst = Me![lstFilesInSubDirectory]
    Set txt = Me![txtFilePath]
      
    Set Fso = CreateObject("Scripting.FileSystemObject")
      Set fmain = Fso.GetFolder(txt)
        For Each aFile In fmain.Files
          lst.AddItem aFile.Path
        Next
        
    Set fmain = Nothing
    Set flevel1 = Nothing
    Set aFile = Nothing
    Set Fso = Nothing

    Comment

    • katia
      New Member
      • Feb 2011
      • 13

      #3
      Hi ADezii;
      Q1- the code works on my form with the exception that sometimes i get an error because the results from all the sub folder search is too big for the list box (it returns the complete file path for subfolders) - so I would like to display only the text from the return search that comes after the filepath (txt), into the listbox -

      Q2 - the code works on the form just fine, but does not work on a report - does not get the information dynamically to display as part of the report. not sure i can even do this? it seems reports go into print mode right away or something - so won't let the code run because print is running.... i think this is the problem anyway.

      Comment

      Working...