Get path of file

Vassago

Former Staff Turned AWF Retiree
Local time
Today, 12:29
Joined
Dec 26, 2002
Messages
4,696
I have a button that allows a user to select a file using the windows select file dialog using the following code:

Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
.Title = "Select A Text file to Import"
.Filters.Add "Text File", "*.txt"
.FilterIndex = 1
.AllowMultiSelect = False
.InitialFileName = CurrentProject.path
result = .Show
If (result <> 0) Then
Me!txt_path = Trim(.SelectedItems.Item(1))
End If
End With

Is there a way that I can capture the path of the file they select in a separate text box?

Thanks!

Vassago
 
Nevermind, found a workaround.
 
Hi vassago,

This code gives me a table of all files anf thier path on a CD and places them in a table.

Code:
Private Sub CmdReadMedia_Click()

    Dim Drivename As String
    
    Drivename = Forms!frmMediaLabeller!CboDriveName & ":\"
    
    Dim dbs As Database
    Dim Mysize
    Set dbs = OpenDatabase("C:\CD Labeller - Microsoft Access 2003\CDlabel.mdb")
    
    'Clear the tblFilename table before scan is added.
    dbs.Execute "DELETE * FROM tblFilename;"
    
    Set fs = Application.FileSearch
    With fs
        .LookIn = Drivename
        .FileName = "*.*"
        .SearchSubFolders = True
        If .Execute(SortBy:=msoSortByFileName, _
                SortOrder:=msoSortOrderAscending) > 0 Then
            MsgBox "There were " & .FoundFiles.Count & _
                " file(s) found."
            For I = 1 To .FoundFiles.Count
                Mysize = FileLen("" & .FoundFiles(I) & "")
                'send data to a message box for test purpose.
                'MsgBox .FoundFiles(I)
                
                'send data from Sql to variable Sql.
                SQL = "INSERT INTO tblFilename" & _
                "(fldFieldname, fldFileSize) Values " & _
                "('" & .FoundFiles(I) & "', '" & Mysize & "');"
                'send data from sql to message box
                'MsgBox Mysize
                'send data form Sql to database tblFilename.
                dbs.Execute SQL
                
             Next I
                MsgBox "The folderpath and filenames have been written to table tblFilename succesfully."
                'close database connection.
                dbs.Close
        Else
            MsgBox "There were no files found."
        End If
    End With


End Sub

Hope you get an idea from it?

Robert88
 
Last edited:
The ParseFileName() function in my fFindOpenImportFile form in my Browse [Find a directory or file] sample will show you how I do it. I am not displaying the "sLocation" string in my sample but that will do what you want.
 

Users who are viewing this thread

Back
Top Bottom