Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using vb.net framework 4 and access 2003 db. i was wondering you be able assist or recommend the best method to a achieve the following:

In the Access MDB database i have two tables TBLPerson & TBLHistory.
TBLPerson has 3 fields(personID(PK), Name, Age) and TBLHistory has 4 fields (ID(PK), PersonID, Name, Age).

i have 1 form in vb.net with datagridview with bound source to TBLPerson. What i want to be able to do is when the user selects a row in the datagrid and is able to delete the record in TBLPerson and delete all records in TBLHistory corrsponding to PERSONID Field.

So far i have been able to successfully delete the person from TBLPerson but bit unsure how to purge all records in TBLhistory.

Any help or assist in regards to this would be much appericated.

Thanks
Posted

try as
SQL
DELETE TableB.*, TableA.*
FROM TableA 
INNER JOIN TableB ON TableA.PID = TableB.PID
WHERE TableB.PID="123";
 
Share this answer
 
To delete the rows using ADO.NET the following code can be used
VB
'At entry level say in Load event of Form set the DataRelation
    'DataRelation can alos be created in the DataSet designer
        Dim parentColumn As DataColumn = _
        DataSet1.Tables("TBLPerson").Columns("PersonID")
    Dim childColumn As DataColumn = DataSet1.Tables( _
        "TBLHistory").Columns("PersonID")

    ' Create a DataRelation between the tables
    Dim relPersonHistory As DataRelation
    relPersonHistory = New DataRelation( _
        "PersonHistory", parentColumn, childColumn)

    ' Add relation of tables to the DataSet
    DataSet1.Relations.Add(relPersonHistory)

    'To delete the rows
    For Each row As DataGridViewRow In dataGridView1.SelectedRows
        Dim personRow As DataRow = TryCast(row.DataBoundItem, DataRow)
        If personRow IsNot Nothing Then
            dim historyRows as DataRow() = personRow.GetChildRows(relPersonHistory)
          ' Delete child rows
          For i = 0 To historyRows.Length-1
             historyRows(i).Delete()
          Next i
          personRow.Delete()
        End If
    Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900