防止条件重复数据

问题描述 投票:0回答:1

我想在用于输入序列号的文本框中设置数据重复检查。

如果在数据库中已经找到输入的序列号,则应在清除文本框中的值之前调用MsgBox来警告用户。

但是,如果输入的序列号包含“ RW”,则应禁用该检查。

Private Sub Serial_Number_AfterUpdate()
Dim NewSerialNumber As String
Dim stLinkCriteria As String

NewSerialNumber = Me.Serial_Number.Value
stLinkCriteria = "[Serial_Number] = " & "'" & NewSerialNumber & "'"
If Me.Serial_Number = DLookup("[Serial_Number]", "Esagon_End", stLinkCriteria) Then
    MsgBox "This serial number, " & NewSerialNumber & ", has already been entered into the database." _
            & vbCr & vbCr & "Please check the serial number again.", vbI, "Duplicate information"
    Me.Undo
End If

End Sub

如果使用VBA无法做到这一点,我可以接受其他方法,例如查询。谢谢。

access-vba
1个回答
0
投票

看着您的要求,我认为这就是您想要的。如果不是,请发表评论,我将尝试更新我的答案。

Private Sub Serial_Number_AfterUpdate()

'If it doesn't contain "RW"
If InStr(Me.Serial_Number, "RW") = 0 Then
    'If serial number not in the database
    If DCount("*", "Esagon_End", "Serial_Number = '" & Me.Serial_Number & "'") > 0 Then
        'Alert user and blank the text box
        Call MsgBox("The serial number " & Me.Serial_Number & " is already in the database." _
                    & vbCrLf & vbCrLf & "Please check the serial number you are entering.", _
                    vbInformation, "Duplicate Serial")
        Me.Serial_Number = ""
    End If
End If

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.