Re: VB6 - Delete From DataGrid - Deletes the Wrong Record

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"jdoggz" <brownsugareve2003@xxxxxxxxx> wrote in message
news:1159370772.198244.322440@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,

I have a datagrid that is bound to a recordset in a SQL 2000 database.
I want the user to be able to highlight the record and press a command
(delete) button to delete the record.

Currently, when the command button is pressed, it just deletes the top
record, regardless of the record I have selected. I also noticed that
even if I just hit delete without selecting a record, it deletes
whatever record is at the top.

My code is below. Any help is greatly appreciated!!!

First, for single-row deletes, if you simply leave AllowDelete set to true
the DataGrid will handle all this for you.

If you want to do it yourself anyway, you'll need to keep or create a clone
of the recordset to which the grid is bound, set the bookmark property of
the clone to the bookmark property of the grid, and then act on the clone.

How to acquire a clone from the grid's datasource depends upon what you've
bound it to. ADODC exposes a recordset propertry, so if you're using that:

Dim rsClone As ADODB.Recordset
Set rsClone = ADODC1.recordset.Clone()

' alternatively, you could use this
'Dim obj As Object
'Set obj = DataGrid1.DataSource
'Set rsClone = obj.recordset.Clone()
' or if the grid is simply bound to a recordset...
'If TypeOf obj Is ADODB.Recordset Then Set rsClone = obj.Clone()

Once you have a clone, you can use the bookmark property of the grid to
position it to the same record that is selected in the grid:

' always trap errors for this assignment, because if the user is adding
' a new row using the grid, its bookmark will be invalid
rsClone.Bookmark = DataGrid1.Bookmark

' anything you do to the clone will be reflected in the grid
rsClone.Delete

' closing the clone does not close the original
rsClone.Close

A few other comments inline...

[snip]

Db.CursorLocation = adUseServer

DataGrid works best with a client-side cursor.

Db.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security
Info=False;Initial Catalog=ItemProcessing;Data Source=UF2KOBDSSQL1"
strSQL = "select * from b1accounts"
tempRecSet.Open strSQL, Db, adOpenDynamic, adLockOptimistic


DataGrid1.AllowDelete = True
tempRecSet.Delete
tempRecSet.UpdateBatch

UpdateBatch is of no consequence unless a batch cursor is being used.

tempRecSet.Requery
DataGrid1.AllowDelete = False
DataGrid1.Refresh

Using a clone of the recordset that's already open/bound is almost
infinitely more efficient.


-Mark



End If




End Sub



.



Relevant Pages

  • Re: Local dataset show parts in two grids
    ... How about using clone of original ADODataSet with properly assined filter ... for each grid. ... > other in a second DBGrid depending on the value in one of the fields. ...
    (borland.public.delphi.database.ado)
  • Re: View updated data in recordset
    ... I have a grid that has the datasource bind to a recordset. ... When I use the goRs add a new record, I can see the record immediated ... The best way I know of is to use a clone of the recordset that's bound to ... You can then use the grid's bookmark property to position the ...
    (microsoft.public.vb.controls)
  • Re: Local dataset show parts in two grids
    ... > How about using clone of original ADODataSet with properly assined filter ... > for each grid. ... And can I get it so that cursor movement within the ...
    (borland.public.delphi.database.ado)