Microsoft KB Archive/824272

= How to display and to use the File dialog box in Microsoft Access =

Article ID: 824272

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Office Access 2003

-



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

Advanced: Requires expert coding, interoperability, and multiuser skills. 

For a Microsoft Access 2002 version of this article, see 279508.



SUMMARY
This article discusses how to use the new FileDialog method in Microsoft Access to display the built-in File dialog box and to determine the files that the user selects.

Note The FileDialog method works only in the full retail version of Microsoft Access. This method does not work in a Microsoft Access run-time application.



MORE INFORMATION
In earlier versions of Microsoft Access, you may display the file dialog box by using either the Microsoft Common Dialog ActiveX control or by making calls to the Windows API.

By using the FileDialog method in Microsoft Office Access 2003, you can display the File dialog box that is used by Microsoft Access and to determine the files that the user selects. The SelectedItems collection of the FileDialog object contains the paths to the files that are selected by the user. By using a For-Each loop, you can enumerate this collection and then display each file. The following example loops through the ItemsSelected collection and then displays files in the list box.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.  Start Microsoft Access. Open the sample database Northwind.mdb. In the Database window, click Forms under Objects. In the right pane, double-click Create form in Design view.  Add the following controls to the form:

   Command button --   Name: cmdFileDialog Caption: Add Files OnClick: [Event Procedure]

List box -   Name: FileList RowSourceType: Value List  On the View menu, click Code to open the module of the form in the Microsoft Visual Basic Editor.</li> On the Tools menu, click References.</li> In the References-  dialog box, click to select the Microsoft Office 11.0 Object Library check box, and then click OK.</li>  Add the following code to the module of the form: Option Compare Database Option Explicit Private Sub cmdFileDialog_Click

' This requires a reference to the Microsoft Office 11.0 Object Library.

Dim fDialog As Office.FileDialog Dim varFile As Variant

' Clear the list box contents. Me.FileList.RowSource = &quot;&quot;

' Set up the File dialog box. Set fDialog = Application.FileDialog(msoFileDialogFilePicker) With fDialog ' Allow the user to make multiple selections in the dialog box. .AllowMultiSelect = True ' Set the title of the dialog box. .Title = &quot;Select One or More Files&quot;

' Clear out the current filters, and then add your own. .Filters.Clear .Filters.Add &quot;Access Databases&quot;, &quot;*.MDB&quot; .Filters.Add &quot;Access Projects&quot;, &quot;*.ADP&quot; .Filters.Add &quot;All Files&quot;, &quot;*.*&quot;

' Show the dialog box. If the .Show method returns True, the ' user picked at least one file. If the .Show method returns ' False, the user clicked Cancel. If .Show = True Then ' Loop through each file that is selected and then add it to the list box. For Each varFile In .SelectedItems Me.FileList.AddItem varFile Next Else MsgBox &quot;You clicked Cancel in the file dialog box.&quot; End If  End With End Sub </li> Save the form as Form1, and then close it.</li> In the Database window, select Form1, and then click Open to open the form in the Form view.</li> Click Add Files. The Select One or More Files dialog box appears.</li> Select one or more files, and then click OK, or click Cancel.</li></ol>

If you select one or more files, you may notice that the file names appear in the list box. If you click Cancel, you may receive a message that indicates that you clicked Cancel.

<div class="references_section">