|
- Sub 从Excel工作表中向数据表添加纪录()
- Dim Fso As Object, File As Object, cnn As Object, rs As Object, SQL$, myTable$, sh$
- Set cnn = CreateObject("ADODB.Connection")
- myTable = "[MS Access;pwd=;Database=" & ThisWorkbook.Path & "\data.accdb;].不合格汇总"
- Set Fso = CreateObject("Scripting.FileSystemObject")
- For Each File In Fso.GetFolder(ThisWorkbook.Path).Files
- If File.Name Like "*.xlsx" Then
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "provider=microsoft.Ace.oledb.12.0;extended properties=excel 12.0;data source=" & File
- Set rs = cnn.OpenSchema(20)
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- sh = Replace(rs("TABLE_NAME").Value, "'", "")
- If Right(sh, 1) = "$" Then
- SQL = "INSERT INTO " & myTable & "([ACC Number],Problem) SELECT 家具号 as [ACC Number],备注 as Problem FROM [" & sh & "]"
- cnn.Execute SQL
- End If
- End If
- rs.MoveNext
- Loop
- End If
- Next
- MsgBox "纪录添加成功。", vbInformation, "添加纪录"
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- Set Fso = Nothing
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|