文件对话框不起作用 [英] FileDialog doesn't work

查看:29
本文介绍了文件对话框不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经很认真地看了,但找不到可以直接解决我的问题的帖子.

I've looked quite intensely, but couldn't find a post that directly solves my problem.

我创建的表单的以下代码适用于我在工作中使用的 Access 2003.

The following code for a 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

但是,当我尝试在个人计算机上的 Access 2010 中的表单上运行相同的代码时,它不起作用.错误消息突出显示了第一行并显示未定义用户定义的类型".我也尝试将 FileName 声明为 Office.FileDialog,但也没有运气.我确实有 Microsoft Access 14.0 对象库作为正在使用的引用之一,所以我不知道这有什么问题.

However, when I tried to run the same code on a form in Access 2010 on my personal computer, it doesn't work.The error message highlights the first line and says "User-defined type not defined." I also tried declaring FileName as Office.FileDialog, but no luck either. I do have Microsoft Access 14.0 Object Library as one of the references in use, so I don't know what's wrong with that.

我只使用 Access 两周,我所有的知识都来自谷歌搜索,所以很可能我遗漏了一些明显的东西.

I've only been using Access for two weeks, and all my knowledge come from googling, so it's very likely that I'm missing something obvious.

推荐答案

FileDialog 对象不是由 Access 库提供的,而是由 Office 库提供的.因此,如果您设置对 Microsoft Office [版本号] 对象库的引用,您的代码应该可以工作.要么你没有那个参考集,要么它坏了.

The FileDialog object is not provided by the Access library, but by the Office library. So your code should work if you set a reference to the Microsoft Office [version number] Object Library. Either you don't have that reference set, or it's broken.

但是,如果是我,我会不设置引用并像这样修改代码.看看它是否适合你.

However if it were me, I would leave the reference unset and modify the code like this. See if it 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

这篇关于文件对话框不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