访问VBA - 输入表单检测重复,然后显示重复记录

时间:2017-03-10 04:35:55

标签: vba access-vba access

我有什么

我有一个Access输入表单,用户填写表单中的数据,按下一个触发VBA脚本的按钮,该脚本检查[Telephone]控件中的输入电话号码是否与表中的条目匹配。如果有重复,它会触发一个MsgBox,如果没有,它会将数据提交给表。这很有效,而且很棒。这是脚本:

Option Compare Database

Private Sub buttonNewRecord_Click()

Dim ErrorInt As Integer
Dim TeleCheck As Variant

TeleCheck = DLookup("[Telephone]", "tblLog", "[Telephone] = '" & Me.Telephone & "'")
If Not IsNull(TeleCheck) Then
MsgBox "Telephone number already exists in the database!"
ErrorInt = ErrorInt + 1
End If

If ErrorInt < 1 Then
DoCmd.GoToRecord , , acNewRec
MsgBox "Record Added!"
End If

End Sub

我正在尝试做什么

在表格中,与[电话]一起,有[日期]和[客户名称]字段。当检测到重复的电话号码时,我希望触发的MsgBox显示找到副本的记录的[Date]和[Customer_Name]。像:

MsgBox "Telephone number already exists for [Customer_Name] added on [Date]!"

我尝试过什么

不是很多,因为我不确定该尝试什么。谷歌搜索提供了许多不同的方法来检测重复,但我发现没有人试图从找到的副本的记录中返回任何数据。

1 个答案:

答案 0 :(得分:1)

Dim cstmerName as String
Dim dateAdded as String

Let cstmerName = DLookup("[Customer_Name]", "tblLog", "[Telephone] = '" & Me.Telephone & "'")
Let dateAdded = CStr(DLookup("[Date]", "tblLog", "[Telephone] = '" & Me.Telephone & "'"))

MsgBox "Telephone number already exists for " & cstmerName & " added on " & dateAdded & "!"