'Access: Navigate to Existing Record to Avoid Duplicate - Error 2169
I have a pop-up form where records are uniquely identified by a combination of fields. When a user is about to create a duplicate record, I want to prompt them to either navigate to the existing record or choose a different combination of fields. My two attempts are shown below, but both result in Error 2169.
Attempt 1:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim answer As String, strConditions As String
Dim rsc As Recordset
Dim db As DAO.Database
strConditions = "Fund='" & Fund.Value & "' AND Department='" & _
Department.Value & "' AND Object='" & Object.Value & "' AND Subcode='" & Subcode.Value & _
"' AND TrackingCode='" & TrackingCode.Value & "' AND Reserve='" & Reserve.Value & _
"' AND FYEnd=" & FYEnd.Value & " AND MonthlySpreadID<>" & MonthlySpreadID.Value
If DCount("*", "[Monthly Spread]", strConditions) = 0 Then
Exit Sub
End If
answer = MsgBox("An entry with this coding already exist." & vbCrLf & vbCrLf & _
"Would you like to be navigated to the existing record?", vbQuestion + vbYesNo, "Duplicate Record.")
If answer = vbYes Then
Me.Undo
Cancel = True
Me.Filter = ""
Set rsc = Me.RecordsetClone
rsc.FindFirst strConditions
Me.Bookmark = rsc.Bookmark
Set rsc = Nothing
Else
Cancel = True
MsgBox "Duplicates are not allowed. Please enter a different combination of dimensions.", vbInformation
End If
End Sub
Attempt #2
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Dim answer As String, strConditions As String
Dim rsc As Recordset
Select Case DataErr
Case 3022
Response = acDataErrContinue 'prevent Access from displaying its own error message
strConditions = "Fund='" & Fund.Value & "' AND Department='" & _
Department.Value & "' AND Object='" & Object.Value & "' AND Subcode='" & Subcode.Value & _
"' AND TrackingCode='" & TrackingCode.Value & "' AND Reserve='" & Reserve.Value & _
"' AND FYEnd=" & FYEnd.Value
answer = MsgBox("An entry with this coding already exist." & vbCrLf & vbCrLf & _
"Would you like to be navigated to the existing record?", vbQuestion + vbYesNo, "Duplicate Record.")
If answer = vbYes Then
Me.Undo
Me.Filter = ""
Set rsc = Me.RecordsetClone
rsc.FindFirst strConditions
Me.Bookmark = rsc.Bookmark
Set rsc = Nothing
Else
Me.Undo
Me.Refresh
MsgBox "Duplicates are not allowed. Please enter a different combination of dimensions.", vbInformation
End If
Case 2169
Response = acDataErrContinue
'How do I choose No from error message?
Case Else
Response = acDataErrDisplay 'cause Access to display its own error message
End Select
End Sub
The problem with both attempts is that after I navigate to the duplicate record, I get error 2169: 
I need to either avoid this error or select "No" using VBA. What am I missing here?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
