Error trying to Add to record using ADO
I am trying to add a record to a SQL Server DB. I am using ADO. When I try adding the record, I get the following error message:
Code:
-2147217873: Violation of PRIMARY KEY constraint 'PK_tblClients'. Cannot insert duplicate key in object 'tblClients'.
I know the primary key is unique in this instance. I'm not sure what I'm doing wrong. I have attached the code below:
Code:
Private Sub client_IO()
On Error GoTo ErrorProc
If tblClients.State = 1 Then
tblClients.Close
End If
tblClients.Open "tblClients", oConn, adOpenStatic, adLockOptimistic
If tblClients.Supports(adAddNew) Then
With tblClients
.AddNew
!cliClientID = txtClientID.Text
!cliCommonName = txtCommon.Text
!cliEmail = txtEmail.Text
!cliAddr1 = txtAddr1.Text
!cliAddr2 = txtAddr2.Text
!cliCity = txtCity.Text
!cliState = cmbState1.Text
!cliZipCode = txtZip.Text
.Update
End With
End If
End Sub
Re: Error trying to Add to record using ADO
Are you sure you are not trying to add an alread existing clientID (or whichever is your primary key)
Re: Error trying to Add to record using ADO
It definetely sounds like you are adding a key that already exists. ;)
Re: Error trying to Add to record using ADO
use debug.print to see the record that will be added and then check with the database. it sure is the problem with the primary key.