Consulting

Results 1 to 2 of 2

Thread: Error Handling for InputBoxes

  1. #1

    Question Error Handling for InputBoxes

    I'm trying to have some inputboxes for a byte-sized number, and when there's an error I want to display a msgbox saying "Please Enter a Number Between 1 and 10.". Here's what I have so far:

    Sub Example()
    
    
    Dim CodeLength As Byte, CodeRange As Byte
    
    
    CodeLengthInput:
    On Error GoTo CodeLengthError
        CodeLength = InputBox("Enter Code Length")
            
    CodeRangeInput:
        On Error GoTo CodeRangeError
        CodeRange = InputBox("Enter Code Range")
            
    GoTo InputsOK
            
    CodeLengthError:
        MsgBox ("Please Enter a Number Between 1 and 10.")
        GoTo CodeLengthInput
    CodeRangeError:
        MsgBox ("Please Enter a Number Between 1 and 10.")
        GoTo CodeRangeInput
        
    InputsOK:
    
    
    End Sub
    What happens when I run the above code is that the error handling performs correctly ONLY ONCE. On the second error, I get a run-time error window. This happens for both mismatch and overflow errors.

    The confusing part is that if I put in an incorrect value for the first inputbox, get the error msgbox, put in a correct value, then put an incorrect value for the second inputbox, then I still don't get a second error msgbox - the error msgbox will only appear once no matter what.

    Anybody have an idea on what I'm doing wrong? How do I get error handling to work correctly every time?

  2. #2
    VBAX Wizard
    Joined
    Apr 2007
    Posts
    6,883
    Location
    Instead of all those GoTo's, this might be simpler

    Option Explicit
    
    
    Sub Example()
        Dim CodeLength As Byte, CodeRange As Byte
        Dim bOK As Boolean
        
        
        bOK = False
        Do While Not bOK
            CodeLength = InputBox("Enter Code Length, 0 to exit")
            Select Case CodeLength
                Case 0
                    Exit Sub
                Case 1 To 10
                    bOK = True
                Case Else
                    MsgBox ("Please Enter a Number Between 1 and 10.")
            End Select
        Loop
    
    
        bOK = False
        Do While Not bOK
            CodeRange = InputBox("Enter Code Range, 0 to exit")
            Select Case CodeRange
                Case 0
                    Exit Sub
                Case 1 To 10
                    bOK = True
                Case Else
                    MsgBox ("Please Enter a Number Between 1 and 10.")
            End Select
        Loop
    End Sub
    Paul

    ------------------------------------------------------------------------------------------------------------------------
    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •