1

我有一个 Access 数据库,我需要对网站(应用程序和数据)进行逆向工程。

我刚刚尝试使用 Outlook 2013 打开它,但我收到一个错误,即数据库已启用复制并且它是使用早期版本的 Access 创建的(有影响的东西)。

我认为这只是复制问题。有没有办法删除复制,以便我可以在 Access 2013 中使用它?

4

1 回答 1

3

因此,取消复制数据库并非易事。

首先我找到了 WV Mitchell 的 VBA 脚本,它可以将你的表复制到一个新的数据库中: http ://www.wvmitchell.com/tips/Removing%20Access%20Replication.htm

这不会复制主键或关系,所以我写了一些快速而肮脏的 VBA 脚本来帮助解决这个问题。

第 1 步:在MakeOneTable上述脚本的函数中,我添加了这个(在 db.Close 行上方):

Dim td As TableDef
Dim idxLoop As Index

Set td = db.TableDefs(TableName)
For Each idxLoop In td.Indexes
    If idxLoop.Primary = True Then

        Dim colnames As String
        colnames = idxLoop.Fields
        colnames = Replace(colnames, ";+", "],[")
        colnames = Replace(colnames, "+", "[")
        colnames = colnames & "]"
        Debug.Print "DoCmd.RunSQL ""CREATE INDEX [PrimaryKey] ON [" & TableName & "] (" & colnames & ") With Primary;"""

        Exit For
    End If
Next idxLoop

这将向调试(立即)窗口输出一些用于创建主键的 VBA 代码(以及创建新数据库并将数据导出到它 - MV Mitchell 的代码)。复制该 VBA 代码,将其放入新数据库中的模块/宏中,然后运行它 - 它应该创建主键。

第 2 步:在旧(复制的)数据库中,运行此 VBA 代码,这将生成更多用于创建关系的 VBA 代码:

Sub GenerateRelationshipCode()
    Dim db As DAO.Database
    Set db = CurrentDb()
    Dim rs As DAO.Recordset

    Set rs = db.OpenRecordset("SELECT DISTINCT szRelationship,szObject,szReferencedObject FROM MSysRelationships ORDER BY szObject,szReferencedObject")
    rs.MoveFirst

    Do While Not rs.EOF

        Dim rsFields As DAO.Recordset
        Set rsFields = db.OpenRecordset("SELECT * FROM MSysRelationships WHERE szRelationship = '" & Replace(rs.Fields(0), "'", "''") & "'")

        Dim PKFields As String, PKTable As String, FKFields As String, FKTable As String
        PKFields = "": PKTable = "": FKFields = "": FKTable = ""

        Do While Not rsFields.EOF
            PKFields = PKFields & rsFields("szReferencedColumn") & ","
            PKTable = rsFields("szReferencedObject")
            FKFields = FKFields & rsFields("szColumn") & ","
            FKTable = rsFields("szObject")
            rsFields.MoveNext
        Loop

        PKFields = Left(PKFields, Len(PKFields) - 1)
        FKFields = Left(FKFields, Len(FKFields) - 1)

        Debug.Print "Call AddRelationship(""" & rs.Fields(0) & """, """ & FKTable & """, """ & FKFields & """, """ & PKTable & """, """ & PKFields & """)"
        rs.MoveNext
    Loop

    Set db = Nothing
End Sub

您需要针对新数据库运行上述输出,但您还需要此函数:

Public Sub AddRelationship(Name As String, FKTable As String, FKFields As String, PKTable As String, PKFields As String)
    Dim strSQL As String
    Dim db As DAO.Database
    Set db = CurrentDb()
    Name = "FK_" & Replace(FKTable, " ", "") & "_" & Replace(PKTable, " ", "") 'only enable this line if there aren't multiple relationships between same 2 tables
    strSQL = "ALTER TABLE [" & FKTable & "] " & _
    "  add constraint " & Name & " foreign key (" & FKFields & ") " & _
    "   references [" & PKTable & "](" & PKFields & ") "
    db.Execute strSQL, dbFailOnError
    Set db = Nothing
End Sub

在具有上述功能的模块中运行生成的 VBA 代码,它应该在新数据库中重建您的关系。

请注意,此代码快速且肮脏,因此可能需要对您的数据库/数据进行一些错误修复。

于 2013-11-14T07:30:14.073 回答