Access 2007-2016
Access 2000-2003
Access Seminars
TechHelp Support
Tips & Tricks
Access Forum
Course Index
Topic Glossary
Insider Circle
 
Home   News   Tips   Glossary   Templates   Forums   Help   Logon   Order   Contact Us  
 
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.

Code:
	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.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
	

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)
NameValueDescription
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)
NameValueDescription
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:

 

Learn
 
Accessindex
Excelindex
Wordindex
Windowsindex
PowerPointindex
Photoshopindex
Visual Basicindex
ASPindex
Seminars
More...
Customers
 
Account Login
Online Theater
Downloads
Lost Password
Free Upgrades
Insider Circle
Student Databases
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Articles
Search Our Site
Waiting List
Production Schedule
Collapse Menus
Help
 
Live Chat
Customer Support
WalkThru Tutorials
Troubleshooting
FAQs
TechHelp
Consulting Services
About Us
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
Order
 
Video Tutorials
Handbooks
MYOLP Memberships
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Gift CDs
Contact
 
Live Chat
General Info
Support Policy
Contact Form
Email Us
Mailing Address
Phone Number
Fax Number
Course Survey
Facebook    Twitter

Google Plus    LinkedIn

Blog RSS Feed    YouTube Channel
Richard Rost Microsoft MVP