Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    3

    Question Unanswered: VB6: Deleting a record from a DataGrid

    Hello

    I'm currently working on a CD-Database application. I have a DataGrid control positioned on the form, and when the user selects a record (row) in the DataGrid and clicks on a delete-button, the record should be removed from the DataGrid and the database. I can't get this to work! I get the row and column numbers, but when I use the Delete method, it gives me an error. Here's the code I have right now:

    Dim intCol As Integer
    Dim intRow As Integer
    Dim intCounter As Integer
    intCol = dgdCd.Col 'find out in which column the selection is
    intRow = dgdCd.Row 'find out in which row the selection is

    Do While Not adoCd.Recordset.EOF
    If dgdCd.Row = intRow Then
    adoCd.Recordset.Delete adAffectCurrent
    End If
    frmMain.adoCd.Recordset.MoveNext 'move to the next record
    Loop


    The error I get is: "Run-Time Error. Key column information is insufficient or incorrect. Too many rows were affected by update."

    The things is, that after I get the error, close the program and then starts it up again, the records has been deleted - even though I got that error message. Another weird thing about it is that it deletes all records that have the same contents, and I can't see why that is happening...

    I'd be happier than happy if someone could give me some tips.

    Simon

  2. #2
    Join Date
    Jan 2004
    Posts
    184

    Re: VB6: Deleting a record from a DataGrid

    The reason for this error will vary depending on the data provider. In the case of SQL Server OLE DB Provider, this error will occur only because the base table does not have a Primary Key, a Unique Constraint or a Unique Index.

    With the Jet OLE DB Provider, this error will also occur because the base table does not have a Primary Key or because the Jet Provider cannot determine the Key column(s) (In Access the Primary key MUST be included in the Select statement).

    I also highly recommend the use of this control featured on download.com in your application:

    http://www.geocities.com/scirocco_ha/DataControl.htm

    I hope this helps, let me know what you think.

    Originally posted by pennywally
    Hello

    I'm currently working on a CD-Database application. I have a DataGrid control positioned on the form, and when the user selects a record (row) in the DataGrid and clicks on a delete-button, the record should be removed from the DataGrid and the database. I can't get this to work! I get the row and column numbers, but when I use the Delete method, it gives me an error. Here's the code I have right now:

    Dim intCol As Integer
    Dim intRow As Integer
    Dim intCounter As Integer
    intCol = dgdCd.Col 'find out in which column the selection is
    intRow = dgdCd.Row 'find out in which row the selection is

    Do While Not adoCd.Recordset.EOF
    If dgdCd.Row = intRow Then
    adoCd.Recordset.Delete adAffectCurrent
    End If
    frmMain.adoCd.Recordset.MoveNext 'move to the next record
    Loop


    The error I get is: "Run-Time Error. Key column information is insufficient or incorrect. Too many rows were affected by update."

    The things is, that after I get the error, close the program and then starts it up again, the records has been deleted - even though I got that error message. Another weird thing about it is that it deletes all records that have the same contents, and I can't see why that is happening...

    I'd be happier than happy if someone could give me some tips.

    Simon

  3. #3
    Join Date
    Feb 2004
    Posts
    3

    Re: VB6: Deleting a record from a DataGrid

    Thank you for your answer.

    I'm using the Jet OLE DB Provider, and it'd be nice of you to explain what you said a little bit more, since I'm not really a database-expert... Actually, this is my first project ever including a database

    So, what does this really mean?

    With the Jet OLE DB Provider, this error will also occur because the base table does not have a Primary Key or because the Jet Provider cannot determine the Key column(s) (In Access the Primary key MUST be included in the Select statement).
    Thanks again for your help!

  4. #4
    Join Date
    Jan 2004
    Posts
    184

    Re: VB6: Deleting a record from a DataGrid

    Every table should have a field that is a unique identifier. A field or group of fields that are unique to each record (i.e. Social Security Number is unique to every citizen of a country).

    In Access when you are in table design mode you select the field (or fields) that you want as your primary key, then you right-click and choose "Primary Key". I have attached a gif file that shows you this.

    The unique identifier will prevent more then one record from being deleted also.

    Again I highly recommend the control:

    http://www.geocities.com/scirocco_ha/DataControl.htm

    Originally posted by pennywally
    Thank you for your answer.

    I'm using the Jet OLE DB Provider, and it'd be nice of you to explain what you said a little bit more, since I'm not really a database-expert... Actually, this is my first project ever including a database

    So, what does this really mean?



    Thanks again for your help!
    Attached Thumbnails Attached Thumbnails untitled.gif  

  5. #5
    Join Date
    Feb 2004
    Posts
    3

    Re: VB6: Deleting a record from a DataGrid

    Ahh, I fixed the problem today, you were right, the primary key was screwed up. I had none. Oh well, I'm just a noob anyways. Thank you for your tips!

    Now I'm searching for info to fix my search function too; don't be shocked if you see a new thread in a few days about that

    Originally posted by rami.haddad
    Every table should have a field that is a unique identifier. A field or group of fields that are unique to each record (i.e. Social Security Number is unique to every citizen of a country).

    In Access when you are in table design mode you select the field (or fields) that you want as your primary key, then you right-click and choose "Primary Key". I have attached a gif file that shows you this.

    The unique identifier will prevent more then one record from being deleted also.

    Again I highly recommend the control:

    http://www.geocities.com/scirocco_ha/DataControl.htm

Posting Permissions

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