PDA

Click to See Complete Forum and Search --> : Deleting Selection From DataGrid


PJB
Aug 16th, 2000, 02:56 PM
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

davidrobin
Aug 17th, 2000, 06:17 AM
Do you want to delete the row from the datagrid AND record from the database or just the row from the datagrid?

PJB
Aug 17th, 2000, 09:58 AM
From the datagrid and the database

davidrobin
Aug 17th, 2000, 10:02 AM
This works for me. The rs is a ADODB.recordset I have declared with form wide scope.

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.

PJB
Aug 17th, 2000, 10:09 AM
I'll give that a shot

PJB
Aug 17th, 2000, 10:14 AM
when i put that code in it gave me a "object required" error on this line:
rs.Delete adAffectCurrent

davidrobin
Aug 17th, 2000, 10:15 AM
How have you set up your recordset? some sample code would help

PJB
Aug 17th, 2000, 10:24 AM
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!!!

davidrobin
Aug 17th, 2000, 11:05 AM
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:

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

PJB
Aug 17th, 2000, 11:19 AM
That worked like a charm!!!! Your assistance is greatly appreciated!!!


Pete Butler