Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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).

↑ Back to the top


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.

↑ Back to the top


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:
a. If you have not already done so, create a list box on a form.
b. Right-click the list box, and then click Properties.
c. In the list box properties window, click the Data tab, and then set the RowSourceType property to Value List.
d. 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:
a. If you have not already done so, create a command button.
b. Right-click the button, and then click Properties.
c. In the command button properties window, click the Build button next to the On Click box.
d. In the Choose Builder dialog box, click Code Builder, and then click OK.
e. 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
						
f. Open the form in Form view.
g. Click one of the files in the box, and then click the new command button to print the file.

↑ Back to the top


Keywords: KB308634, kbprogramming, kbhowto

↑ Back to the top

Article Info
Article ID : 308634
Revision : 2
Created on : 6/30/2004
Published on : 6/30/2004
Exists online : False
Views : 257