|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 fzxba 于 2024-2-20 13:45 编辑
- Sub test0() '简化一下
-
- Dim Conn As Object, rs As Object, SQL As String
- Dim i As Integer, j As Integer, p As Integer, t
-
- Cells.Clear
-
- Set Conn = CreateObject("ADODB.Connection")
- Set rs = CreateObject("ADODB.Recordset")
-
- Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.Path & "\Database1.accdb"
- Set rs = Conn.OpenSchema(20) 'adSchemaTables
- Do
- If rs!TABLE_TYPE = "TABLE" Then t = t & "|" & rs!TABLE_NAME
- rs.MoveNext
- Loop Until rs.EOF
- If rs.State = 1 Then rs.Close
-
- t = Split(t, "|")
- For j = 1 To UBound(t)
- p = Cells(2, Columns.Count).End(xlToLeft).Column
- If p > 1 Then p = p + 2
- SQL = "SELECT * FROM " & t(j)
- Set rs = Conn.Execute(SQL)
- Cells(1, p) = t(j)
- For i = 0 To rs.Fields.Count - 1
- Cells(2, p).Offset(, i) = rs.Fields(i).Name
- Next
- Cells(3, p).CopyFromRecordset rs
- Next
-
- If rs.State = 1 Then rs.Close
- If Conn.State = 1 Then Conn.Close
- Set rs = Nothing
- Set Conn = Nothing
- Beep
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|