Re: VB6 - Delete From DataGrid - Deletes the Wrong Record
- From: "Mark J. McGinty" <mmcginty@xxxxxxxxxxxxxxx>
- Date: Thu, 28 Sep 2006 07:43:17 -0700
"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
.
- References:
- Prev by Date: Re: VB6 - Delete From DataGrid - Deletes the Wrong Record
- Previous by thread: Re: VB6 - Delete From DataGrid - Deletes the Wrong Record
- Next by thread: Recordset Fetchcomplete Event Question
- Index(es):
Relevant Pages
|