ACC2000: How to Fill a List Box with File Names and Enable Printing of Files from a Form (308634)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q308634
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

SUMMARY

This article describes how to fill a list box on a form with the names of files that are in a folder, and how to enable printing of one file or several of the files from the form.

MORE INFORMATION

To fill a list box with file names and then enable printing of one file or several of the files, follow these steps:
  1. Create and then set up a list box:
    1. If you have not already done so, create a list box on a form.
    2. Right-click the list box, and then click Properties.
    3. In the list box properties window, click the Data tab, and then set the RowSourceType property to Value List.
    4. Click the Other tab, and then set the MultiSelect property to Extended.
  2. Select the form, and then click Properties on the View menu.
  3. Click the Event tab, and then click the Build button next to the On Open box.
  4. In the Choose Builder dialog box, click Code Builder, and then click OK.
  5. Under the FormOpen event, type the following code:
    Private Sub Form_Open(Cancel As Integer)
    
          ' Dimension variables.
          Dim myarray()
          Dim fs As Object
          Dim i As Integer
    
          ' Declare filesearch object.
          Set fs = Application.FileSearch
    
          ' Set folder to search. This example assumes that the files reside
          ' in the C:\Documents and Settings\a-fell\Desktop folder.
          fs.LookIn = "C:\Documents and Settings\a-fell\Desktop\"
    
          ' Set file name to search for. This example assumes that you
          ' want to search for .txt files.
          fs.FileName = "*.txt"
    
          ' Execute the file search, and check to see if the file(s) are
          ' present.
          If fs.Execute > 0 Then
    
             ' Redimension the array to the number of files found.
             ReDim myarray(fs.foundfiles.Count)
    
             ' Loop through all found file names and fill the array.
             For i = 1 To fs.foundfiles.Count
                myarray(i) = fs.foundfiles(i)
             Next i
          Else
             ' Display message if no files were found.
             MsgBox "No files were found"
          End If
    
          ' Loop through the array and fill the list box on the UserForm.
          If fs.foundfiles.Count > 1 Then
             Me.List0.RowSource = myarray(1)
             For i = 2 To fs.foundfiles.Count
                Me.List0.RowSource = Me.List0.RowSource & ";" & myarray(i)
             Next i
          Else
             For i = 1 To fs.foundfiles.Count
                Me.List0.RowSource = myarray(i)
             Next i
          End If
    
          ' Display the UserForm.
    
    End Sub
    					
  6. Create and then code a command button to enable printing:
    1. If you have not already done so, create a command button.
    2. Right-click the button, and then click Properties.
    3. In the command button properties window, click the Build button next to the On Click box.
    4. In the Choose Builder dialog box, click Code Builder, and then click OK.
    5. Under the CommandButtonName_Click event, type the following code, where the CommandButtonName is Command2:
      Private Sub Command2_Click()
      
            On Error GoTo Err_Command2_Click
            Dim strvalue As String
      
            For i = 0 To List0.ListIndex
               If List0.Selected(i) = True Then
                  strvalue = List0.ItemData(i)
                  Dim WordObj As Object
                  Set WordObj = CreateObject("Word.Application")
                  WordObj.Documents.Open strvalue
                  WordObj.PrintOut Background:=False
                  WordObj.Quit
                  Set WordObj = Nothing
               End If
            Next i
       
            Exit_Command2_Click:
               Exit Sub
      
            Err_Command2_Click:
               MsgBox Err.Description
               Resume Exit_Command2_Click   
      
      End Sub
      						
    6. Open the form in Form view.
    7. Click one of the files in the box, and then click the new command button to print the file.

Modification Type:MajorLast Reviewed:6/30/2004
Keywords:kbhowto kbProgramming KB308634