|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 佚_名 于 2016-3-12 19:46 编辑
我手里有一个例子是把多个csv文件导入一张表里,想请教一下不同的csv文件导入不同的表怎么做,表名是以csv文件的名字命名,不加后缀,字段名为csv第一行,求修改!!!以下是主代码:
1-14012Q53209.rar
(438.92 KB, 下载次数: 40)
- Option Compare Database
- Public conn As New ADODB.Connection 'ADO
- Public rs As New ADODB.Recordset
- Public rs1 As New ADODB.Recordset
- Public Function ReadCSVFile(ByVal strFilePath As String, ByVal strFileName As String)
- Dim i As Integer
- '===============================================================================
- '-函数名称: ReadCSVFile
- '-功能描述: 导入csv文件
- '-输入参数说明: 参数1:strFilePath As String 目标路径
- ' 参数2:strFileName As String 文件名称
- '-使用语法示例: ReadCSVFile("路径","文件名")
- '===============================================================================
- conn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
- "DBQ=" & strFilePath & ";Extensions=asc,csv,tab,txt;Persist Security Info=False" '打开数据链接关键
- With rs
- .CursorType = 2
- .LockType = 3
- .CursorLocation = 3
- .ActiveConnection = conn
- End With
- Set conn = CurrentProject.Connection
- sqlcsv = "SELECT * FROM [" & strFileName & "]"
- rs.Open sqlcsv
- rs.Move (0) '移动到指定记录
- sql = "Select * From tbl_Export"
- rs1.Open sql, conn, 1, 3
- Do Until rs.EOF
- rs1.AddNew
- For i = 0 To 7
- rs1(i + 1) = rs(i)
- Next
- rs.MoveNext
- Loop
- rs1.UpdateBatch
- rs1.Close
- rs.Close
- conn.Close
- Set rs = Nothing
- Set rs1 = Nothing
- Set conn = Nothing
- End Function
复制代码
|
|