如何擷取出Recordset的Table Name
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
本帖最後由 GBKEE 於 2018-2-7 11:04 編輯
回復 7# clio - Option Explicit
- '引用:Microsoft Ado Ext.2.x For DDL And Security
- Sub Ex_Ado()
- Dim myCat As New ADOX.Catalog, myBook As String, i As Integer
- Dim strUser As String, strPWD As String, Msg As String
- myBook = "d:\excel\資料庫.xlsx" '指定要查詢的工作簿完整名稱
- Msg = UCase(Split(myBook, ".")(UBound(Split(myBook, "."))))
- Select Case Msg '建立與指定工作薄的連接
- Case "XLS"
- myCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" _
- & "Extended Properties=Excel 8.0;" & "Data Source=" & myBook
- Case Is = "XLSM", Is = "XLSX"
- myCat.ActiveConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
- & "Extended Properties=""Excel 12.0 xml"";" & "Data Source=" & myBook
- Case "MDB"
- myCat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & myBook & _
- ";User ID=" & strUser & ";Jet OLEDB:Database Password=""" & strPWD & """;"
- End Select
- Cells.Clear
- Range("A1:B1") = Array("查詢出的名稱", "處理後的名稱")
- With myCat.Tables
- For i = 0 To .Count - 1
- Cells(i + 2, 1) = .Item(i).Name
- If Msg <> "MDB" Then Cells(i + 2, 2) = Mid(.Item(i).Name, 1, Len(.Item(i).Name) - 1)
- Next
- End With
- End Sub
複製代碼 |
|
|
|
|
|
|
|