FileDialog not working

I looked pretty hard, but could not find a message that directly solves my problem.

The following code for the form I created works in Access 2003, which I use at work.

Dim FileName As FileDialog Set FileName = Application.FileDialog(msoFileDialogFilePicker) Dim Name As Variant With FileName .AllowMultiSelect = False .Show If .SelectedItems.Count = 0 Then MsgBox "No file selected." Exit Sub End If End With For Each Name In FileName.SelectedItems FileNameTextBox.Text = Mid$(Name, InStrRev(Name, "\") + 1) Next Name 

However, when I tried to run the same code in the Access 2010 form on my personal computer, it did not work. The error message highlights the first line and says, "The user-defined type is not defined." I also tried declaring FileName as Office.FileDialog , but no luck. I have a Microsoft Access 14.0 Object Library as one of the links used, so I don’t know what is wrong with this.

I only use Access for two weeks, and all my knowledge comes from googling, so it is very likely that I am missing something obvious.

+7
source share
2 answers

The FileDialog object is not provided by the Access library, but by the Office library. Therefore, your code should work if you link to the Microsoft Office Object Library [version number]. Either you do not have this set of links, or it is broken.

However, if it were me, I would leave the link disabled and change code like this. See if this works for you.

 Const msoFileDialogFilePicker As Long = 3 Dim objDialog As Object Set objDialog = Application.FileDialog(msoFileDialogFilePicker) With objDialog .AllowMultiSelect = False .Show If .SelectedItems.Count = 0 Then MsgBox "No file selected." Else Me.FileNameTextBox.Value = Dir(.SelectedItems(1)) End If End With 
+17
source

In tools, links ..., you should select "Microsoft Office 14.0 Object Library" instead of Microsoft Access.

0
source

All Articles