Sadie Hewgill
Registered User.
- Local time
- Today, 16:55
- Joined
- Oct 18, 2010
- Messages
- 52
error message for duplicate entry
I have two primary keys and when duplicates are entered in the form, it is not allowed. I would like to display a custom error message that gives the user the option to go back and change the information they entered, or to be taken to the form with the duplicate information. So far my code is doing half of this. The Cancel option works fine, but when I press OK to be taken to the duplicate record, I am just taken back to the first record. Here's my code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Dim strWhere As String
Dim rst As DAO.Recordset
On Error GoTo HandleErrors
Select Case DataErr
Case 3022 'Duplicate record
If MsgBox("This record already exists. Click OK to go to the existing record, or Cancel to change your entry", vbOKCancel, "Duplicate record ") = vbOK Then
strWhere = "MfrPartNumbers_MfrPartNumber ='" & Me.MfrPartNumbers_MfrPartNumber & "' AND MfrPartNumbers_Mfr" = " & MfrPartNumbers_Mfr & "
Me.Undo
Set rst = Me.RecordsetClone
With rst
.FindFirst strWhere
Me.Bookmark = rst.Bookmark
.Close
End With
Response = acDataErrContinue
Else
Response = acDataErrContinue
End If
Case Else
End Select
ExitHere:
Exit Sub
HandleErrors:
Select Case Err.Number
Case Else
MsgBox Err.Description & " (" & Err.Number & ")"
End Select
Resume ExitHere
End Sub
Thank you,
Sadie
I have two primary keys and when duplicates are entered in the form, it is not allowed. I would like to display a custom error message that gives the user the option to go back and change the information they entered, or to be taken to the form with the duplicate information. So far my code is doing half of this. The Cancel option works fine, but when I press OK to be taken to the duplicate record, I am just taken back to the first record. Here's my code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Dim strWhere As String
Dim rst As DAO.Recordset
On Error GoTo HandleErrors
Select Case DataErr
Case 3022 'Duplicate record
If MsgBox("This record already exists. Click OK to go to the existing record, or Cancel to change your entry", vbOKCancel, "Duplicate record ") = vbOK Then
strWhere = "MfrPartNumbers_MfrPartNumber ='" & Me.MfrPartNumbers_MfrPartNumber & "' AND MfrPartNumbers_Mfr" = " & MfrPartNumbers_Mfr & "
Me.Undo
Set rst = Me.RecordsetClone
With rst
.FindFirst strWhere
Me.Bookmark = rst.Bookmark
.Close
End With
Response = acDataErrContinue
Else
Response = acDataErrContinue
End If
Case Else
End Select
ExitHere:
Exit Sub
HandleErrors:
Select Case Err.Number
Case Else
MsgBox Err.Description & " (" & Err.Number & ")"
End Select
Resume ExitHere
End Sub
Thank you,
Sadie
Last edited: