2011年11月9日 星期三

取得ACCESS資料庫內所有資料表相關資訊


'須引用Microsoft ActiveX Data Objects 2.5 Library
Private Sub Command1_Click()
  Dim conConnection As New ADODB.Connection
  Dim cmdCommand As New ADODB.Command
  Dim rstRecordSet As New ADODB.Recordset
  conConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
      App.Path & "\" & "database.mdb;Mode=Read|Write"
  conConnection.CursorLocation = adUseClient
  conConnection.Open
  
    'Get all database tables.
     Set TablesSchema = conConnection.OpenSchema(adSchemaTables)
     Do While Not TablesSchema.EOF
                  
         If TablesSchema("TABLE_TYPE") = "TABLE" Then
             'Get all table columns.
             Set ColumnsSchema = conConnection.OpenSchema(adSchemaColumns, Array(Empty, Empty, "" & TablesSchema("TABLE_NAME")))
             Do While Not ColumnsSchema.EOF
               Debug.Print TablesSchema("TABLE_NAME") & ", " & _
                 ColumnsSchema("COLUMN_NAME")
               ColumnsSchema.MoveNext
             Loop
         End If
       TablesSchema.MoveNext
     Loop


  conConnection.Close

  Set conConnection = Nothing
  Set cmdCommand = Nothing
  Set rstRecordSet = Nothing
End Sub

沒有留言:

張貼留言