Free Lessons
Fast Tips
Topic Index
Home   Courses   Seminars   Templates   TechHelp   Fast Tips   Forums   Help   Contact   Join   Order   Logon  
Tips & Tricks

New Tips Added Weekly!
Click here to get on our Mailing List

MS Access

File Dialog

How to use the File Dialog.

There was a question on the forum asking how to use the FileDialog instead of the Common Dialog control.

This is an alternative to the Common Dialog control used in the Access Imaging Seminar.

Create a Form, add a Listbox control and rename it "FileList".

Add a button to a Form.

Set the OnClick() Event of the Button.

In the VBA Editor go to Tools | References.

Find the Microsoft Office 11.0 Object Library Reference and add it.

This maybe a higher number depending which version of Office you have installed.

	Private Sub cmdFileDialog_Click() 
	   ' Requires reference to Microsoft Office 11.0 Object Library. 
	   Dim fDialog As Office.FileDialog 
	   'Dim fDialog As Object
	   Dim varFile As Variant 
	   ' Clear listbox contents. 
	   Me.FileList.RowSource = "" 
	   ' Set up the File Dialog. 
	   Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 
	   'Set fDialog = Application.FileDialog(3)
	   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.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 
			 MsgBox "You clicked Cancel in the file dialog box." 
		  End If 
	   End With 
	End Sub

You might get the following error using the Office.FileDialog so just swap it to the below.

Compile error: User-defined type not defined

	'Dim fDialog As Office.FileDialog 
	Dim fDialog As Object
	'Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 
	Set fDialog = Application.FileDialog(3)

There are 4 options you can choose from as a Dialog but the msoFileDialogOpen and msoFileDialogSaveAs constants are not supported in Microsoft Access.

ObjectType (MsoFileDialogType)
msoFileDialogFilePicker3File picker dialog box.
msoFileDialogFolderPicker4Folder picker dialog box.
msoFileDialogOpen1Open dialog box.
msoFileDialogSaveAs2Save As dialog box.

If you use the Folder Picker you can't use the Filters.

There are other options you can set.

	With fDialog
		.InitialView = msoFileDialogViewSmallIcons
	End With
ObjectType (MsoFileDialogType)
msoFileDialogViewDetails2Files displayed in a list with detail information.
msoFileDialogViewLargeIcons6Files displayed as large icons.
msoFileDialogViewList1Files displayed in a list without details.
msoFileDialogViewPreview4Files displayed in a list with a preview pane showing the selected file.
msoFileDialogViewProperties3Files displayed in a list with a pane showing the selected file's properties.
msoFileDialogViewSmallIcons7Files displayed as small icons.
msoFileDialogViewThumbnail5Files displayed as thumbnails.
msoFileDialogViewTiles9Files displayed as tiled icons.
msoFileDialogViewWebView8Files displayed in Web view.

You can also set which Folder will open:

	With fDialog
		.InitialFileName = "c:\599CD\"
	End With

You can change which Filter is selected if there is more than 1.

	With fDialog
		.FilterIndex = 1
	End With

You can also change the text on the OK Button.

	With fDialog
		.ButtonName = "Choose &Access file"
	End With

This won't change until you select a File.

The & allows the Alt key to be used.


Alex Hedley (Avatar) By: Alex Hedley

Click here to sign up for more FREE tips


You may want to read these articles from the 599CD News:


Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
My Account
My Courses
Lost Password
Student Databases
Change Email
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Customer Support
Web Site Tour
Consulting Services
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Video Tutorials
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Terms of Sale
Contact Info
Support Policy
Email Richard
Mailing Address
Phone Number
Fax Number
Course Survey
Blog RSS Feed    Twitter

YouTube Channel    LinkedIn