あるフォームを別のフォームで開き、すぐに閉じる必要があります。私のフォームには、2 秒ごとに発生する onTimer イベント プロシージャがあることに注意してください。次のコマンドを認識しています。
DoCMD Close
と
DoCMD OpenForm "frmOnError"
どちらも個別に動作しますが、次のようにすると:
DoCMD OpenForm "frmOnError"
DoCMD Close
次に、フォームが点滅するだけで、閉じません。開いているフォームはそれ自体ではなく、開きたいフォームの名前です。繰り返しますが、これらの行のいずれかをコメントアウトすると、残りの行の内容が実行されます。
次に、最初のフォームで DoCMD OpenForm を使用しようとしましたが、他のフォームに if ステートメントがあり、最初のフォームが開いている場合は閉じます。これは、両方のフォームが同じコマンドを持っていて、どちらが他のフォームをコマンドに打ち負かしたとしても、それを実行するため、無秩序に機能しました。これは、どのフォームが閉じるかを知る方法がないことを意味していました。
私の最後のバリエーションは、button_clicked サブで DoCMD OpenForm コマンドを使用し、コードの下の Form_Timer セクションで DoCMD close コマンドを使用することでした。他のフォームが読み込まれると閉じるのではなく、他のフォームが読み込まれると自動的に閉じます。これはより安定していましたが、それでもカオスでした。
これで、私はアイデアがありません。
コードの説明:
コードのポイントは、最初のフォームを閉じることです。これは、リンクされたテーブルに依存し、接続が失われたときにエラーが発生します。これにより、リンクされた接続に依存せず、引き続き機能する 2 番目のフォームが開きます。次に、新しいフォームには、それを閉じて最初のフォームを再度ロードしようとするボタンがあります。どちらの形式も、PLC からの一定のランダム データをシミュレートするために 2 秒のタイマーに依存しており、無期限にタイマーを必要とし続けます。
最後に、動作する順序でコードのセグメントを示します。
ループから:
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
等
彼らは基本的に跳ね返り、4番目に跳ね返ります。一方がエラー ハンドラによって閉じられるとすぐに、もう一方のフォームが開き、それ自体が閉じられます。ボタンが他のフォームで押されると、最初のフォームが開き、次に閉じます。
それが理論です。私のコードの何かがひどく間違っています。あなたの誰かがそれを見つけて指摘してくれることを願っていました。ありがとう!
編集:ユーザーの回答に応じてコードを更新し、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
サブ終了