Okay,

so here's my problem.
I have a MS Hierarchical Flex Gird Control [fgdCompany] which is used to display records that are taken from a database. Everything works great but not the Delete button. What the delete button is supposed to do is when a record is selected in fgdCompany, it is supposed to delete that current record. But the problem is, it always deletes the first record in the fgdCompany. Not the record that is selected in fgdCompany. What I am doing wrong? Please help me debug this error!!!!

Code used by me for the Delete button is given below :

  Dim rsCompany As New ADODB.Recordset
  Dim cn As New ADODB.Connection
  Dim strSQL As String

  cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                        "Data Source=" & App.Path & "\Sample.mdb;" & _
                        "Persist Security Info:False"

  cn.Open
  strSQL = "SELECT [Company].* FROM [Company]"
  rsCompany.Open strSQL, cn, adOpenStatic, adLockPessimistic

  If rsCompany.RecordCount > 0 Then
    msg = MsgBox("Delete Record?", vbYesNo)
    If msg = vbYes Then
      rsCompany.Delete
      rsCompany.MovePrevious
      If rsCompany.EOF Then
        rsCompany.MovePrevious
      ElseIf rsCompany.BOF Then
        rsCompany.MoveNext
      End If
      If rsCompany.EOF And rsCompany.BOF Then
        txtName = ""
        txtAddress = ""
        txtTelephoneNo = ""
        txtVehiclesReg = ""
        Exit Sub
      End If
    End If
    If rsCompany.Fields("Company Name") <> "" Then
      txtName = ""
    End If
    If rsCompany.Fields("Address") <> "" Then
      txtAddress = ""
    End If
    If rsCompany.Fields("Telephone Numbers") <> "" Then
      txtTelephoneNo = ""
    End If
    If rsCompany.Fields("Vehicles Registered") <> "" Then
      txtVehiclesReg = ""
    End If
  ElseIf rsCompany.RecordCount = 0 Then
    txtName = ""
    txtAddress = ""
    txtTelephoneNo = ""
    txtVehiclesReg = ""
    MsgBox "No records to be deleted."
  End If

  Set fgdCompany.DataSource = rsCompany

  If rsCompany.RecordCount <> 0 Then
    rsCompany.Update
  End If

Hi,

Your Code always delees first record.. as you have not used a "Where" condition in your recordset... Instead of that, run the "delete" command, and refresh the grid..

something like this:

strSQL = "Delete From Company Where [Company Name] = '" _
    & fgdCompany.TextMatrix(fgdCompany.Row, 1) & "'"
Cn.Execute strSQL
strSQL = "Select * From Company"
rsCompany.Open strSQL, cn, adOpenStatic, adLockPessimistic
Set fgdCompany.DataSource = rsCompany

Note: Assuming Company Name is displayed in Column 1 of flex grid... change accordingly

Regards
Veena

Comments
OMG!!! thank you sooo much! It works. Jsut have to tweak the rest of the code.... thnx a lot!! =P

Hi,

Your Code always delees first record.. as you have not used a "Where" condition in your recordset... Instead of that, run the "delete" command, and refresh the grid..

something like this:

>     strSQL = "Delete From Company Where [Company Name] = '" _
>     & fgdCompany.TextMatrix(fgdCompany.Row, 1) & "'"
>     Cn.Execute strSQL
>     strSQL = "Select * From Company"
>     rsCompany.Open strSQL, cn, adOpenStatic, adLockPessimistic
>     Set fgdCompany.DataSource = rsCompany

Note: Assuming Company Name is displayed in Column 1 of flex grid... change accordingly

Regards
Veena

**OMG!!! thank you sooo much! It works. Jsut have to tweak the rest of the code.... After two weeks of debugging.... finally! thnx a lot!! =P **

@QVeen72 OMG!!! thank you sooo much! It works. Jsut have to tweak the rest of the code.... After two weeks of debugging.... finally! thnx a lot!! =P

This question has already been answered. Start a new discussion instead.