有表1和nrb两个表,表1中有pID字段,nrb中有id字段。表1窗体的数据源是表1,为连续窗体。通过Shift键选择2条连续的窗体记录并按Del删除时,希望连带删除nrb表中的对应id的记录。所用代码如下:
Private Sub Form_Delete(Cancel As Integer)
Dim n2 As Long
n2 = Me.SelHeight '选取的记录条数作为For循环的终点
For I = 1 To n2
‘MsgBox "I: " & I & " n2: " & n2 & " pID: " & pID '监视循环参数及pID值的变化
DoCmd.RunSQL "DELETE nrb.* FROM nrb WHERE id = " & Me.pID
DoCmd.GoToRecord '使下一条记录成为当前
Next
End Sub
执行循环时,第一圈正确。见图一