2013-07-09 34 views
11

Tôi cần đọc các thuộc tính của hơn 100 bảng trong cơ sở dữ liệu Access 2003 và ghi các chi tiết đó - tên bảng, tên trường, loại và kích cỡ - vào tệp để có thêm tài liệu .Làm thế nào để lặp qua tất cả các bảng trong MS Access DB

Tôi có thể tìm thấy gì từ các tìm kiếm web về đọc thuộc lĩnh vực, giá trị trường chỉ ...

Ai đó có thể xin vui lòng cho tôi biết những gì biến recordset tôi phải khai báo (và cú pháp) để lặp qua tất cả các bảng trong DB và trích xuất tên trường, loại và kích cỡ từ mỗi trường? Tôi sẽ viết kết quả cho một tập tin văn bản, nhưng tôi nghĩ rằng tôi có thể xử lý điều đó! :)

Tôi đang bế tắc cho đến khi tôi có thể sắp xếp điều này. Tôi mất một ngày để ghi lại HAI bảng theo cách thủ công. Một số bảng có hơn 100 trường.

Trả lời

16

Trình hướng dẫn Documenter cơ sở dữ liệu với các tùy chọn này sẽ cung cấp cho bạn những gì bạn muốn với ít nỗ lực nhất.

enter image description here

Nếu phương pháp đó là không thỏa đáng, bạn có thể sử dụng tùy chỉnh mã VBA để thu thập thông tin mà bạn muốn. Bạn có thể lấy tên của các bảng trong cơ sở dữ liệu của bạn bằng cách lặp qua bộ sưu tập DAO TableDefs.

Dim db As DAO.Database 
Dim tdf As DAO.TableDef 
Set db = CurrentDb 
For Each tdf In db.TableDefs 
    ' ignore system and temporary tables 
    If Not (tdf.name Like "MSys*" Or tdf.name Like "~*") Then 
     Debug.Print tdf.name 
    End If 
Next 
Set tdf = Nothing 
Set db = Nothing 

Để có được thông tin chi tiết lĩnh vực mà bạn muốn, thích ứng Allen Browne TableInfo() function ... thay thế báo cáo tập tin ghi cho Debug.Print báo cáo. Lưu ý rằng hàm sử dụng 2 hàm trợ giúp, GetDescripFieldTypeName, cả hai đều được bao gồm trong trang được liên kết đó.

Đây là mẫu đầu ra cửa sổ ngay lập tức từ TableInfo() cho bảng trong cơ sở dữ liệu của tôi --- Tôi nghĩ rằng nó bao gồm thông tin trường bạn muốn.

TableInfo "foo" 
FIELD NAME FIELD TYPE SIZE   DESCRIPTION 
========== ========== ====   =========== 
id   AutoNumber  4    
MyNumber  Long Integer 4    
MyText  Text   255   
bar   Long Integer 4    
========== ========== ====   =========== 

Sau khi bạn đã thích nghi chức năng, gọi nó là từ For Each tdf vòng lặp trong ví dụ trên và thức ăn nó mỗi tdf.name:

TableInfo tdf.name 
4

Bạn sẽ phải tinh chỉnh điều này một chút, nó được thiết kế để sao chép các bảng từ cơ sở dữ liệu này sang cơ sở dữ liệu khác nhưng nó phải là điểm khởi đầu tuyệt vời.

' Database. 
    Dim dbRep As DAO.Database 
    Dim dbNew As DAO.Database 

    ' For copying tables and indexes. 
    Dim tblRep As DAO.TableDef 
    Dim tblNew As DAO.TableDef 
    Dim fldRep As DAO.Field 
    Dim fldNew As DAO.Field 
    Dim idxRep As DAO.Index 
    Dim idxNew As DAO.Index 

    ' For copying data. 
    Dim rstRep As DAO.Recordset 
    Dim rstNew As DAO.Recordset 
    Dim rec1 As DAO.Recordset 
    Dim rec2 As Recordset 
    Dim intC As Integer 

    ' For copying table relationships. 
    Dim relRep As DAO.Relation 
    Dim relNew As DAO.Relation 

    ' For copying queries. 
    Dim qryRep As DAO.QueryDef 
    Dim qryNew As DAO.QueryDef 

    ' For copying startup options. 
    Dim avarSUOpt 
    Dim strSUOpt As String 
    Dim varValue 
    Dim varType 
    Dim prpRep As DAO.Property 
    Dim prpNew As DAO.Property 

    ' For importing forms, reports, modules, and macros. 
    Dim appNew As New Access.Application 
    Dim doc As DAO.Document 

    ' Open the database, not in exclusive mode. 
    Set dbRep = OpenDatabase(Forms!CMDB_frmUpgrade.TxtDatabase, False) 


    ' Open the new database 
    Set dbNew = CurrentDb 

    DoEvents 

    ' Turn on the hourglass. 
    DoCmd.Hourglass True 

    '******************** 
    Debug.Print "Copy Tables" 
    '******************** 
