2013-07-09 11 views
11

में सभी टेबल्स के माध्यम से लूप कैसे करें मुझे एक्सेस 2003 डेटाबेस में 100 से अधिक टेबलों के गुणों को पढ़ने और उन विवरणों को लिखने की आवश्यकता है - तालिका का नाम, फ़ील्ड नाम, प्रकार और आकार - आगे के दस्तावेज़ के लिए फ़ाइल में ।एमएस एक्सेस डीबी

मैं क्षेत्र के गुण, बस क्षेत्र मूल्यों को पढ़ने के बारे में वेब खोज से कुछ भी नहीं मिल सकता है ...

कर सकते हैं किसी कृपया मुझे बताओ क्या recordset चर मैं (और वाक्य रचना) पाश करने के लिए टेबल के सभी के माध्यम से घोषित करने के लिए है डीबी में और उनमें से प्रत्येक से क्षेत्र का नाम, प्रकार और आकार निकालें? मैं परिणाम एक टेक्स्ट फ़ाइल में लिखूंगा, लेकिन मुझे लगता है कि मैं इसे संभाल सकता हूं! :)

मैं इसे तब तक खड़ा कर सकता हूं जब तक कि मैं इसे हल नहीं कर सकता। मुझे मैन्युअल रूप से TWO तालिकाओं को दस्तावेज़ करने के लिए एक दिन लगा। कुछ तालिकाओं में 100 से अधिक फ़ील्ड हैं।

उत्तर

16

इन विकल्पों के साथ डेटाबेस डॉक्यूमेंटर विज़ार्ड आपको कम से कम प्रयास के साथ क्या करना चाहता है।

enter image description here

अगर ऐसा दृष्टिकोण संतोषजनक नहीं है, आप इच्छित जानकारी इकट्ठा करने के लिए कस्टम VBA कोड का उपयोग कर सकते हैं। आप डीएओ टेबलडिफ संग्रह के माध्यम से लूप करके अपने डेटाबेस में टेबल के नाम पुनर्प्राप्त कर सकते हैं।

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 

क्षेत्र विवरण आप चाहते हैं पाने के लिए, Debug.Print बयानों के लिए एलन ब्राउन की TableInfo() function ... स्थानापन्न फ़ाइल लिखने बयान अनुकूलन। ध्यान दें कि फ़ंक्शन 2 सहायक फ़ंक्शन, GetDescrip और FieldTypeName का उपयोग करता है, जिनमें से दोनों उस लिंक किए गए पृष्ठ में शामिल हैं।

मेरे डेटाबेस में एक तालिका के लिए TableInfo() से एक तत्काल विंडो आउटपुट नमूना है --- मुझे लगता है कि इसमें फ़ील्ड जानकारी शामिल है जो आप चाहते हैं।

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

के बाद आप समारोह अनुकूलित किया है, उपर्युक्त नमूने में For Each tdf पाश से इसे कहते हैं और यह प्रत्येक tdf.name फ़ीड:

TableInfo tdf.name 
4

आपको इसे थोड़ा सा ट्विक करना होगा, यह एक डेटाबेस से दूसरे डेटाबेस में टेबल कॉपी करने के लिए डिज़ाइन किया गया है लेकिन यह एक महान प्रारंभिक बिंदु होना चाहिए।

' 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

तुम दोनों के लिए धन्यवाद। मैं देख सकता हूं कि अब मुझे क्या करना है। उत्तम सामग्री। – Capfka