浏览并获取图像路径,然后将选择加载到Access窗体的文本框中

时间:2015-05-21 14:53:31

标签: ms-access access-vba ms-access-2010

我正在使用HansUp的代码:

Private Sub Command7_Click()
    Dim f As Object
    Dim strFile As String
    Dim strFolder As String
    Dim varItem As Variant

    Set f = Application.FileDialog(3)
    f.AllowMultiSelect = True
    If f.Show Then
        For Each varItem In f.SelectedItems
            strFile = Dir(varItem)
            strFolder = Left(varItem, Len(varItem) - Len(strFile))
            MsgBox "Folder: " & strFolder & vbCrLf & _
                "File: " & strFile
        Next
    End If
    Set f = Nothing
End Sub

但是,我希望将我选择的绝对路径放入文本框而不是弹出窗口。

我正在学习。

1 个答案:

答案 0 :(得分:1)

我不明白为什么你得到错误438,所以只是编写并测试了这个版本。

我的命令按钮名为 cmdBrowse ,我存储所选文件路径的文本框名为 MyTextBox

Private Sub cmdBrowse_Click()
    Const msoFileDialogFilePicker As Long = 3
    Dim f As Object

    Set f = Application.FileDialog(msoFileDialogFilePicker)
    f.AllowMultiSelect = False
    If f.Show Then
        MsgBox f.SelectedItems(1)
        'Me!MyTextBox.Value = f.SelectedItems(1)
    End If
    Set f = Nothing
End Sub

验证MsgBox正确显示文件路径后,请禁用该行并启用下一行:

'MsgBox f.SelectedItems(1)
Me!MyTextBox.Value = f.SelectedItems(1)