各位大侠帮帮忙,请问我现在已经建立两个表,access,两个表的主键都是ID,我想达到这样的效果:
1、选中ID=3的DataGrid1中某一行,另外一个表中(用DataGrid2来显示)也跟这自动移到ID=3的记录,语句要怎么写呢?
2、我要把DataGrid1中ID=3的记录删除,DataGrid2中的ID=3的记录也随之删除,这又怎么写呢?
我试了好几遍,都出错了,所以求助好心人帮帮忙o(∩_∩)o...

解决方案 »

  1.   

    1.select A.*,B.* From Table1 A Lefe Join Table2 B On A.ID=B.ID where A.ID="3"
    2.Delete From Table1 A Left Join Table2 B On A.ID=B.ID Where A.ID="3"
      

  2.   

    做法應該是選中DataGrid1行,找出ID,查找出另一個表的ID,將DataGrid2設為當前行,或是可以查找顯示出來。刪除記錄只能一條條刪了,相同的ID好,兩個刪除語句就行了。
      

  3.   

    假定 DataGrid1 绑定 rs1,DataGrid2 绑定 rs2.1
    Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
        rs2.MoveFirst
        rs2.Find "ID=" & rs1!ID
    End Sub2
    如果你是在界面上用键盘删除 DataGrid1 的记录,就有些麻烦。需要给 rs1 添加事件:Dim WithEvents rs1 As ADODB.RecordsetPrivate Sub rs1_RecordsetChangeComplete(ByVal adReason As ADODB.EventReasonEnum, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
        cn.Execute "DELETE FROM table2 WHERE id NOT IN(SELECT id FROM table1)"
        rs2.requery
        rs2.Find "ID=" & rs1!ID
    End Sub