将 On Error GoTo 与 If、elseIf 语句一起使用,msg 会重复自身

Using On Error GoTo with If, elseIf statements, msg repeats itself

我是 On Error GoTo 的新手。我有一个检查客户端状态的功能。如果为 0,则通过 else,代码应为 5-18。我试图让它到达错误到达的地方,弹出一个包含该错误的消息框,一旦用户单击确定,我的应用程序将从头开始并 运行 通过查看该错误是否已修复或如果在那之后还有另一个错误。不确定 On Error GoTo 是否是最好的方法。我不知道如何根据返回的数字显示每条单独的消息,而不是一条通用消息。目前,当我 运行 时,我一直不得不按 ok,然后我收到错误消息并按 ok,等等

On Error GoTo Error
    Debug.Print "Starting Check."
TryAgain:
        client.Read "State", State
    If State <> 0 Then
    ElseIf code = 5 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 6 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 7 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 8 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 9 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 10 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 11 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 12 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 13 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 14 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 15 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 16 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 17 Then
        Message = "*** - ERROR: Description Here"
    ElseIf code = 18 Then
        Message = "*** - ERROR: Description Here"
    End If
    Else
MsgBox ("Successful")
    Exit Function
    End If
    
Error:
 'Display Message from above is what i am having trouble with
  Resume TryAgain

这里有一个建议:

Don't use GoTo in VBA as a way to control program flow, except for error and exit handling "on error goto"

它将引导您进入意大利面条代码

我重构了您的代码,并添加了一些亮点:

  • 不要重复自己的原则
  • 通过单独的函数处理错误消息
  • 循环处理读取客户端成功

我对变量类型和其他内容做了一些假设,因此请阅读代码注释并根据您的需要对其进行自定义。

PS。不要忘记添加退出和错误处理

希望这能让您走上正确的道路。如果有帮助,请告诉我。

代码

Public Function Test()

    ''''''Your code here'''''

    Debug.Print "Starting Check."
    
    Dim retry As Boolean

    ' Try to not repeat code / text if you can
    Dim messagePrefix As String
    messagePrefix = "*** - ERROR: "
    
    Do
        
        ' Read and assign state
        client.Read "State", State
        
        If State = 0 Then
            retry = False
            MsgBox ("Successful")
            Exit Do
        Else
            ' State <> 0 Add error description
            message = GetClientMessage(code)
            'Display message from above
            MsgBox messagePrefix & message
            retry = False
        End If
        
    Loop While retry = False
    
    ' Do something else???
    
' Add the exit and error handling labels

End Function


Private Function GetClientMessage(ByVal code As Integer) As String
    
    Dim message As String
    
    Select Case code
    Case 5
        message = "Description Here"
    Case 6
        message = "Description Here"
    Case 7
        message = "Description Here"
    Case 8
        message = "Description Here"
    Case Else
        message = "Description Here"
    End Select
    
    GetClientMessage = message

End Function