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

Article translations Article translations
Article ID: 279508 - View products that this article applies to.
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).

Expand all | Collapse all

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.
  1. Open the sample database Northwind.mdb.
  2. Create a new blank form in Design view.
  3. Add the following controls to the form:
    
       Command button
       --------------------------
       Name: cmdFileDialog
       Caption: Add Files
       OnClick: [Event Procedure]
    
       List box
       -------------------------
       Name: FileList
       RowSourceType: Value List
    					
  4. On the View menu, click Code to open the form's module in the Visual Basic Editor.
  5. On the Tools menu, click References.
  6. Click to select the Microsoft Office 10.0 Object Library check box.
  7. Click OK to close the References dialog box.
  8. 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 = ""
    
       '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 = "Please select one or more files"
    
          'Clear out the current filters, and add our 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 selected and add it to our 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
    					
  9. Save the form as Form1, and then close it.
  10. Open the Form1 form in Form view.
  11. Click Add Files. Note that the Please select one or more files dialog box appears.
  12. Select one or more files, and then click OK, or click Cancel.
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.

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 Office Assistant or the Answer Wizard, and then click Search to view the topic.

Properties

Article ID: 279508 - Last Review: October 11, 2006 - Revision: 4.2
APPLIES TO
  • Microsoft Access 2002 Standard Edition
Keywords: 
kbprogramming kbhowto KB279508

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com