-1

我如何比较两个 MS ACCESS 2007 数据库。两个数据库都包含具有相同字段广告结构的相同表。我需要比较两个数据库之间的记录值以检测记录值的任何差异。

                           ACCESS 2007 Database1                                

             serial no.     |    NAME         | ADDRESS                   
                1                smith           street 1                                
                2                john            street 4                                  
                3                alix            street 8                   


                           ACCESS 2007 Database2

               serial no.|   NAME     | ADDRESS
               1           smith      street 1              
               2           jhn        stret 4               
               3           alix       street 8

我需要一个用于 ms 访问的 VBA 代码,它可以检测记录的差异,就像序列号 2 的记录一样。

4

2 回答 2

0
Option Compare Database

Private Sub Command4_Click()

Dim tablename1, tablename2 As String
tablename1 = Text0.Value
tablename2 = Text2.Value

'On Error GoTo Err_cmdValidateGeneralInfo_Click
Dim F As DAO.Field
Dim rs As DAO.Recordset
Dim rs1 As DAO.Recordset
Set curDB = CurrentDb()
'If Me.DateModified = Date Then
    'Adds new employees to the TT_GeneralInfo table in the FTEI_PhoneBook.mdb - which is used thru out the AP databases.
 '   DoCmd.OpenQuery "qryEmpData_TT_General"

strsql = "Select * from " & tablename1

Set rs = curDB.OpenRecordset(strsql)

strsql1 = "Select * from " & tablename2

   DoCmd.CopyObject , "Unmatched_records", acTable, tablename1
   curDB.Execute "DELETE FROM Unmatched_records"

Set rs1 = curDB.OpenRecordset(strsql1)
    Do Until rs.EOF
      For Each F In rs.Fields
        If rs.Fields(F.Name) <> rs1.Fields(F.Name) Then
          'rs.Edit
          strsql = "Select * into test from " & tablename1 & " where " & F.Name & " = """ & rs.Fields(F.Name) & """"
          DoCmd.RunSQL strsql

          If DCount(F.Name, "test") <> 0 Then
          GoTo append_unmatch

          'appending unmacthed records
append_unmatch:

          strsql2 = "insert into Unmatched_records Select * from test"
          DoCmd.RunSQL strsql2

          'if record doesnt match move to next one
          GoTo Nextrecord
          End If
         ' rs.Fields(F.Name) = rs1.Fields(F.Name)
         ' rs.Update
        End If
      Next F

Nextrecord: 
rs.MoveNext
rs1.MoveNext
    Loop

   'To check whether tables matched or not
    Dim rs2 As DAO.Recordset
    strsql3 = "select * from Unmatched_records"
    Set rs2 = curDB.OpenRecordset(strsql3)
    For Each F In rs2.Fields
    If DCount(F.Name, "Unmatched_records") <> 0 Then
    MsgBox ("The two tables didnt match. Check table test for unmatching reocrds.")
    Else
    MsgBox ("Tables match!")
    End If
Exit Sub
   Next F
    rs2.Close


    End Sub
于 2012-12-28T08:53:47.417 回答
0

您应该做的第一件事是将其中一个表链接到另一个数据库,例如将数据库 2 表链接到数据库一个(这允许将两者一起查询)然后您可以使用这个简单的示例和连接来确定是否所有字段根据序列号串在一起匹配:

SELECT T1.*, T2.*
FROM Table1 As T1, Table2 As T2
WHERE T2.[serial no.] = T1.[serial no.]
AND T2.[NAME] & T2.[ADDRESS] <> T1.[NAME] & T1.[ADDRESS]

如果您愿意,还可以指定具有各自条件的列。注意:这是假设您只查找序列号不匹配的差异,如果您还需要识别可能出现在一个表中但不在另一个表中的记录,那么您将需要使用“不匹配”查询,查询设计师可以帮助您或回帖,我可以更新我的答案。

于 2012-04-05T08:57:02.517 回答