我已经构建了一个宏,可以将大约 10 个不同表中的值从一个数据库移动到另一个数据库。它需要一个唯一标识符,所以说列“nid”并检查它是否已经存在于新数据库中,如果没有匹配,它会移动数据,如果存在匹配并且它已经存在它不存在。
这个宏工作正常,但是,我希望它检查值是否已经存在,是否检查每一列是否有任何更改,以及值是否有更改以移动新值。例如,在新数据库上更新原始数据库的密码更改。
有些表最多有 50 列,因此手动循环每个列将是一个非常长的宏,我想知道是否有更简单的方法来做到这一点?如果不是,我将如何循环它们?
这是我的一张表的宏:
Public Function update1()
'Open source database
Dim dSource As Database
Set dSource = CurrentDb
'Open dest database
Dim dDest As Database
Set dDest = DAO.OpenDatabase("C:\Users\simon\Documents\SellerDeck 2013\Sites\dest\ActinicCatalog.mdb")
'Open source recordset
Dim rSource As Recordset
Set rSource = dSource.OpenRecordset("Address", dbOpenForwardOnly)
'Open dest recordset
Dim rDest As Recordset
Set rDest = dDest.OpenRecordset("Address", dbOpenDynaset)
'Loop through source recordset
While Not rSource.EOF
'Look for record in dest recordset
rDest.FindFirst "nCustomerID = " & rSource.Fields("nCustomerID") & ""
'If not found, copy record
If rDest.NoMatch Then
rDest.AddNew
rDest.Fields("nCustomerID") = rSource.Fields("nCustomerID")
rDest.Fields("sName") = rSource.Fields("sName")
rDest.Fields("sLine2") = rSource.Fields("sLine2")
rDest.Fields("sLine4") = rSource.Fields("sLine4")
rDest.Fields("nCountryID") = rSource.Fields("nCountryID")
rDest.Fields("bValidInvoiceAddress") = rSource.Fields("bValidInvoiceAddress")
rDest.Fields("bValidDeliveryAddress") = rSource.Fields("bValidDeliveryAddress")
rDest.Fields("nStateID") = rSource.Fields("nStateID")
rDest.Fields("bExemptTax1") = rSource.Fields("bExemptTax1")
rDest.Fields("sExemptTax1Number") = rSource.Fields("sExemptTax1Number")
rDest.Fields("bExemptTax2") = rSource.Fields("bExemptTax2")
rDest.Fields("sExemptTax2Number") = rSource.Fields("sExemptTax2Number")
rDest.Fields("bPurge") = rSource.Fields("bPurge")
rDest.Fields("bChanged") = rSource.Fields("bChanged")
rDest.Fields("nID") = rSource.Fields("nID")
rDest.Fields("nTax1ID") = rSource.Fields("nTax1ID")
rDest.Fields("nTax2ID") = rSource.Fields("nTax2ID")
rDest.Fields("nResidential") = rSource.Fields("nResidential")
rDest.Fields("sCompanyName") = rSource.Fields("sCompanyName")
rDest.Fields("sLine1") = rSource.Fields("sLine1")
rDest.Fields("sLine3") = rSource.Fields("sLine3")
rDest.Fields("sPostalCode") = rSource.Fields("sPostalCode")
rDest.Fields("sEmailAddress") = rSource.Fields("sEmailAddress")
rDest.Fields("sFaxNumber") = rSource.Fields("sFaxNumber")
rDest.Fields("sFirstName") = rSource.Fields("sFirstName")
rDest.Fields("sFullName") = rSource.Fields("sFullName")
rDest.Fields("sLastName") = rSource.Fields("sLastName")
rDest.Fields("sMobileNumber") = rSource.Fields("sMobileNumber")
rDest.Fields("sSalutation") = rSource.Fields("sSalutation")
rDest.Fields("sTelephoneNumber") = rSource.Fields("sTelephoneNumber")
rDest.Fields("sTitle") = rSource.Fields("sTitle")
rDest.Update
End If
'Next source record
rSource.MoveNext
Wend
'Close dest recordset
rDest.Close
Set rDest = Nothing
'Close source recordset
rSource.Close
Set rSource = Nothing
'Close dest database
dDest.Close
Set dDest = Nothing
'Close source database
dSource.Close
Set dSource = Nothing
End Function