Results 1 to 10 of 10

Thread: Deleting Selection From DataGrid

  1. #1

    Thread Starter
    Hyperactive Member PJB's Avatar
    Join Date
    Aug 2000
    Location
    dunno at the moment
    Posts
    302

    Angry

    OK here's my silly problem for today:
    I want to delete a currently selected record from my DataGrid. In the DataGrid properties i initially have AllowDelete turned off. When the user clicks the a delete record command button i added i want a message box to pop up confirming the delete then allowing the delete to happen.
    I got as far as the message box but it never deletes anything???

    I'm beginning to think selling a database application before buying VB may have been a mistake.


    Pete Butler

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    Do you want to delete the row from the datagrid AND record from the database or just the row from the datagrid?


    Things I do when I am bored: DotNetable

  3. #3

    Thread Starter
    Hyperactive Member PJB's Avatar
    Join Date
    Aug 2000
    Location
    dunno at the moment
    Posts
    302
    From the datagrid and the database

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    This works for me. The rs is a ADODB.recordset I have declared with form wide scope.

    Code:
    Private Sub Command1_Click()
    Dim ret As Integer
    
        ret = MsgBox("Delete Current row?", vbYesNo, "Record Delete")
        If ret = vbYes Then
            rs.Delete adAffectCurrent
            DataGrid1.Refresh
        End If
    End Sub
    If the user clicks the 'NO' button everything is left as is.


    Things I do when I am bored: DotNetable

  5. #5

    Thread Starter
    Hyperactive Member PJB's Avatar
    Join Date
    Aug 2000
    Location
    dunno at the moment
    Posts
    302
    I'll give that a shot

  6. #6

    Thread Starter
    Hyperactive Member PJB's Avatar
    Join Date
    Aug 2000
    Location
    dunno at the moment
    Posts
    302
    when i put that code in it gave me a "object required" error on this line:
    rs.Delete adAffectCurrent

  7. #7
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    How have you set up your recordset? some sample code would help


    Things I do when I am bored: DotNetable

  8. #8

    Thread Starter
    Hyperactive Member PJB's Avatar
    Join Date
    Aug 2000
    Location
    dunno at the moment
    Posts
    302
    Not alot of code behind it, it's a datagrid with it's Datasource property set to DataEnvironment1 and it's DataMember property set to a table called Property(oddly enough)

    Connection1:
    Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=C:\LandLordManager\dbLandLord.mdb

    there is also a second table setup as a child command called Apartment

    I've only had VB 2 weeks, i should have tried that "Hello World" sample first!!!

  9. #9
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    This works for me and I will go through the process

    Right click on the DataEnvironment to add a connection.
    Right click the Connection1, select 'properties' as you have to set up the connection.
    Right click the Connection and add a Command (Mine is called Command1)
    Right click Command1, select 'properties' and Select the Database object radio button
    Select TAble from the Database object drop down list
    Select the table you want to use from the Object Name drop down list
    Click on the Advanced tab and ensure the lock type is not ReadOnly and the Recordset Returning check box is checked.
    Within the properties browser set the DataSource property to 'DataEnvironment1' and DataMember to 'Command1' (these should be the only options available from the drop down lists).


    Change the offending Delete line to that below:

    Code:
    DataEnvironment1.rsCommand1.Delete adAffectCurrent
    Note the rsCommand1. This is a result of checking the recordset returning check box. If it is not checked there will be no rows to display.
    With all this I have no problem deleting a row. To test it run the application, attempt to delete a row close down the application and then run the application again.

    Hope this all helps


    Things I do when I am bored: DotNetable

  10. #10

    Thread Starter
    Hyperactive Member PJB's Avatar
    Join Date
    Aug 2000
    Location
    dunno at the moment
    Posts
    302
    That worked like a charm!!!! Your assistance is greatly appreciated!!!


    Pete Butler

Posting Permissions

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



Click Here to Expand Forum to Full Width