Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ms access 如何循环访问MS Access数据库中的所有表_Ms Access_Vba - Fatal编程技术网

Ms access 如何循环访问MS Access数据库中的所有表

Ms access 如何循环访问MS Access数据库中的所有表,ms-access,vba,Ms Access,Vba,我需要读取Access 2003数据库中100多个表的属性,并将这些详细信息(表名、字段名、类型和大小)写入一个文件以供进一步的文档记录 我在网络搜索中找不到任何关于读取字段属性的信息,只找到字段值 有人能告诉我我必须声明哪些记录集变量(以及语法)才能循环遍历数据库中的所有表,并从每个表中提取字段名、类型和大小吗?我将把结果写入一个文本文件,但我想我能处理它!:) 在我解决这件事之前,我一直处于停顿状态。我花了一天的时间手动记录了两个表。有些表有超过100个字段。您需要对此进行一些调整,它旨在将

我需要读取Access 2003数据库中100多个表的属性,并将这些详细信息(表名、字段名、类型和大小)写入一个文件以供进一步的文档记录

我在网络搜索中找不到任何关于读取字段属性的信息,只找到字段值

有人能告诉我我必须声明哪些记录集变量(以及语法)才能循环遍历数据库中的所有表,并从每个表中提取字段名、类型和大小吗?我将把结果写入一个文本文件,但我想我能处理它!:)


在我解决这件事之前,我一直处于停顿状态。我花了一天的时间手动记录了两个表。有些表有超过100个字段。

您需要对此进行一些调整,它旨在将表从一个数据库复制到另一个数据库,但这应该是一个很好的起点

    ' 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

具有这些选项的数据库文档管理器向导应该以最少的工作量为您提供所需的内容

如果这种方法不令人满意,可以使用自定义VBA代码来收集所需的信息。通过循环DAO TableDefs集合,可以检索数据库中表的名称

Dim db作为DAO.Database
将tdf调暗为DAO.TableDef
Set db=CurrentDb
对于以db.TableDefs为单位的每个tdf
'忽略系统和临时表
如果不是(tdf.name如“MSys*”或tdf.name如“~*”),则
调试。打印tdf.name
如果结束
下一个
设置tdf=无
Set db=Nothing
要获得您想要的现场详细信息,请改编艾伦·布朗的。。。用文件写入语句代替
Debug.Print
语句。请注意,该函数使用了两个辅助函数,
getdescripp
FieldTypeName
,这两个函数都包含在链接页面中

这是我数据库中一个表的
TableInfo()
的即时窗口输出示例,我想它包含了您想要的字段信息

TableInfo“foo”
字段名称字段类型大小说明
==========    ==========    ====          ===========
自动识别号码4
MyNumber长整数4
MyText文本255
条形长整数4
==========    ==========    ====          ===========
调整函数后,从上面示例中每个tdf的
循环调用它,并为每个
tdf提供它。name

TableInfo tdf.name

以下子项将所有表名、字段名、类型、必填项和默认值导出到excel工作表中

Sub TableDef()
 Dim def As TableDef
 Dim wb As Object
 Dim xL As Object
 Dim lngRow As Long
 Dim f As Field
 Set xL = CreateObject("Excel.Application")
 xL.Visible = True
 Set wb = xL.workbooks.Add
 lngRow = 2
 For Each def In CurrentDb.TableDefs
   For Each f In def.Fields
     With wb.sheets("Sheet1")
           .Range("A" & lngRow).Value = def.Name
           .Range("B" & lngRow).Value = f.Name
           .Range("C" & lngRow).Value = f.Type
           .Range("D" & lngRow).Value = f.Size
           .Range("E" & lngRow).Value = f.Required
           .Range("F" & lngRow).Value = f.DefaultValue
            lngRow = lngRow + 1
    End With
   Next
 Next
End Sub

谢谢你们两位。我知道我现在需要做什么了。好东西。