在我看来,如果没有在子表单中输入数据,您希望删除主表单中的记录。我假设子表单中的数据绑定到不同的表,并用于绑定到 IDOrderNumber 的多个记录
我建议使用下面的 vba 代码。我假设你的一些数据库结构,所以根据需要更正我
此代码将进入您的 MainFormsForm_Close()
事件
Private Sub Form_Close()
Dim recordexists as Integer
Dim db as Database
Dim rs as Recordset
Dim strSQL as String
strSQL = "SELECT * FROM YourSecondaryTable T2 WHERE T2.IDOrderNumber =" & Me.IDOrderNumberField.Value
Set db = CurrentDb()
Set rs = db.OpenRecordset(strSQL)
'This recordset is checking to see if a value in the secondary table exists with the OrderNumber that exists in the main table.
With rs
If Not .BOF And Not .EOF Then
.MoveFirst
While (Not .EOF)
'If the code gets to this point, then a record exists in your subform.
'So we set the value to 1
recordexists = 1
Wend
End If
.Close
End With
'If the above recordset didnt find anything, the the recordexists value would never be set.
'Therefore you want to delete the parent record. So we delete it
If recordexists = 0 Then
DoCmd.SetWarnings False
DoCmd.RunSQL "DELETE TOP 1 FROM YourMainTable T1 WHERE T1.IDOrderNumber = " & Me.IDOrderNumberField.Value
DoCmd.SetWarnings True
End IF
Set rs = Nothing
Set db = Nothing
End Sub