【发布时间】:2017-09-08 22:07:51
【问题描述】:
我在主窗体上的“取消”按钮的单击事件中附加了以下 VBA 代码。目的是先删除“entities_subform”中的所有记录,然后再删除主表单中的相关记录。
但是,虽然子表单中的实体已成功删除,但由于运行时错误 3021(无当前记录),主表单记录并未删除。
我需要做什么才能有效地将焦点重新设置在主窗体上以使其正常工作?在我添加代码以从实体子表单中删除记录之前,命令acCmdDeleteRecord 在主表单中运行良好。我已经尝试在下面的acCmdDeleteRecord 之前插入行Me.SetFocus,但这没有任何区别。
Private Sub Cancel_New_Record_Click()
If MsgBox("Are you sure you want to delete this record?", vbYesNo) = vbYes Then
'first we need to delete all the entities in the subform, to prevent orphans being left behind
entities_subform.SetFocus
Dim entityRecSet As Recordset
Set entityRecSet = entities_subform.Form.Recordset.Clone()
entityRecSet.Delete
entityRecSet.Close
Set entityRecSet = Nothing
'now we can delete the check record
DoCmd.RunCommand acCmdDeleteRecord
DoCmd.Close acForm, "checks"
DoCmd.OpenForm "menu"
End If
End Sub
编辑:现在我已经能够通过使用以下来实现我正在寻找的功能:
Private Sub Cancel_New_Record_Click()
'--------------------------------------------------
'deletes the newly created record from the database
'--------------------------------------------------
If MsgBox("Are you sure you want to delete this record?", vbYesNo) = vbYes Then
'grab the id of the current check record for later
Dim checkID As Integer
checkID = Me.Check_ID
'delete the current check record
DoCmd.RunCommand acCmdDeleteRecord
'now delete any orphan entities from the entity table
CurrentDb.Execute "DELETE * FROM entities WHERE entities.[Check ID] = " & checkID & ";"
'close the form and return to the menu
DoCmd.Close acForm, "checks"
DoCmd.OpenForm "menu"
End If
End Sub
【问题讨论】: