Add Filename to Import Text Files Code

I have the code below that is working as far as importing data from text files automatically into an access database (MS Access 2010). However, I added a part to the VBA that would hopefully pull in the filename along with the data. It is not working as expected. It's only pulling in the first filename from the first file in the folder, and assigning it to all records. (line 20) Each record needs to have it's own unique file name assigned. Any ideas what's wrong?

Private Sub bImportFiles_Click()

    
    Dim objFS As Object, objFolder As Object
    Dim objFiles As Object, objF1 As Object
    Dim strFolderPath As String
    Dim myfile
    
    strFolderPath = "C:\My Documents\TextFiles\"
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder(strFolderPath)
    Set objFiles = objFolder.files
    myfile = Dir(strFolderPath)
    
    For Each objF1 In objFiles
        If Right(objF1.Name, 3) = "txt" Then
            DoCmd.TransferText acImportDelim, "TextImportSpecification", "tblImportedFiles", strFolderPath & objF1.Name, False
            
 '<<--this is the part I added to try and pull in filename to each respective record.
            CurrentDb.Execute "update [tblImportedFiles] set [FileNameField]='" & myfile & "' where [FileNameField] is null"

           ' Name strFolderPath & objF1.Name As "C:\Users\u287844\Downloads\TextFiles\Move\" & objF1.Name 'Move the files to the archive folder
        End If
Next
    
End Sub

Open in new window

ctownsen80Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rey Obrero (Capricorn1)Commented:
revise your codes like this


   For Each objF1 In objFiles
        If Right(objF1.Name, 3) = "txt" Then
            
           myfile=objF1.name
            
            DoCmd.TransferText acImportDelim, "TextImportSpecification", "tblImportedFiles", strFolderPath & objF1.Name, False
           
 '<<--this is the part I added to try and pull in filename to each respective record.
            CurrentDb.Execute "update [tblImportedFiles] set [FileNameField]='" & myfile & "' where [FileNameField] is null"

           ' Name strFolderPath & objF1.Name As "C:\Users\u287844\Downloads\TextFiles\Move\" & objF1.Name 'Move the files to the archive folder
        End If
Next

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewIT / Software Engineering ConsultantCommented:
A few other things to consider, to reduce the number of variables a bit and keep things cleaner.  Questions welcome.

Private Sub bImportFiles_Click()

    Dim objFS As Object
    Dim objFolder As Object
    Dim objF1 As Object
    Dim strFolderPath As String
    Dim strExtension As String
    
    strFolderPath = "C:\My Documents\TextFiles\"
    strExtension = ".txt"
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFS.GetFolder(strFolderPath)
    
    For Each objF1 In objFolder.Files
        If LCase(Right(objF1.Name, Len(strExtension))) = LCase(strExtension) Then
            DoCmd.TransferText acImportDelim, "TextImportSpecification", "tblImportedFiles", objF1.Path, False
            
            CurrentDb.Execute "update [tblImportedFiles] set [FileNameField]='" & objF1.Name & "' where [FileNameField] is null"

           ' Name objF1.Path As "C:\Users\u287844\Downloads\TextFiles\Move\" & objF1.Name 'Move the files to the archive folder
        End If
    Next
    
End Sub

Open in new window

~bp
ctownsen80Author Commented:
Thanks Everyone for your help !
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.