事务中不支持此操作

时间:2018-01-19 01:25:19

标签: vba ms-access access-vba

我的微软访问应用程序中有两个非常紧密相关的子表单。这两种形式称为阶段和场景。每个表单从相应的阶段和方案表中获取其记录。数据库中的每个阶段都有许多场景。

因此,在Phases表单的表单当前事件中,我需要获取其唯一的PhaseID来过滤Scenarios表单。这一切都很有效,除非删除一个阶段。删除阶段后,所有关联的方案也会被删除。删除后,访问将自动选择一个新阶段,当我尝试将过滤器应用于Scenarios表单时,我收到此错误:

  

运行时错误' 2074':   事务中不支持此操作。

这是我的代码:

相电流事件

Private Sub Form_Current()
   Dim filtStr As String
   Dim frm As Form
   Set frm = Forms!Main
   filtStr = "PhaseID=" & Me!PhaseID
   frm.Scenarios.Form.Filter = filtStr <--ERROR HERE
   frm.Scenarios.Form.FilterOn = True
End Sub

阶段删除事件

Private Sub Form_Delete(Cancel As Integer)
  Dim PhaseID As Long
  Dim Response As Long
  Dim style As Long
  PhaseID = Forms!Main!Phases!PhaseID

  style = vbYesNo + vbQuestion
  Response = MsgBox("Are you sure you wish to delete this phase and all related scenarios and nodes?", style)
  If Response = vbYes Then
     deleteScenariosAndNodes PhaseID
     Forms!Main!Scenarios.Requery
  Else
     Cancel = True
  End If
End Sub

Public Sub deleteScenariosAndNodes(PhaseID As Long)
  Dim ESDHeadNodeID As Long
  Dim scenID As Long
  Dim rst_Del As Recordset
  Set rst_Del = CurrentDb.OpenRecordset("SELECT * FROM Scenarios WHERE PhaseID = " & PhaseID)
  While Not rst_Del.EOF
     scenID = rst_Del!ScenarioID
     ESDHeadNodeID = DLookup("ESDNodeID", "ESDNodes", "((ESDNodes.ESDNodeType)=1) AND ((ESDNodes.ScenarioID) = " & scenID & ")")
     DeleteESDChildren ESDHeadNodeID, False
     rst_Del.Delete
     rst_Del.MoveNext
  Wend
End Sub

1 个答案:

答案 0 :(得分:2)

完全删除OnCurrent代码。

然后将子窗体控件的属性 MasterLinkFields ChildLinkFields 指定为:Private Sub Form_Delete(Cancel As Integer) Dim Response As Long Dim style As Long style = vbYesNo + vbQuestion Response = MsgBox("Are you sure you wish to delete this phase and all related scenarios and nodes?", style) If Response <> vbYes Then Cancel = True End If End Sub

此外,如果您在主表和子表之间应用参照完整性并将级联删除设置为开启,则其余代码可以减少到:

<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" id="animal"  value="1">Animal1</a>
<a class="dropdown-item" id="animal"  value="2">Animal2</a>
<a class="dropdown-item" id="animal"  value="3">Animal3</a>
</div>