Consulting

Results 1 to 5 of 5

Thread: Using FileDialog to open and display FileName

  1. #1

    Using FileDialog to open and display FileName

    Hi pals,

    I would like to know how to display the FileName of the selected file and open it using FileDialog property. I have imported the necessary reference from the library - Microsoft Office Object Library 10.0. Tks

    Control:
    TextBox - Display FileName
    Button - Open

    Private Sub Command_Onclick()
    Dim fd as FileDialog
    Dim varSelected as Variant
    set fd = Application.FileDialog(msopen....)
    With fd
        .MultiSelect = true
        .Show
        if . show = -1 then
            for each varSelected .SelectedItem
            Next
       Else
           MsgBox "Cancel"
       End If
    End with
    End Sub
    Last edited by Aussiebear; 04-27-2023 at 03:41 AM. Reason: Added code tags

  2. #2
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Something like this maybe?

    Private Sub CommandButton1_Click()     
        Dim fd As FileDialog
        Dim varSelected As Variant
        Set fd = Application.FileDialog(msoFileDialogOpen)
        With fd
        .AllowMultiSelect = True
        If .Show = -1 Then
            Me.TextBox1.Text = vbNullString
            For Each varSelected In .SelectedItems
                Me.TextBox1.Text = Me.TextBox1.Text & "," & varSelected
                Next
            Else
            MsgBox "Cancel"
        End If
        End With
    End Sub
    Last edited by Aussiebear; 04-27-2023 at 03:40 AM. Reason: Adjusted the code tags
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  3. #3

    You cant reference a property or method for a control unless the control has a focus.

    First of all, tks for the prompt reply.

    However, I am still encountering some issues when i stepped in the code to debug

    Line
    Me.Text0.Text = vbNullString
    Me.Text0.Text = Me.Text0.Text & "," & varSeleced
    Error message
    Runtime error = '2185'
    You cant reference a property or method for a control unless the control has a focus.

    tks
    Last edited by Aussiebear; 04-27-2023 at 03:42 AM. Reason: Added code tags

  4. #4

    Display and open selected file using FileDialog - MS Access using visual basic

    Issue resolved. But the selected file still cannot be open and the name of the selected file is not display in the textbox. When the prog is executed, only , is displayed in the textbox


    Quote Originally Posted by technocraze
    First of all, tks for the prompt reply.

    However, I am still encountering some issues when i stepped in the code to debug

    Me.Text0.Text = vbNullString
    Me.Text0.Text = Me.Text0.Text & "," & varSeleced
    Error message
    Runtime error = '2185'
    You cant reference a property or method for a control unless the control has a focus.

    tks
    Last edited by Aussiebear; 04-27-2023 at 03:38 AM. Reason: Adjusted the code tags

  5. #5
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Try using the value property instead of the text property.
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •