我正在尝试将同一目录中的多个 csv 文件导入 MS Access 2016。
这是我目前拥有的 VBA 模块,感谢物理2010,但不起作用。有什么建议吗?
Option Compare Database
Option Explicit
Function DoImport()
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in CSV worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the CSV files
strPath = "C:\Users\xxx"
' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "Table1"
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strTable = Left(strFile, Len(strFile) - 4)
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
MsgBox "done"
End Function
答案1
谢谢阿兹蒙,问题出在我的 strPath 中,而不是strPath = "C:\Users\xxx"
应该是strPath = "C:\Users\xxx\"
答案2
这可以导入到一个数据文件中吗?代码可以工作,但是每次我执行此操作时,它都会将每个文件导入为单独的数据库。