Filepicker VBA 到 select 文件并存储文件名不起作用

Filepicker VBA to select file and store the file name not working

我正在尝试 运行 以下内容以获取用户选择的文件名。该文件是先前附加到 SQL 服务器的 .mdf 文件。但是当我 运行 它时,出现 window 并说我没有权限打开该文件。我知道这是因为它在 SQL 中使用,因为如果我不将它附加到 SQL 服务器中,它 运行 不会有问题。 问题是我在 运行 宁 vba 代码之前需要 SQL 中的 mdf,我只需要文件名。有没有办法在没有 "opening" 的情况下存储文件名?

Function GetDB() As String

Dim db As Office.FileDialog
Dim fileName As String
Set db = Application.FileDialog(msoFileDialogFilePicker)
With db
    .Title = "Select a Database"
    .AllowMultiSelect = False
    .InitialFileName = Application.DefaultFilePath
      Application.DisplayAlerts = False
    If .Show = True Then
    fileName = Mid(.SelectedItems(1), InStrRev(.SelectedItems(1), "\") + 1)
        End If
    End With
End Function

替换

sItem = .SelectedItems(1)

与:

GetDB = .SelectedItems(1)

我最终设置和 ADODB 连接直接从服务器获取数据库而没有 "The file is in use" 问题。