Adding a file/folder browser button to a MS Access form

1. Folder

    Dim objFD As Object
    Dim strOut As String

    strOut = vbNullString
    'msoFileDialogFolderPicker = 4
    Set objFD = Application.FileDialog(4)
    If objFD.Show = -1 Then
        strOut = objFD.SelectedItems(1)
    End If
    Set objFD = Nothing
    FolderSelection = strOut

    Dim strChoice As String
    strChoice = FolderSelection
    If Len(strChoice) > 0 Then
        Me.Text1 = strChoice
    Else
        ' what should happen if user cancelled selection?
    End If

2. File

    Dim objDialog As Object
    
    Set objDialog = Application.FileDialog(3)
    
    With objDialog
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count = 0 Then
            MsgBox "No file selected."
        Else
            'Me.Text1 = Dir(.SelectedItems(1))
            Me.Text1 = .SelectedItems(1)
        End If
    End With
    Set objDialog = Nothing

猜你喜欢

转载自tjj006.iteye.com/blog/2014469
今日推荐