Exit Sub Not Working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • soodjunk
    New Member
    • Feb 2010
    • 2

    Exit Sub Not Working

    I have a problem, the Exit Sub command is not working for some reason. If the user says NO to the MsgBox I want to stop the code, but the program just continues to execute.

    I have use breakpoints and traced the code and found that the Exit Sub command is only working 50% of the time.


    tempNumber = MsgBox("Try Again?", 292)
    'If the user wants to try again
    If tempNumber = 6 Then
    ' Continue
    Else
    ' The user wants to exit
    Exit Sub

    End If
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Code:
    Dim tempNumber As Integer
    
    tempNumber = MsgBox("Try Again?", vbYesNo + vbInformation + vbDefaultButton2)
    
    If tempNumber = vbNo Then Exit Sub
    
    'Continue if User selects 'Yes'

    Comment

    • soodjunk
      New Member
      • Feb 2010
      • 2

      #3
      Thank you for your response i appreciate the help you're trying to give me. I rearranged my code as per your suggestion but I'm running into the same problem.

      My code is setup so that if the user wants to retry I am calling my function again and starting from the beginning. So I keep on looping if the user makes a mistake and wants to re-enter information.

      If the user wants to exit after making a mistake the FIRST time , the program ends properly. If the user chooses to try again then makes the mistake again and then clicks no, the program comes to the Exit Sub command again and just continues with the code as if it executed it.




      Code:
      Private Sub btnLedgerEnter_Click()
      
      Dim tempNumber As Integer
      
      tempNumber = MsgBox("Try Again?", vbYesNo + vbInformation + vbDefaultButton2)
        
      If tempNumber = vbNo Then Exit Sub
      
      else
        btnLedgerEnter_Click
      end if
      ' MORE CODE
      end sub

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        You can have a one-line If...Then statement

        If tempNumber = vbNo Then Exit Sub

        but you cannot use that If for the followup Else and End If. Try

        Code:
        If tempNumber = vbNo Then 
          Exit Sub
        Else
           btnLedgerEnter_Click
        End if
        Welcome to Bytes!

        Linq ;0)>

        Comment

        Working...