Hi,

My requirement is to delete a selected row in the datagrid from SQL server. If no row is selected then nothing should happen.

Below is my code

Private Sub BT_Remove_Click()
On Error GoTo ErDsc:
Dim MyConn As ADODB.Connection
Dim MyRecSet1 As New ADODB.Recordset
Set MyConn = New ADODB.Connection
MyConn.ConnectionString = "Provider=SQLOLEDB;Data Source=TGSSRV-08;Initial Catalog=RMG;User Id=RMG;Password=Thinksoft;Trusted_Connection=no"

MyConn.Open
    If AddProfile.DataGrid2.SelBookmarks.Count = 0 Then
        MsgBox ("Please select the profile that you want to delete"), vbOKOnly, "Alert"
        Exit Sub
    End If

But when the code is executed I am gettign error "RowSet not available"

Dont know the reason. Kindly help me.

have you opened the record set ?

No. I didn't select any row in the data grid and no record in the data grid. If you say the issue is no recordset in datagrid, then wats the solution if no recordset is there in datagrid and user trying to click the remove button.

Please post some sample code snippets

How you populate the grid ? is it not from recordset ?

Populating grid from recordset. But if user click remove button when the grid is empty then how to manage the error?

I presume you are populating from the object MyRecSet1. If you have populated this recordset why not just check the RecordCount property to see if there are any rows returned and disable the remove button if there are no rows.

Thanks Chris. Valid point. Will do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.