Microsoft KB Archive/279508

= How to display and use the file dialog box in Access 2002 =

Article ID: 279508

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Access 2002 Standard Edition

-



This article was previously published under Q279508





For a Microsoft Office Access 2003 version of this article, see 824272.

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

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



SUMMARY
This article shows you how to use the new FileDialog method in Microsoft Access to display the built-in file dialog box and to determine what files the user selected.

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 could not display the file dialog box without using either the Microsoft Common Dialog ActiveX Control or by making calls to the Windows API.

In Microsoft Access 2002, the FileDialog method allows you to display the file dialog box used by Microsoft Access and to determine what files were selected by the user. The SelectedItems collection of the FileDialog object contains the paths to the files selected by the user. By using a For...Each loop, you can enumerate this collection and display each file. The following example loops through the ItemsSelected collection and displays the files in a 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.  Open the sample database Northwind.mdb. Create a new blank 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 form's module in the Visual Basic Editor. On the Tools menu, click References.</li> Click to select the Microsoft Office 10.0 Object Library check box.</li> Click OK to close the References dialog box.</li>  Add the following code to the form's module: Option Compare Database Option Explicit Private Sub cmdFileDialog_Click

'Requires reference to Microsoft Office 10.0 Object Library.

Dim fDialog As Office.FileDialog Dim varFile As Variant

'Clear listbox contents. Me.FileList.RowSource = &quot;&quot;

'Set up the File Dialog. Set fDialog = Application.FileDialog(msoFileDialogFilePicker) With fDialog 'Allow user to make multiple selections in dialog box .AllowMultiSelect = True 'Set the title of the dialog box. .Title = &quot;Please select one or more files&quot;

'Clear out the current filters, and add our 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 selected and add it to our 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> Open the Form1 form in Form view.</li> Click Add Files. Note that the Please 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 selected one or more files, note that those file names appear in the list box. If you clicked Cancel, note that you receive a message indicating that you clicked Cancel.

<div class="references_section">