Access 2013 防止保存时出现重复记录
Access 2013 Prevent Duplicate Record on Save
所以我有一个像这样的报名表,New Visitor Form
如果访问者已经在table,我试图阻止它保存记录。这是我尝试过的,但运气不佳。它将匹配来自不同的记录。我愿意接受任何建议。
Dim FName As String
Dim LName As String
Dim CName As String
Dim stLinkCriteriaFN As String
Dim stLinkCriteriaLN As String
Dim stLinkCriteriaCN As String
If IsNull(Me.tbFirstName.Value) Or IsNull(Me.tbLastName.Value) Or IsNull(Me.cbCompany.Value) Then
MsgBox "Not all information has been entered, visitor was not added.", vbOKOnly, "Data Entry Error"
Else
LName = Me.tbLastName.Value
FName = Me.tbFirstName.Value
CName = Me.cbCompany.Value
stLinkCriteriaLN = "[LastName] = " & "'" & LName & "'"
stLinkCriteriaFN = "[FirstName] = " & "'" & FName & "'"
stLinkCriteriaCN = "[Company] = " & "'" & CName & "'"
If Me.tbLastName = DLookup("[LastName]", "VisitorInfo", stLinkCriteriaLN) And Me.tbFirstName = DLookup("[FirstName]", "VisitorInfo", stLinkCriteriaFN) And Me.cbCompany = DLookup("[Company]", "VisitorInfo", stLinkCriteriaCN) Then
MsgBox "Visitor Already Added", vbOKOnly
Else
Dim f As Form
DoCmd.RunCommand acCmdSaveRecord
For Each f In Access.Forms
f.Requery
Next
DoCmd.RunCommand acCmdClose
End If
End If
假设您在 VisitorInfo table 中有某种主键,您可以组合条件并检查 DLookup 结果,如下所示:
If IsNull(DLookup("[VisitorID]", "VisitorInfo", "[LastName] = '" & LName & "' AND [FirstName] = '" & FName & "' AND [Company] = '" & CName & "'") Then
Dim f As Form
DoCmd.RunCommand acCmdSaveRecord
For Each f In Access.Forms
f.Requery
Next
DoCmd.RunCommand acCmdClose
Else
MsgBox "Visitor Already Added", vbOKOnly
End If
所以我有一个像这样的报名表,New Visitor Form
如果访问者已经在table,我试图阻止它保存记录。这是我尝试过的,但运气不佳。它将匹配来自不同的记录。我愿意接受任何建议。
Dim FName As String
Dim LName As String
Dim CName As String
Dim stLinkCriteriaFN As String
Dim stLinkCriteriaLN As String
Dim stLinkCriteriaCN As String
If IsNull(Me.tbFirstName.Value) Or IsNull(Me.tbLastName.Value) Or IsNull(Me.cbCompany.Value) Then
MsgBox "Not all information has been entered, visitor was not added.", vbOKOnly, "Data Entry Error"
Else
LName = Me.tbLastName.Value
FName = Me.tbFirstName.Value
CName = Me.cbCompany.Value
stLinkCriteriaLN = "[LastName] = " & "'" & LName & "'"
stLinkCriteriaFN = "[FirstName] = " & "'" & FName & "'"
stLinkCriteriaCN = "[Company] = " & "'" & CName & "'"
If Me.tbLastName = DLookup("[LastName]", "VisitorInfo", stLinkCriteriaLN) And Me.tbFirstName = DLookup("[FirstName]", "VisitorInfo", stLinkCriteriaFN) And Me.cbCompany = DLookup("[Company]", "VisitorInfo", stLinkCriteriaCN) Then
MsgBox "Visitor Already Added", vbOKOnly
Else
Dim f As Form
DoCmd.RunCommand acCmdSaveRecord
For Each f In Access.Forms
f.Requery
Next
DoCmd.RunCommand acCmdClose
End If
End If
假设您在 VisitorInfo table 中有某种主键,您可以组合条件并检查 DLookup 结果,如下所示:
If IsNull(DLookup("[VisitorID]", "VisitorInfo", "[LastName] = '" & LName & "' AND [FirstName] = '" & FName & "' AND [Company] = '" & CName & "'") Then
Dim f As Form
DoCmd.RunCommand acCmdSaveRecord
For Each f In Access.Forms
f.Requery
Next
DoCmd.RunCommand acCmdClose
Else
MsgBox "Visitor Already Added", vbOKOnly
End If