我需要从多个外部来源将“联系人”导入数据库。
一些“联系人”可能已经存在,所以我只需要“新”数据。
我已经编写了一条更新记录代码,但是它将覆盖所有数据,因此会损坏表的完整性,因为旧数据可能包含一些有效值。
我尝试使用更新/追加查询,但这仅覆盖原始字段的值,而不是仅在旧值为NULL / FALSE的情况下才更新。这样做的问题是它将应用/删除配置文件标志,从而导致通信和数据使用(错误更新=可能违反GDPR)。
我无法使用SQL进行编程,我了解这些函数的工作方式以及它们的作用,但是不了解如何编译/按什么顺序排序(至今),因此暂时使用VBA。
Dim myR As Recordset
Dim myR2 As Recordset
Set myR = CurrentDb.OpenRecordset("Staging - Import", dbOpenDynaset)
Set myR2 = CurrentDb.OpenRecordset("Contacts", dbOpenDynaset)
Do Until myR.EOF = True
myR2.FindFirst ("Email = '" & myR![Email] & "'")
If myR2.NoMatch = True Then
myR2.AddNew
myR2![Email] = myR![Email]
myR2![First Name] = myR![First Name]
myR2![Last Name] = myR![Last Name]
myR2![Position] = myR![Position]
myR2![Company] = myR![Company]
myR2![Industry] = myR![Industry]
myR2![Size] = myR![Size]
myR2![Website] = myR![Website]
myR2![Location] = myR![Location]
myR2![Office Number] = myR![Office Number]
myR2![Mobile Number] = myR![Mobile Number]
myR2![Source] = myR![Source]
myR2![CFO-DEL] = myR![CFO-DEL]
myR2![CFO-SPON] = myR![CFO-SPON]
myR2![DP-DEL] = myR![DP-DEL]
myR2![DP-SPON] = myR![DP-SPON]
myR2![HR-DEL] = myR![HR-DEL]
myR2![HR-SPON] = myR![HR-SPON]
myR2![CIO-DEL] = myR![CIO-DEL]
myR2![CIO-SPON] = myR![CIO-SPON]
myR2![CMO-DEL] = myR![CMO-DEL]
myR2![CMO-SPON] = myR![CMO-SPON]
myR2![CISO-DEL] = myR![CISO-DEL]
myR2![CISO-SPON] = myR![CISO-SPON]
myR2![NIS] = myR![NIS]
myR2![Supress] = myR![Surpress]
myR2.Update
Else
myR2.Edit
myR2![First Name] = myR![First Name]
myR2![Last Name] = myR![Last Name]
myR2![Position] = myR![Position]
myR2![Company] = myR![Company]
myR2![Industry] = myR![Industry]
myR2![Size] = myR![Size]
myR2![Website] = myR![Website]
myR2![Location] = myR![Location]
myR2![Office Number] = myR![Office Number]
myR2![Mobile Number] = myR![Mobile Number]
myR2![Source] = myR![Source]
myR2![CFO-DEL] = myR![CFO-DEL]
myR2![CFO-SPON] = myR![CFO-SPON]
myR2![DP-DEL] = myR![DP-DEL]
myR2![DP-SPON] = myR![DP-SPON]
myR2![HR-DEL] = myR![HR-DEL]
myR2![HR-SPON] = myR![HR-SPON]
myR2![CIO-DEL] = myR![CIO-DEL]
myR2![CIO-SPON] = myR![CIO-SPON]
myR2![CMO-DEL] = myR![CMO-DEL]
myR2![CMO-SPON] = myR![CMO-SPON]
myR2![CISO-DEL] = myR![CISO-DEL]
myR2![CISO-SPON] = myR![CISO-SPON]
myR2![NIS] = myR![NIS]
myR2![Supress] = myR![Surpress]
myR2.Update
End If
myR.MoveNext
Loop
Set myR = Nothing
End Sub
是否有更简单的方式编写此代码,或者我应该使用代码
myR2.FindFirst ("Email = '" & myR![Email] & "'")
If myR2.NoMatch = True Then
对于每个值,有效地创建15-20个子对象和一个宏来一起运行?
我尝试了几种代码变体,尝试包含elseIf,isNull()和isFalse(),但是它们始终无法编译或没有完成更新/更改了记录。
我需要执行以下操作的代码:
检查联系人是否在联系人表中
如果联系人不存在,请添加所有数据
如果联系人确实存在,则添加新数据或将“是/否”字段从“否”更新为“是”
注意:当前“联系人”表为空,因为我们需要在将数据导入到“联系人”表中之前创建新的/合并重复项。
因此“联系人”当前为:
Email Name Surname
- - -
- - -
- - -
- - -
登台-当前正在导入:
Email Name Surname
b@b.c Brad
t@b.c Tony Tiger
b@b.c B Pitt
r@b.c Ryan Reynolds
完成后,联系人应如下所示:
Email Name Surname
t@b.c Tony Tiger
b@b.c Brad Pitt
r@b.c Ryan Reynolds
答案 0 :(得分:2)
在比较字符串数据时确定要更新或添加的内容可能非常复杂,并且通常需要逐案审查。对于从一个记录中选择“ Brad”而从另一个记录中选择“ Pitt”的程序决定,应该应用什么规则?如果同一封电子邮件的数据是Brad Pitt
和Bradley Pitt
,该怎么办?哪个正确,应该保存?可能必须做一个查询,以在“登台”中查找重复的电子邮件,并根据情况决定要修复/删除这些重复的邮件的情况。然后插入“联系人”。插入代码可以测试每个字段的内容是否为Null或False,并确定是否接受新值。
对于非是/否字段,请使用Nz()函数(假设文本字段不会包含空字符串)
myR2![First Name] = Nz(myR2![First Name], myR![First Name])
或(以处理可能的空字符串)
If myR2![First Name] & "" = "" Then myR2![First Name] = myR![First Name]
(建议不要在表设计中在文本字段中使用空字符串,在数字字段中使用零默认值)。
对于是/否字段,测试False(在表设计中不要设置DefaultValue属性):
myR2![Supress] = IIf(myR2![Supress] = False, myR![Supress], True)
或
If myR2![Supress] = False Then myR2![Supress] = myR![Supress]
导入程序的短代码。用上面的内容进行修改。
Do Until myR.EOF = True
myR2.FindFirst ("Email = '" & myR![Email] & "'")
If myR2.NoMatch = True Then
myR2.AddNew
myR2![Email] = myR![Email]
Else
myR2.Edit
End If
myR2![First Name] = myR![First Name]
myR2![Last Name] = myR![Last Name]
myR2![Position] = myR![Position]
myR2![Company] = myR![Company]
myR2![Industry] = myR![Industry]
myR2![Size] = myR![Size]
myR2![WebSite] = myR![WebSite]
myR2![Location] = myR![Location]
myR2![Office Number] = myR![Office Number]
myR2![Mobile Number] = myR![Mobile Number]
myR2![Source] = myR![Source]
myR2![CFO-DEL] = myR![CFO-DEL]
myR2![CFO-SPON] = myR![CFO-SPON]
myR2![DP-DEL] = myR![DP-DEL]
myR2![DP-SPON] = myR![DP-SPON]
myR2![HR-DEL] = myR![HR-DEL]
myR2![HR-SPON] = myR![HR-SPON]
myR2![CIO-DEL] = myR![CIO-DEL]
myR2![CIO-SPON] = myR![CIO-SPON]
myR2![CMO-DEL] = myR![CMO-DEL]
myR2![CMO-SPON] = myR![CMO-SPON]
myR2![CISO-DEL] = myR![CISO-DEL]
myR2![CISO-SPON] = myR![CISO-SPON]
myR2![NIS] = myR![NIS]
myR2![Supress] = myR![Supress]
myR2.Update
myR.MoveNext
Loop
另一个,假设记录集具有完全相同的字段。
Dim myR As DAO.Recordset
Dim myR2 As DAO.Recordset
Dim fld As DAO.Field
Set myR = CurrentDb.OpenRecordset("Staging - Import", dbOpenDynaset)
Set myR2 = CurrentDb.OpenRecordset("Contacts", dbOpenDynaset)
Do Until myR.EOF = True
myR2.FindFirst "Email = '" & myR![Email] & "'"
If myR2.NoMatch = True Then
myR2.AddNew
myR2![Email] = myR![Email]
Else
myR2.Edit
End If
For Each fld In myR.Fields
If fld.Name <> "Email" And _
(myR2.Fields(fld.Name) & "" = "" Or myR2.Fields(fld.Name) = False) Then
myR2.Fields(fld.Name) = fld
End If
Next
myR2.Update
myR.MoveNext
Loop