【发布时间】:2015-03-13 03:16:59
【问题描述】:
我在驱动器 C: 上的一个主文件夹中的多个文件夹中有多个 csv 文件。有些文件每天都会更新。如果文件有更新,我需要将新的每日数据加载到包含文件名的访问表中。到目前为止,该脚本从所有 csv 文件中导入所有数据。然后它将文件名添加到新记录中。我需要将文件名添加到所有记录中。
任何帮助将不胜感激。
脚本:
子 Import_multiple_csv_files()
Const strPath As String = "C:\text1\" 'Directory Path
Dim strFile As String 'Filename
Dim strFileList() As String 'File Array
Dim intFile As Integer 'File Number
Dim rs As DAO.Recordset
'Loop through the folder & build file list
strFile = Dir(strPath & "*.csv")
While strFile <> ""
'add files to the list
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
strFile = Dir()
Wend
'see if any files were found
If intFile = 0 Then
MsgBox "No files found"
Exit Sub
End If
'cycle through the list of files & import to Access
'creating a new table called MyTable
For intFile = 1 To UBound(strFileList)
DoCmd.TransferText acImportDelimi, , _
"Test", strPath & strFileList(intFile)
‘add file name to record
Set rs = CurrentDb.OpenRecordset("Test")
rs.AddNew
rs.Fields("Com").Value = Dir(strPath & "*")
rs.Update
rs.Close
Set rs = Nothing
Next
MsgBox UBound(strFileList) & " Files were Imported"
结束子
【问题讨论】: