Microsoft KB Archive/308634

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

Article ID: 308634

Article Last Modified on 6/30/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



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:  Create and then set up a list box:  If you have not already done so, create a list box on a form. Right-click the list box, and then click Properties. In the list box properties window, click the Data tab, and then set the RowSourceType property to Value List. Click the Other tab, and then set the MultiSelect property to Extended. </li> Select the form, and then click Properties on the View menu.</li> Click the Event tab, and then click the Build button next to the On Open box.</li> In the Choose Builder dialog box, click Code Builder, and then click OK.</li>  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 = &quot;C:\Documents and Settings\a-fell\Desktop\&quot;

' Set file name to search for. This example assumes that you ' want to search for .txt files. fs.FileName = &quot;*.txt&quot;

' 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 &quot;No files were found&quot; 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 & &quot;;&quot; & 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 </li> Create and then code a command button to enable printing:  If you have not already done so, create a command button.</li> Right-click the button, and then click Properties.</li> In the command button properties window, click the Build button next to the On Click box.</li> In the Choose Builder dialog box, click Code Builder, and then click OK.</li>  Under the _Click event, type the following code, where the   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(&quot;Word.Application&quot;) 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 </li> Open the form in Form view.</li> Click one of the files in the box, and then click the new command button to print the file.</li></ol> </li></ol>

Additional query words: prb

Keywords: kbhowto kbprogramming KB308634

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.