VBA 在 Access 中 - 重复条目消息框

VBA in Access - Duplicate Entries Msg Box

任何人都可以帮我确定这段代码有什么问题吗?如果我将 table 中的 DOB 更改为短文本并将其 DIM 为字符串,则代码有效。但是,将 DOB 作为日期字段,将 DIM 作为日期,我收到此错误消息。

"运行-时间错误'3464' 条件表达式中的数据类型不匹配

当我点击调试时,下面加粗的行以黄色突出显示。

Private Sub DOB_AfterUpdate()
Dim DOB As Date
Dim FirstName As String
Dim LastName As String
Dim stLinkCriteria As String
Dim PIN As Integer

'Assign the entered customer name and  address  to a variable
NewJII = Me.FirstName.Value
NewJII2 = Me.LastName.Value
NewDOB = Me.DOB.Value
stLinkCriteria = "[FirstName] = " & "'" & NewJII & "' and [LastName] = " & "'" & NewJII2 & "' And [DOB] = " & "'" & NewDOB & "'"

**If Me.FirstName & Me.LastName & Me.DOB = DLookup("[FirstName]", 
"TblPersonLog", stLinkCriteria) Then**

  MsgBox "This Customer, " & stLinkCriteria & " has already been entered in database." _
            & vbCr & vbCr & "with DOB " & NewDOB & "" _
            & vbCr & vbCr & "Please check Customer name and Date Of Birth again.", vbInformation, "Duplicate information"
   Cancel = True
End If



End Sub

非常感谢任何指导!!几天来我一直在尝试解决这个问题。 VBA.

不是很强

首先,您不能那样使用 DLookup。其次,如果您想取消,请使用 BeforeUpdate 事件。第三,如 Tim 所述,对日期条件使用正确的语法。

因此,它可能是这样的:

Private Sub DOB_BeforeUpdate(Cancel As Integer)

    Dim FirstName       As String
    Dim LastName        As String
    Dim NewDOB          As Date
    Dim stLinkCriteria  As String

    If Not IsNull(Me!DOB.Value) Then
        ' Assign the entered customer name and DOB to variables.
        FirstName = Nz(Me!FirstName.Value)
        LastName Nz(Me!LastName.Value)
        NewDOB = Me!DOB.Value

        stLinkCriteria = "[FirstName] = '" & FirstName & "' And [LastName] = '" & LastName & "' And [DOB] = #" & Format(NewDOB, "yyyy\/mm\/dd") & "#"
        Cancel = Not IsNull(DLookup("[FirstName]", "TblPersonLog", stLinkCriteria))

        If Cancel = True Then
            MsgBox "This Customer, " & FirstName & " " & LastName & ", has already been entered in the database" _
                & vbCr & vbCr & "with DOB " & NewDOB & "." _
                & vbCr & vbCr & "Please check Customer name and Date Of Birth again.", vbInformation, "Duplicate information"
        End If
    End If 

End Sub