0
我有以下VBA代码附加到我的主窗体上的“取消”按钮的点击事件。其目的是删除'entities_subform'中的所有记录,然后删除主窗体中的相关记录。当试图从Access窗体中删除当前记录时,为什么会出现运行时错误3021(无当前记录)?
但是,虽然子窗体中的实体被成功删除,但由于运行时错误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
有趣的是,记录正在从实体表中删除,即使我只操作记录集的克隆。我会研究参考完整性,因为我以前没有使用过。目前,我已经能够实现我需要的功能,使用SQL查询从主窗体删除记录后编辑实体表。 – DrewCraven