Search code examples
vb6

how to get the file path from button click and print it on textbox VB6


I have a button and a textbox and I want to make the button to open the folder by a default path.

I will give example:

    'C:\Folder\...

    Dim path as string
    path = "C:\Folder\.."
    Dim fso
    set fso = createobject("Scripting FileSystemObjh)

I tried some method where it only opens the folder but doesn't get the file path so not what I want to do.

And when I select the file and click okay the file path to be printed on the textbox.

Thank you in advance!


Solution

  • If I understand what you need, the following should point you in the right direction. First, select the following Component:

    Microsoft Common Dialog Control 6.0 (SP6)

    Second, select the following Reference:

    Microsoft Scripting Runtime

    Third, drop a CommonDialog control onto your form along with the CommandButton and TextBox. Type the following code:

    Option Explicit
    
    Private Sub Command1_Click()
       Dim fso As FileSystemObject
       Dim path As String
       
       path = "c:\temp\"
       
       CommonDialog1.InitDir = path
       CommonDialog1.Filter = "Text Files (*.txt)|*.txt|All files (*.*)|*.*"
       CommonDialog1.DefaultExt = "txt"
       CommonDialog1.DialogTitle = "Select File"
       CommonDialog1.ShowOpen
       
       Text1.Text = CommonDialog1.FileName
       
       Set fso = New FileSystemObject
       'use fso to do whatever you need
    End Sub