If Forms!CMDB_frmUpgrade.CkTables = True Then 
    Forms!CMDB_frmUpgrade.LstMessages.addItem "Copying Tables:" 

    ' Loop through the collection of table definitions. 
    For Each tblRep In dbRep.TableDefs 
    Set rec1 = dbRep.OpenRecordset("SELECT MSysObjects.Name FROM MsysObjects WHERE ([Name] = '" & tblRep.Name & "') AND ((MSysObjects.Type)=4 or (MSysObjects.Type)=6)") 

    If rec1.EOF Then 
     XF = 0 
    Else 
     XF = 1 
    End If 

     ' Ignore system tables and CMDB tables. 
     If InStr(1, tblRep.Name, "MSys", vbTextCompare) = 0 And _ 
      InStr(1, tblRep.Name, "CMDB", vbTextCompare) = 0 And _ 
      XF = 0 Then 

      '***** Table definition 
      ' Create a table definition with the same name. 
      Set tblNew = dbNew.CreateTableDef(tblRep.Name) 
      Forms!CMDB_frmUpgrade.LstMessages.addItem "--> " & tblRep.Name & "" 

      ' Set properties. 
      tblNew.ValidationRule = tblRep.ValidationRule 
      tblNew.ValidationText = tblRep.ValidationText 

      ' Loop through the collection of fields in the table. 
      For Each fldRep In tblRep.Fields 

       ' Ignore replication-related fields: 
       ' Gen_XXX, s_ColLineage, s_Generation, s_GUID, s_Lineage 
       If InStr(1, fldRep.Name, "s_", vbTextCompare) = 0 And _ 
        InStr(1, fldRep.Name, "Gen_", vbTextCompare) = 0 Then 

        '***** Field definition 
        Set fldNew = tblNew.CreateField(fldRep.Name, fldRep.Type, _ 
         fldRep.Size) 

        ' Set properties. 
        On Error Resume Next 
        fldNew.Attributes = fldRep.Attributes 
        fldNew.AllowZeroLength = fldRep.AllowZeroLength 
        fldNew.DefaultValue = fldRep.DefaultValue 
        fldNew.Required = fldRep.Required 
        fldNew.Size = fldRep.Size 

        ' Append the field. 
        tblNew.Fields.Append fldNew 
        'On Error GoTo Err_NewShell 
       End If 
      Next fldRep 

      '***** Index definition 

      ' Loop through the collection of indexes. 
      For Each idxRep In tblRep.Indexes 

       ' Ignore replication-related indexes: 
       ' s_Generation, s_GUID 
       If InStr(1, idxRep.Name, "s_", vbTextCompare) = 0 Then 

        ' Ignore indices set as part of Relation Objects 
        If Not idxRep.Foreign Then 

         ' Create an index with the same name. 
         Set idxNew = tblNew.CreateIndex(idxRep.Name) 

         ' Set properties. 
         idxNew.Clustered = idxRep.Clustered 
         idxNew.IgnoreNulls = idxRep.IgnoreNulls 
         idxNew.Primary = idxRep.Primary 
         idxNew.Required = idxRep.Required 
         idxNew.Unique = idxRep.Unique 

         ' Loop through the collection of index fields. 
         For Each fldRep In idxRep.Fields 
          ' Create an index field with the same name. 
          Set fldNew = idxNew.CreateField(fldRep.Name) 
          ' Set properties. 
          fldNew.Attributes = fldRep.Attributes 
          ' Append the index field. 
          idxNew.Fields.Append fldNew 
         Next fldRep 

         ' Append the index to the table. 
         tblNew.Indexes.Append idxNew 
        End If 
       End If 
      Next idxRep 

      ' Append the table. 
      dbNew.TableDefs.Append tblNew 
     End If 
    Next tblRep 
+0

Nhờ cả hai bạn. Tôi có thể thấy những gì tôi cần làm bây giờ. Công cụ tuyệt vời. – Capfka