Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Help shows invalid usage? 3

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
0
0
GB
Hi,

I'm trying to set the filters on the filedialog as per the help example..

' 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", "*.*"

But when I use this syntax...
Code:
    Set fd = Application.FileDialog(msoFileDialogSaveAs)

    If Not IsMissing(cFilters) Then
        With fd
            .Filters.Clear
            For Each vFilter In cFilters.keys
                .Filters.Add cFilters.Item(vFilter), vFilter
            Next
        End With
    End If

.Filters.Clear errors with "Object doesn't support this property or method"

Any ideas why I'm getting this error that contradicts the MS help for this FileDialog object?

Thanks,
1DMF




"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
The help file is pretty explicit:

Microsoft Access help file said:
A run-time error will occur if the Filters property is used in conjunction with the Clear, Add, or Delete methods when applied to a Save As FileDialog object
 
The help file is pretty explicit:
I have no idea where you are getting that, my F1 help says nothing of the sort?

Access 2010 Developer Reference > Access Object Model Reference > Application Object > Properties
Access Developer Reference
Application.FileDialog Property
Returns a FileDialog object which represents a single instance of a file dialog box. Read-only.
Syntax

expression.FileDialog(dialogType)

expression A variable that represents an Application object.

Parameters

Name Required/Optional Data Type Description
dialogType Required MsoFileDialogType An MsoFileDialogType constant that represents the type of dialog box.

Remarks


The msoFileDialogOpen and msoFileDialogSaveAs constants are not supported in Microsoft Access.


Example


This example illustrates how to use the FileFialog object to display a dialog box that allow the user to select one or more files. The selected files are then added to a listbox named FileList.

Visual Basic for Applications
Private Sub cmdFileDialog_Click()

' Requires reference to Microsoft Office 11.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


© 2010 Microsoft Corporation. All rights reserved.



See Also
Application Object
Application Object Members

It does state : The msoFileDialogOpen and msoFileDialogSaveAs constants are not supported in Microsoft Access.

However, as they are simply enums I declared my own constant and it works fine?

Private Const msoFileDialogSaveAs As Integer = 2

According to the office enums :
2 = Save As Dialog

I'm confused!


@Duane I don't want to select an existing file per se, so that GUI is no good to me as it errors if you type your own filename stating it doesn't exist :-(



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
It is because of the type of filedialog you picked. If it was a "picker" it would work. Since it is a "saveas" it is not an option
 
Sorry I did not see Duane's post, which is correct. You get the same type of errors when working with different type of recordsets, because you can only do certain things depending on the type of recordset but the properties are all still visible.
 
Why would you look up the FileDialog object rather than the FileDialogFilters for help on a FileDialogFilters method?

[small]Access 2010 Developer Reference > Object Library Reference for Microsoft Office 2010 > Microsoft Office 2010 Object Model Reference > FileDialogFilters Object
Office Developer Reference
FileDialogFilters Object
A collection of FileDialogFilter objects that represent the types of files that can be selected in a file dialog box that is displayed using the FileDialog object.
Example


Use the Filters property of the FileDialog object to return a FileDialogFilters collection. The following code returns the FileDialogFilters collection for the File Open dialog box.

Visual Basic for Applications
Application.FileDialog(msoFileDialogOpen).Filters

Use the Add method to add FileDialogFilter objects to the FileDialogFilters collection. The following example uses the Clear method to clear the collection and then adds filters to the collection. The Clear method completely empties the collection; however, if you do not add any filters to the collection after you clear it, the "All files (*.*)" filter is added automatically.

Visual Basic for Applications
Sub Main()

'Declare a variable as a FileDialog object.
Dim fd As FileDialog

'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)

'Declare a variable to contain the path
'of each selected item. Even though the path is aString,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant

'Use a With...End With block to reference the FileDialog object.
With fd

'Change the contents of the Files of Type list.
'Empty the list by clearing the FileDialogFilters collection.
.Filters.Clear

'Add a filter that includes all files.
.Filters.Add "All files", "*.*"

'Add a filter that includes GIF and JPEG images and make it the first item in the list.
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1

'Use the Show method to display the File Picker dialog box and return the user's action.
'The user pressed the button.
If .Show = -1 Then

'Step through eachString in the FileDialogSelectedItems collection.
For Each vrtSelectedItem In .SelectedItems

'vrtSelectedItem is aString that contains the path of each selected item.
'You can use any file I/O functions that you want to work with this path.
'This example displays the path in a message box.
MsgBox "Path name: " & vrtSelectedItem

Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With

'Set the object variable to Nothing.
Set fd = Nothing

End Sub

When changing the FileDialogFilters collection, remember that each application can only create an instance of a single FileDialog object. This means that the FileDialogFilters collection resets to its default filters whenever you call the FileDialog method with a new dialog box type. The following example iterates through the default filters of the SaveAs dialog box and displays the description of each filter that includes a Microsoft Excel file.

Visual Basic for Applications
Sub Main()

'Declare a variable as a FileDialogFilters collection.
Dim fdfs As FileDialogFilters

'Declare a variable as a FileDialogFilter object.
Dim fdf As FileDialogFilter

'Set the FileDialogFilters collection variable to
'the FileDialogFilters collection of the SaveAs dialog box.
Set fdfs = Application.FileDialog(msoFileDialogSaveAs).Filters

'Iterate through the description and extensions of each
'default filter in the SaveAs dialog box.
For Each fdf In fdfs

'Display the description of filters that include
'Microsoft Excel files
If InStr(1, fdf.Extensions, "xls", vbTextCompare) > 0 Then
MsgBox "Description of filter: " & fdf.Description
End If
Next fdf

End Sub


[blue] Note
A run-time error will occur if the Filters property is used in conjunction with the Clear, Add, or Delete methods when applied to a Save As FileDiaog object. For example,
Application.FileDialog(msoFileDialogSaveAs).Filters.Clear
[/blue]

will result in a run-time error.

© 2010 Microsoft Corporation. All rights reserved.[/small]


 
Why would you look up the FileDialog object rather than the FileDialogFilters for help on a FileDialogFilters method

Because filedialogfilters in not a method it is a property to a collection according to the filediolog help

I wanted to find out info on the filedialog, so I highlighted it and pressed F1 and got the help that implied the filedialogfilters collection could be augmented.

hey ho , no biggie , just wondered why the help implied the collection was available yet I was getting an error.



"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Sorry, yes, typo, FileDialogFilters object

>it is a property to a collection according to the filediolog help

No, no Filters is a property. That property contains a FileDialogFilters object

But I appreciate the fact that Microsoft's Office developer references are nowhere near as good (or accurate) as they used to be (e.g the example on the actual FileDialogFilters.Clear method page doesn't actually include an example of the Clear method - well it couldn't do, since it uses the SaveAs dialog for the example. Shoddy, very shoddy)

Oh, and if you add a reference to Microsoft Office to your project, you'll find msoFileDialogSaveAs magically becomes available ...
 
>> No, no Filters is a property. That property contains a FileDialogFilters object

Oh, the docs say it is a property to a collection, not a FileDialogFilters class object, but yeah, all a bit confusing and not very helpful, which is why I come to TT to ask the experts ;-)

>> Oh, and if you add a reference to Microsoft Office to your project, you'll find msoFileDialogSaveAs magically becomes available ...

But that then messes with multiple office versions and late bindings doesn't it?

I do hate the fact that to get things to not be version bound, you don't reference the library, but then that means you get no intelisense [banghead]





"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
>But that then messes with multiple office versions and late bindings doesn't it?

Yes, yes it does.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top