如何在Access 2007中显示文件选择器对话框?

时间:2009-11-13 11:18:00

标签: ms-access ms-access-2007

我想显示一个对话框,用户可以在其中选择文件,单击“确定”,然后该文件的路径将保存在数据库中。

我只有一个问题,我无法弄清楚如何显示对话框窗口。你呢?

4 个答案:

答案 0 :(得分:1)

您可以使用WinAPI。导入

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

您还必须导入OPENFILENAME结构。

Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

然后填写结构并调用GetOpenFileName。

 Dim of As OPENFILENAME

of.lStructSize = Len(of)
of.hwndOwner = Access.hWndAccessApp
of.hInstance = vbNull
of.lpstrFilter = m_strFilter ' *.doc for example
of.nFilterIndex = 1
of.lpstrFile = String(257, 0)
of.nMaxFile = Len(of.lpstrFile) - 1
of.lpstrFileTitle = of.lpstrFile
of.nMaxFileTitle = of.nMaxFile
of.lpstrInitialDir = m_strDirectory ' Folder to start
of.lpstrTitle = m_strTitle ' Title of dialog window
of.Flags = 0

If GetOpenFileName(of) <> 0 Then
    filename = VBString(of.lpstrFile)
end if

其中VBString是一个帮助函数,用于转换以null结尾的字符串。

Private Function VBString(str As String) As String
   Dim pos As Integer
   pos = InStr(1, str, Chr(0), vbTextCompare)
   VBString = Left(str, pos - 1)
End Function

答案 1 :(得分:1)

与@ dwo的答案相似:How to display the Common 'File-Open' Dialog to Choose a File

创建一个新模块并将代码粘贴到新模块中 在上面的链接中还有一个如何使用它的例子。

答案 2 :(得分:0)

不要忘记fileDialog对象,简单,允许多种选择,fileOpen,文件夹选择等,以这种方式使用:

Dim m_fileList As FileDialog, _
    i as long

'my choice here: pick up multiple files. Other options are available'
Set m_fileList = Application.FileDialog(msoFileDialogFilePicker)
m_fileList.AllowMultiSelect = True
m_fileList.InitialFileName = myDefaultFolder
m_fileList.InitialView = msoFileDialogViewDetails
m_fileList.Title = yourTitle
m_fileList.InitialFileName = myDefaultFileName

'we can add a file extension filter serie'
'm_fileList.filters(i) = ...'

If m_fileList.Show = -1 And m_fileList.SelectedItems.Count > 0 Then
    For i = 1 To m_fileList.SelectedItems.Count
        debug.print m_fileList.selectedItems(i).
    Next i
End If

答案 3 :(得分:0)

This code做到了。