excelvbaonerror

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


I am new to On Error GoTo. I have a function that checks the state of my client. If 0, it passes else, the code should be 5-18. I am trying to make it to where once an error arrives, a message box pops up with that error and once the user clicks okay, my application will start from the beginning and run through to see if that error was fixed or if there is another error after that one. Not sure if On Error GoTo is the best method for this or not. I don't know how to make each individual message display based on the number returned, rather than one generic message. Currently, when I run, I keep having to press ok and then i get error message and press ok, etc.

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

Solution

  • Here is a suggestion:

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

    It will lead you to spaguetti code

    I refactored your code with some highlights:

    I made some assumptions about variable types and other stuff, so please read the code's comments and customize it to fit your needs.

    PS. Don't forget to add exit and error handling

    Hope this puts you in the right path. Let me know if it helps.

    Code

    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