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.

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


View products that this article applies to.

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.

↑ Back to the top


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.
  1. Start Microsoft Access.
  2. Open the sample database Northwind.mdb.
  3. In the Database window, click Forms under Objects.
  4. In the right pane, double-click Create form in Design view.
  5. Add the following controls to the form:
    	Command button
    	--------------------------
    	Name: cmdFileDialog
    	Caption: Add Files
    	OnClick: [Event Procedure]
    
    	List box
    	-------------------------
    	Name: FileList
    	RowSourceType: Value List
    
  6. On the View menu, click Code to open the module of the form in the Microsoft Visual Basic Editor.
  7. On the Tools menu, click References.
  8. In the References-DatabaseName dialog box, click to select the Microsoft Office 11.0 Object Library check box, and then click OK.
  9. 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 = ""
    
       ' 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 = "Select One or More Files"
    
          ' Clear out the current filters, and then add your own.
          .Filters.Clear
          .Filters.Add "Access Databases", "*.MDB"
          .Filters.Add "Access Projects", "*.ADP"
          .Filters.Add "All Files", "*.*"
    
          ' 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 "You clicked Cancel in the file dialog box."
          End If
       End With
    End Sub
    			
  10. Save the form as Form1, and then close it.
  11. In the Database window, select Form1, and then click Open to open the form in the Form view.
  12. Click Add Files. The Select One or More Files dialog box appears.
  13. Select one or more files, and then click OK, or click Cancel.
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.

↑ Back to the top


References

For more information about what you can do with the file dialog box, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type filedialog object in the Search for box in the Assistance pane, and then click Start searching to view the topic.

↑ Back to the top


Keywords: KB824272, kbhowto, kbusage, kbcode, kbfile, kbprogramming

↑ Back to the top

Article Info
Article ID : 824272
Revision : 3
Created on : 9/17/2011
Published on : 9/17/2011
Exists online : False
Views : 356