PDA

View Full Version : Problem with Common File Dialog Box



bosque
05-18-2011, 03:05 PM
Hi all. I'm relatively new to access, and I've had several issues getting my current database built. At this point everything is working well, except for the stupid file dialog box. I'm using Access 2007, FYI.

I found code online to open the dialog box and modified it to work with my tables. My problem is I get a runtime error that says user defined type is not defined. The error fires on my "Dim fDialog as Office.FileDialog" line. The specific error is "Compile error: User-defined type not defined". What am I doing wrong? I see the line in the code that says "' Requires reference to Microsoft Office 11.0 Object Library." Am I supposed to manually add a reference to some library somewhere in my code??

My code is here below:
Private Sub btn_retina_import_Click()
' Requires reference to Microsoft Office 11.0 Object Library.

Dim fDialog As Office.FileDialog
Dim varFile As Variant

lblStat.Caption = "Importing Retina Data"

' 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 "Retina XML Data", "*.XML"
.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."
lblStat.Caption = "Idle"
Exit Sub
End If
End With

'Call up the XML Parser
i = Filelist.ListCount
i = i - 1
Do Until i = -1
X = Parse_Retina(Filelist.ItemData(i)) 'Call input method
i = i - 1
Loop
lblStat.Caption = "Idle"

hansup
05-19-2011, 07:12 AM
You needn't bother with setting a reference if you make these 2 changes to your code:

'Dim fDialog As Office.FileDialog
Dim fDialog As Object

'Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
Set fDialog = Application.FileDialog(3)

However, if you want to add the reference instead, go to the Visual Basic editor and choose Tools, then References from the main menu. Scroll down the list of Available References until you find the Microsoft Office Object Library, and place a check mark in the box next to it. Then click OK.

With Access 2007, the available version will be Microsoft Office 12.0 Object Library, rather than Microsoft Office 11.0 Object Library.

You can get to the Visual Basic Editor in several ways. From the Access main window, you can open a code module in design view. Or press Alt+F11. I use Ctrl+g, which opens the Immediate Window in the Visual Basic editing environment.

bosque
05-19-2011, 07:22 AM
Thanks! That worked great!!