我需要将100多个CSV文件导入新的访问表
我正在努力使用代码,说实话,任何帮助都表示赞赏。这就是我到目前为止所做的:
Option Compare Database
Dim sql
Dim rs
Const strPath As String = "C:\Folder\" 'Directory Path
sql = "Select * from FileNames"
Set rs = CurrentDb.OpenRecordset(sql)
rs.MoveFirst
Do Until rs.EOF
'docmd.TransferText rs("fileName")
DoCmd.TransferText acImportDelimi, , "FileNames", strPath, True
rs.MoveNext
Loop
我已经有一个同时包含文件路径和文件名的表。格式如下:
C:\文件夹\ File17-08-01.csv
答案 0 :(得分:0)
循环“FileNames”记录集并使用TransferText方法导入每个文件。
将“YourTableName”更改为应导入文件的表名称。
Public Sub ImportFiles()
On Error GoTo ErrorTrap
Const strPath As String = "C:\Folder\"
Dim rs As DAO.Recordset
Set rs = CurrentDb().OpenRecordset("SELECT * FROM FileNames", dbOpenSnapshot)
With rs
If .EOF Then GoTo Leave
.MoveLast
.MoveFirst
End With
Dim idx As Long
For idx = 1 To rs.RecordCount
DoCmd.TransferText TransferType:=acImportDelim, _
TableName:="YourTableName", _
FileName:=strPath & rs![FileName], _
HasFieldNames:=True
rs.MoveNext
Next idx
Leave:
On Error Resume Next
rs.Close
Set rs = Nothing
On Error GoTo 0
Exit Sub
ErrorTrap:
MsgBox Err.Description, vbCritical
Resume Leave
End Sub