我需要让一种形式打开另一种形式,并立即关闭。请注意,我的表单有一个onTimer事件过程,每两秒钟发生一次。我知道以下命令:
DoCMD Close和
DoCMD OpenForm "frmOnError"它们都是单独工作的,但当我做以下工作时:
DoCMD OpenForm "frmOnError"
DoCMD Close然后表单就会闪现,而不会关闭。它正在打开的表单本身并不是,它是我希望打开的表单的名称。再一次,如果我注释掉这两行中的任何一行,它就会按照剩下的行的意思来做。
接下来,我尝试在第一个表单上使用一个DoCMD OpenForm,在另一个表单中有一个if语句,如果第一个表单是打开的,就会关闭它。这是一种混乱的工作方式,因为这两种表单都有相同的命令,无论哪一种要击败另一种命令,都会执行它。这意味着我无法判断哪一种形式会关闭。
我的最后一个版本是在一个DoCMD子类中使用一个OpenForm命令,而在代码的较低部分,在Form_Timer部分中,我使用了DoCMD close命令。它不是在加载后关闭另一个窗体,而是在加载另一个窗体时关闭自己。这是比较稳定的,但仍然混乱。
有了这个,我就没有主意了。
守则说明:
代码的目的是关闭第一个表单,它依赖于连接丢失时的链接表和错误。这将打开不依赖链接连接并继续工作的第二种形式。然后,新表单将有一个按钮关闭它,并尝试再次加载第一个表单。这两种形式都依赖于2秒计时器来模拟来自PLC的恒定随机数据,并且将继续无限期地要求计时器。
最后,下面是按代码的工作顺序排列的代码段:
frmLoop:
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结束子对象
发布于 2013-08-14 20:55:08
DoCmd.Close语句还可用于指定要关闭的窗体:
DoCmd.Close acForm, "frmFormName"如果你使用它,它应该能帮你解决这个问题。
作为附带说明,我建议使用CurrentDb.Execute "SQL语句“,dbFailOnError而不是DoCmd.RunSQL。这样,您就不必处理警告,当insert语句无法工作时,dbFailOnError标志将返回一个有用的错误代码。
https://stackoverflow.com/questions/18241903
复制相似问题