I have successfully run the following error trapping code for duplicate entries,but am having one problem that I can't figure out. First, all of the code I have found in forums has been placed in the On_Error event of the form, but the only way I could get it to work was to put it in the AfterUpdate event of my text box. My form's purpose is to add training records for employees. They select a document (DocID) from a combo box and two additional fields (Revision(if applicable) and Description) auto-fill based on the selection. Is there any reason that I should be concerned that placing the code in the AfterUpdate event would cause future problems?
Second, I have the code deleting the entry (DocID) if it is a duplicate after the message box is displayed and setting focus back to the DocID field, but, if it is NOT a duplicate, it is leaving the entry, but focus is going back to the DocID field when I need it to go to the next field (DateCompleted). I have posted my code and would appreciate any help on this.
Thanks,
Toni
Private Sub DocID_AfterUpdate()
' Autofill these fields after selecting Document
Me.Revision = Me.DocID.Column(2)
Me.Description = Me.DocID.Column(3)
On Error GoTo Err_DocID_AfterUpdate
Me.Refresh
Exit_DocID_AfterUpdate:
Exit Sub
Err_DocID_AfterUpdate:
If Err.Number = 3022 Then 'Error #3022 = Duplicate entry
Forms![Add Records]!DocID.Undo
Forms![Add Records]!Revision.Undo
Forms![Add Records]!Description.Undo
MsgBox "This entry already exists in your training record." & vbCrLf & _
"Please enter a different training name.", vbOKOnly + vbCritical, "Duplicate Training Entry (Error #:" & Err.Number & ")"
Forms![Add Records]!Revision.SetFocus
Forms![Add Records]!DocID.SetFocus
DoCmd.CancelEvent
Else
Forms![Add Records]!DateCompleted.SetFocus
MsgBox Err.Description
Resume Exit_DocID_AfterUpdate
Me.DocID.Requery
End If
End Sub
Second, I have the code deleting the entry (DocID) if it is a duplicate after the message box is displayed and setting focus back to the DocID field, but, if it is NOT a duplicate, it is leaving the entry, but focus is going back to the DocID field when I need it to go to the next field (DateCompleted). I have posted my code and would appreciate any help on this.
Thanks,
Toni
Private Sub DocID_AfterUpdate()
' Autofill these fields after selecting Document
Me.Revision = Me.DocID.Column(2)
Me.Description = Me.DocID.Column(3)
On Error GoTo Err_DocID_AfterUpdate
Me.Refresh
Exit_DocID_AfterUpdate:
Exit Sub
Err_DocID_AfterUpdate:
If Err.Number = 3022 Then 'Error #3022 = Duplicate entry
Forms![Add Records]!DocID.Undo
Forms![Add Records]!Revision.Undo
Forms![Add Records]!Description.Undo
MsgBox "This entry already exists in your training record." & vbCrLf & _
"Please enter a different training name.", vbOKOnly + vbCritical, "Duplicate Training Entry (Error #:" & Err.Number & ")"
Forms![Add Records]!Revision.SetFocus
Forms![Add Records]!DocID.SetFocus
DoCmd.CancelEvent
Else
Forms![Add Records]!DateCompleted.SetFocus
MsgBox Err.Description
Resume Exit_DocID_AfterUpdate
Me.DocID.Requery
End If
End Sub