我需要让一个表格打开另一个表格,然后立即关闭。请注意,我的表单有一个每两秒发生一次的 onTimer 事件过程。我知道以下命令:
DoCMD Close
和
DoCMD OpenForm "frmOnError"
并且它们都单独工作,但是当我执行以下操作时:
DoCMD OpenForm "frmOnError"
DoCMD Close
然后表格只是闪烁,并没有关闭。它打开的表单不是它本身,它是我要打开的表单的名称。同样,如果我注释掉其中任何一行,它就会按照剩余行所说的那样做。
接下来,我尝试在第一个表单上使用 DoCMD OpenForm,并且我在另一个表单中有一个 if 语句,如果它是打开的,它将关闭第一个表单。这很混乱,因为两种形式都有相同的命令,并且任何一个在命令中击败另一个的都会执行它。这意味着我无法判断哪个表单会关闭。
我的最后一个变体是在 button_clicked 子中使用 DoCMD OpenForm 命令,在代码的下方,在 Form_Timer 部分,我有 DoCMD 关闭命令。它不会在加载后关闭另一个表单,而是在加载另一个表单后自行关闭。这更稳定,但仍然混乱。
有了这个,我没有想法。
代码说明:
代码的重点是关闭第一个表单,这取决于链接表和连接丢失时的错误。这将打开不依赖链接连接并继续工作的第二种形式。然后,新表单将有一个关闭它并尝试再次加载第一个表单的按钮。这两种形式都依赖于 2 秒定时器来模拟来自 PLC 的恒定随机数据,并且将继续无限期地需要定时器。
最后,这里是代码段,按它们应该工作的顺序排列:
从循环:
Public Sub DoSQL4(vFacility As String, vWorkcell As Integer, vStation As Integer, vEventcode As Integer, vFacilityPath, vFacilityID, vFaultCodeID, vStateCode As Integer)
On Error GoTo ErrorHandler4
Const conLINKED_TABLE As String = "tblevent"
'INSERT INTO LINKED TABLE
CurrentDb.TableDefs(conLINKED_TABLE).RefreshLink
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO tblevent (vchrFacility, intWorkCell, intStn, intEventCode) VALUES ('" & vFacility & "', '" & vWorkcell & "', '" & vStation & "', '" & vEventcode & "');"
DoCmd.RunSQL "INSERT INTO tblfaultdata (vchrFacilityPath, intFacilityID, intFaultCodeID, intWorkcell) VALUES ('" & vFacilityPath & "', '" & vFacilityID & "', '" & vFaultCodeID & "', '" & vWorkcell & "');"
DoCmd.RunSQL "INSERT INTO tblstate (vchrFacility, intWorkCell, intStn, intStateCode) VALUES ('" & vFacility & "', '" & vWorkcell & "', '" & vStation & "', '" & vStateCode & "');"
DoCmd.SetWarnings True
'INSERT INTO LOCAL TABLE
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT INTO Local_tblevent (vchrFacility, intWorkCell, intStn, intEventCode) VALUES ('" & vFacility & "', '" & vWorkcell & "', '" & vStation & "', '" & vEventcode & "');"
DoCmd.RunSQL "INSERT INTO Local_tblfaultdata (vchrFacilityPath, intFacilityID, intFaultCodeID, intWorkcell) VALUES ('" & vFacilityPath & "', '" & vFacilityID & "', '" & vFaultCodeID & "', '" & vWorkcell & "');"
DoCmd.RunSQL "INSERT INTO Local_tblstate (vchrFacility, intWorkCell, intStn, intStateCode) VALUES ('" & vFacility & "', '" & vWorkcell & "', '" & vStation & "', '" & vStateCode & "');"
DoCmd.SetWarnings True
Exit_theSub:
DoCmd.OpenForm "frmOnError"
If CurrentProject.AllForms("frmOnError").IsLoaded And Forms!frmOnError.CurrentView <> acCurViewDesign Then
DoCmd.Close
End If
'ERROR HANDLER
ErrorHandler4:
If Err.Number <> 0 Then
Select Case Err.Number
Case 3265
MsgBox "[" & conLINKED_TABLE & "] does not exist as either an Internal or Linked Table", _
vbCritical, "Table Missing"
Case 3011, 3024 'Linked Table does not exist or DB Path not valid
MsgBox "[" & conLINKED_TABLE & "] is not a valid, Linked Table", vbCritical, "Link Not Valid"
Case Else
MsgBox Err.Description & Err.Number, vbExclamation, "Error"
End Select
Resume Exit_theSub
End If
End Sub
然后frmOnError:
Private Sub btnRetry_Click()
DoCmd.OpenForm "frmLoop"
End Sub
Private Sub Form_Timer()
If CurrentProject.AllForms("frmOnError").IsLoaded And Forms!frmOnError.CurrentView <> acCurViewDesign Then
DoCmd.Close
End If
等等
他们基本上反弹并获得第四名。一旦错误处理程序关闭了一个表单,它就会打开另一个表单,然后自行关闭。如果在另一个窗体上按下按钮,它会先打开第一个窗体,然后自行关闭。
这就是理论。我的代码中的某些东西非常错误,我希望你们中的一个人能够发现并指出它。谢谢!
编辑:我更新了我的代码以响应用户的回答,并将 DoCmd.RunSql 替换为 CurrentDb.Execute、dbfailonerror。现在我的表单在打开时立即关闭,并且没有返回错误。
'INSERT INTO LINKED TABLE
CurrentDb.TableDefs(conLINKED_TABLE).RefreshLink
CurrentDb.Execute "INSERT INTO tblevent (vchrFacility, intWorkCell, intStn, intEventCode) VALUES ('" & vFacility & "', '" & vWorkcell & "', '" & vStation & "', '" & vEventcode & "');", dbFailOnError
CurrentDb.Execute "INSERT INTO tblfaultdata (vchrFacilityPath, intFacilityID, intFaultCodeID, intWorkcell) VALUES ('" & vFacilityPath & "', '" & vFacilityID & "', '" & vFaultCodeID & "', '" & vWorkcell & "');", dbFailOnError
CurrentDb.Execute "INSERT INTO tblstate (vchrFacility, intWorkCell, intStn, intStateCode) VALUES ('" & vFacility & "', '" & vWorkcell & "', '" & vStation & "', '" & vStateCode & "');", dbFailOnError
'INSERT INTO LOCAL TABLE
CurrentDb.Execute "INSERT INTO Local_tblevent (vchrFacility, intWorkCell, intStn, intEventCode) VALUES ('" & vFacility & "', '" & vWorkcell & "', '" & vStation & "', '" & vEventcode & "');", dbFailOnError
CurrentDb.Execute "INSERT INTO Local_tblfaultdata (vchrFacilityPath, intFacilityID, intFaultCodeID, intWorkcell) VALUES ('" & vFacilityPath & "', '" & vFacilityID & "', '" & vFaultCodeID & "', '" & vWorkcell & "');", dbFailOnError
CurrentDb.Execute "INSERT INTO Local_tblstate (vchrFacility, intWorkCell, intStn, intStateCode) VALUES ('" & vFacility & "', '" & vWorkcell & "', '" & vStation & "', '" & vStateCode & "');", dbFailOnError
Exit_theSub:
DoCmd.Close
'ERROR HANDLER
ErrorHandler4:
If Err.Number <> 0 Then
Select Case Err.Number
Case 3265
MsgBox "[" & conLINKED_TABLE & "] does not exist as either an Internal or Linked Table", _
vbCritical, "Table Missing"
DoCmd.OpenForm "frmOnError"
Case 3011, 3024 'Linked Table does not exist or DB Path not valid
MsgBox "[" & conLINKED_TABLE & "] is not a valid, Linked Table", vbCritical, "Link Not Valid"
DoCmd.OpenForm "frmOnError"
Case Else
MsgBox Err.Description & Err.Number, vbExclamation, "Error"
DoCmd.OpenForm "frmOnError"
End Select
Resume Exit_theSub
End If
结束子