Hi all,
I just started using VBA in access and I hope someone can give me a hand with the following:
I have multiple text files in a folder that I need to import in a table, but I want to include the filename (excluding the txt extension) in the first field of my table. I have code that allows me to batch import the file contents, but I can't figure out how I can include the filenames in the first field of my table.
The code is below:
----------------------------------
Function Load_Attendance_Data()
Dim NextFile, ImportFile, FileCriteria, ctr As Variant
Dim DB_Path As Variant
' get the DB path
DB_Path = "C:\IWC\BirdSTAT\F1Files\"
FileCriteria = "C:\IWC\BirdSTAT\F1Files\" & "*.txt"
' create field with path and filename to import'
NextFile = Dir(FileCriteria)
' Check we have something to import
If NextFile = "" Then
MsgBox "No files to import", , "Error"
Exit Function
End If
ctr = 0
' Import each file that meets the criteria 'File for*.txt'
While NextFile <> ""
' count files imported
ctr = ctr + 1
' add the path to the returned filename
ImportFile = DB_Path & NextFile
' Import file into table
DoCmd.TransferText acImportDelim, "3711_0 Import Specification", "Attendance Import", ImportFile, False
' get another file if it exists
NextFile = Dir()
Wend
MsgBox ctr & " files imported", , "Attendance Import"
End Function
-----------------------------
Thanks!
I just started using VBA in access and I hope someone can give me a hand with the following:
I have multiple text files in a folder that I need to import in a table, but I want to include the filename (excluding the txt extension) in the first field of my table. I have code that allows me to batch import the file contents, but I can't figure out how I can include the filenames in the first field of my table.
The code is below:
----------------------------------
Function Load_Attendance_Data()
Dim NextFile, ImportFile, FileCriteria, ctr As Variant
Dim DB_Path As Variant
' get the DB path
DB_Path = "C:\IWC\BirdSTAT\F1Files\"
FileCriteria = "C:\IWC\BirdSTAT\F1Files\" & "*.txt"
' create field with path and filename to import'
NextFile = Dir(FileCriteria)
' Check we have something to import
If NextFile = "" Then
MsgBox "No files to import", , "Error"
Exit Function
End If
ctr = 0
' Import each file that meets the criteria 'File for*.txt'
While NextFile <> ""
' count files imported
ctr = ctr + 1
' add the path to the returned filename
ImportFile = DB_Path & NextFile
' Import file into table
DoCmd.TransferText acImportDelim, "3711_0 Import Specification", "Attendance Import", ImportFile, False
' get another file if it exists
NextFile = Dir()
Wend
MsgBox ctr & " files imported", , "Attendance Import"
End Function
-----------------------------
Thanks!