Results 1 to 4 of 4

Thread: Error trying to Add to record using ADO

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    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

  2. #2
    Addicted Member
    Join Date
    Aug 2004
    Posts
    176

    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)

  3. #3
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176

    Re: Error trying to Add to record using ADO

    It definetely sounds like you are adding a key that already exists.
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  4. #4
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width