I have a popup form that allows users to search for an account and then add that account to the contract they are viewing. When they select an account number the code below runs which adds that account to a table called "tblContractAccounts". The error in question is triggered when the user selects an account that has already been added for a given contract, thus violating the table's unique primary key. I had added some error handling to try and trap this error and provide a more understandable error message to the user. I was expecting this scenario to trigger error number 3022, rather than the strange number I am seeing.
Code:
Private Sub AccountID_Click()
'get user confirmation
If MsgBox("Do you want to add the account " & Me.AccountName & " to this contract?", vbQuestion + vbOKCancel, "Add New Account") = vbCancel Then
Exit Sub
End If
'save the contract record if it is dirty
If Forms!frmContractDetail.Dirty Then Forms!frmContractDetail.Dirty = False
'set up and open the recordset
Dim ContractAccountsRS As New ADODB.Recordset
ContractAccountsRS.Open "tblContractAccounts", CurrentProject.Connection, adOpenDynamic, adLockOptimistic
'add account to the contract accounts table
With ContractAccountsRS
.AddNew
![ContractID] = Forms!frmContractDetail!ContractID
![AccountID] = Me.AccountID
.Update
End With
'clean up
ContractAccountsRS.Close
Set ContractAccountsRS = Nothing
'close search form and requery contract detail form
DoCmd.Close acForm, "frmSearchAccount", acSaveNo
Forms!frmContractDetail!frmContractAccountsDS.Requery
End Sub