Thanks Thanks:  0
Likes Likes:  0
Results 1 to 5 of 5

Thread: On Error GoTo only works once

  1. #1
    New Member
    Join Date
    May 2011
    Posts
    1
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default On Error GoTo only works once

    Hello i have come accross a problem i used on error goto line 1 which shows a error message i will show the code please help if you answer just remember im quiet new to visual basics and will need a bit of explaining:

    Module Module1
    Code:
        Sub Main()
    1:      Console.Clear()
            Console.Write("Simple Caclulator")
            Console.WriteLine()
            Console.WriteLine()
            Console.WriteLine()
            Console.Write("Please Select and option from the menu below by typing the corresponding number and pressing enter")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write("1. All Calculation will be answered in Integer mode")
            Console.WriteLine()
            Console.Write("2. All Calculations wil be answered in Decimal mode")
            Console.WriteLine()
            Console.Write("3. Exit")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write(" Please enter and option:")
            Dim Uoption As String
            Uoption = Console.ReadLine
            If IsNumeric(Uoption) = False Then
    4:          Console.Clear()
                Console.Write("Please select a valid option.")
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                GoTo 1
            End If
            If Uoption = 1 Then
                GoTo 2
            End If
            If Uoption = 2 Then
                GoTo 3
            End If
            If Uoption = 3 Then
                End
            End If
    
    2:      Console.Clear()
            Console.Write("Simple Calculator: Integer Mode")
            Console.WriteLine()
            Console.WriteLine()
            Console.WriteLine()
            Console.Write("Please Select and option from the menu below by typing the corresponding number and pressing enter")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write("1.Addition")
            Console.WriteLine()
            Console.Write("2.Subtraction")
            Console.WriteLine()
            Console.Write("3.Division")
            Console.WriteLine()
            Console.Write("4.Multiplication")
            Console.WriteLine()
            Console.Write("5.Exit")
            Console.WriteLine()
            Console.Write("6.Change Calculator Mode")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write("Please enter and option:")
            Dim Uchoice As Integer
            On Error GoTo 4
            Uchoice = CInt(Console.ReadLine)
            If IsNumeric(Uchoice) = False Then
                Console.Clear()
                Console.Write("Please select a valid option.")
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                GoTo 1
            End If
            If Uchoice = 1 Then
                On Error GoTo 4
                Console.Clear()
                Console.Write("Simple Calculator: Integer mode: Addition")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please Enter your first value:")
                Dim value1 As Integer
                value1 = CInt(Console.ReadLine)
                Console.Write("What would you like to add to {0}?  {0} + ", value1)
                Dim value2 As Integer
                value2 = CInt(Console.ReadLine)
                Dim answer As Integer
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} + {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
            On Error GoTo 4
            If Uchoice = 2 Then
                Console.Clear()
                Console.Write("Simple Calculator: Integer mode: Subtraction")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please Enter your first value:")
                Dim value1 As Integer
                value1 = CInt(Console.ReadLine)
                Console.Write("What would you like to subtract {0} by?  {0} - ", value1)
                Dim value2 As Integer
                value2 = CInt(Console.ReadLine)
                Dim answer As Integer
                answer = value1 - value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} - {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
            On Error GoTo 4
            If Uchoice = 3 Then
                Console.Clear()
                Console.Write("Simple Calculator: Integer mode: Division")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please Enter your first value:")
                Dim value1 As Integer
                value1 = CInt(Console.ReadLine)
                Console.Write("What would you like to divide {0} by?  {0} / ", value1)
                Dim value2 As Integer
                value2 = CInt(Console.ReadLine)
                Dim answer As Integer
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} / {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
    
            End If
            On Error GoTo 4
            If Uchoice = 4 Then
                Console.Clear()
                Console.Write("Simple Calculator: Integer mode: Multiplication")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please Enter your first value:")
                Dim value1 As Integer
                value1 = CInt(Console.ReadLine)
                Console.Write("What would you like to multiply {0} by?  {0} * ", value1)
                Dim value2 As Integer
                value2 = CInt(Console.ReadLine)
                Dim answer As Integer
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} * {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
    3:      Console.Clear()
            Console.Write("Simple Calculator: Integer Mode")
            Console.WriteLine()
            Console.WriteLine()
            Console.WriteLine()
            Console.Write("Please Select and option from the menu below by typing the corresponding number and pressing enter")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write("1.Addition")
            Console.WriteLine()
            Console.Write("2.Subtraction")
            Console.WriteLine()
            Console.Write("3.Division")
            Console.WriteLine()
            Console.Write("4.Multiplication")
            Console.WriteLine()
            Console.Write("5.Exit")
            Console.WriteLine()
            Console.Write("6.Change Calculator Mode")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write("Please enter and option:")
            Uchoice = CDec(Console.ReadLine)
            On Error GoTo 4
            If IsNumeric(Uchoice) = False Then
                Console.Clear()
                Console.Write("Please select a valid option.")
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                GoTo 1
            End If
            If Uchoice = 1 Then
                On Error GoTo 4
                Console.Clear()
                Console.Write("Simple Calculator: Integer mode: Addition")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please Enter your first value:")
                Dim value1 As Decimal
                value1 = CDec(Console.ReadLine)
                Console.Write("What would you like to add to {0}?  {0} + ", value1)
                Dim value2 As Decimal
                value2 = CDec(Console.ReadLine)
                Dim answer As Decimal
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} + {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
            If Uchoice = 2 Then
                On Error GoTo 4
                Console.Clear()
                Console.Write("Simple Calculator: Integer mode: Subtraction")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please Enter your first value:")
                Dim value1 As Decimal
                value1 = CDec(Console.ReadLine)
                Console.Write("What would you like to subtract {0} by?  {0} - ", value1)
                Dim value2 As Decimal
                value2 = CDec(Console.ReadLine)
                Dim answer As Decimal
                answer = value1 - value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} - {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
            If Uchoice = 3 Then
                On Error GoTo 4
                Console.Clear()
                Console.Write("Simple Calculator: Integer mode: Division")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please Enter your first value:")
                Dim value1 As Decimal
                value1 = CDec(Console.ReadLine)
                Console.Write("What would you like to divide {0} by?  {0} / ", value1)
                Dim value2 As Decimal
                value2 = CDec(Console.ReadLine)
                Dim answer As Decimal
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} / {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
    
            End If
            If Uchoice = 4 Then
                On Error GoTo 4
                Console.Clear()
                Console.Write("Simple Calculator: Integer mode: Multiplication")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please Enter your first value:")
                Dim value1 As Decimal
                value1 = CDec(Console.ReadLine)
                Console.Write("What would you like to multiply {0} by?  {0} * ", value1)
                Dim value2 As Decimal
                value2 = CDec(Console.ReadLine)
                Dim answer As Decimal
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} * {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
    
        End Sub
    
    End Module


    PLEASE HELP ASAP

    THANKS SO MUCH

    Brad,
    Last edited by RoryA; Dec 1st, 2015 at 11:34 AM.

  2. #2
    MrExcel MVP boblarson's Avatar
    Join Date
    Nov 2008
    Location
    Portland, Oregon, USA
    Posts
    1,964
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: On Error GoTo only works once

    This is not a Microsoft Access question. This is a VISUAL BASIC question.
    Bob Larson
    Former Access MVP (2008-2010, 2011)

  3. #3
    Banned user
    Join Date
    May 2014
    Location
    Germany
    Posts
    1,336
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: On Error GoTo only works once

    Hi bradywadypoo,

    _ 1 ) Just like in VBA, in VB the
    On Error Go To 4
    Bit in your code needs to be done just the once before you need it, not several times after each possible error occurrence!


    _2) And the same also as in VBA, if you want it to be used more than once, then you need a
    On Error GoTo -1
    At the appropriate point!


    See:
    On Error WTF? | Excel Matters
    https://app.box.com/files/0/f/366413.../f_29651830467



    _ I did this modified code and checked it in the Console Running Bit in VB 2008. Seems to work and do what you want




    Code:
    '
    '
    '
    'http://www.mrexcel.com/forum/general-excel-discussion-other-questions/547261-error-goto-only-works-once.html
    Module Module1
    
        Sub Main()
    
            On Error GoTo 4 'Error Handler for whole Sub
    1:      Console.Clear()
            Console.Write ("Simple Caclulator")
            Console.WriteLine()
            Console.WriteLine()
            Console.WriteLine()
            Console.Write ("Please Select and option from the menu below by typing the corresponding number and pressing enter")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write ("1. All Calculation will be answered in Integer mode")
            Console.WriteLine()
            Console.Write ("2. All Calculations wil be answered in Decimal mode")
            Console.WriteLine()
            Console.Write ("3. Exit")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write (" Please enter and option:")
            Dim Uoption As String
            Uoption = Console.ReadLine
            If IsNumeric(Uoption) = False Then
    4:          Console.Clear()
                Console.Write ("Please select a valid option.")
                Console.WriteLine()
                Console.Write ("Please press enter to go back to the main menu.")
                Console.ReadLine()
                On Error GoTo -1
                GoTo 1
            End If
    
    
            If Uoption = 1 Then
                GoTo 2
            End If
            If Uoption = 2 Then
                GoTo 3
            End If
            If Uoption = 3 Then
                End
            End If
    
    2:      Console.Clear()
            Console.Write ("Simple Calculator: Integer Mode")
            Console.WriteLine()
            Console.WriteLine()
            Console.WriteLine()
            Console.Write ("Please Select and option from the menu below by typing the corresponding number and pressing enter")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write ("1.Addition")
            Console.WriteLine()
            Console.Write ("2.Subtraction")
            Console.WriteLine()
            Console.Write ("3.Division")
            Console.WriteLine()
            Console.Write ("4.Multiplication")
            Console.WriteLine()
            Console.Write ("5.Exit")
            Console.WriteLine()
            Console.Write ("6.Change Calculator Mode")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write ("Please enter and option:")
            Dim Uchoice As Integer
            'On Error GoTo 4
            Uchoice = CInt(Console.ReadLine)
            If IsNumeric(Uchoice) = False Then GoTo 4
            End If
            If Uchoice = 1 Then
                'On Error GoTo 4
                Console.Clear()
                Console.Write ("Simple Calculator: Integer mode: Addition")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please Enter your first value:")
                Dim value1 As Integer
                value1 = CInt(Console.ReadLine)
                Console.Write("What would you like to add to {0}? {0} + ", value1)
                Dim value2 As Integer
                value2 = CInt(Console.ReadLine)
                Dim answer As Integer
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} + {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
            'On Error GoTo 4
            If Uchoice = 2 Then
                Console.Clear()
                Console.Write ("Simple Calculator: Integer mode: Subtraction")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please Enter your first value:")
                Dim value1 As Integer
                value1 = CInt(Console.ReadLine)
                Console.Write("What would you like to subtract {0} by? {0} - ", value1)
                Dim value2 As Integer
                value2 = CInt(Console.ReadLine)
                Dim answer As Integer
                answer = value1 - value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} - {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
            'On Error GoTo 4
            If Uchoice = 3 Then
                Console.Clear()
                Console.Write ("Simple Calculator: Integer mode: Division")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please Enter your first value:")
                Dim value1 As Integer
                value1 = CInt(Console.ReadLine)
                Console.Write("What would you like to divide {0} by? {0} / ", value1)
                Dim value2 As Integer
                value2 = CInt(Console.ReadLine)
                Dim answer As Integer
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} / {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
    
            End If
            'On Error GoTo 4
            If Uchoice = 4 Then
                Console.Clear()
                Console.Write ("Simple Calculator: Integer mode: Multiplication")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please Enter your first value:")
                Dim value1 As Integer
                value1 = CInt(Console.ReadLine)
                Console.Write("What would you like to multiply {0} by? {0} * ", value1)
                Dim value2 As Integer
                value2 = CInt(Console.ReadLine)
                Dim answer As Integer
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} * {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
    3:      Console.Clear()
            Console.Write ("Simple Calculator: Integer Mode")
            Console.WriteLine()
            Console.WriteLine()
            Console.WriteLine()
            Console.Write ("Please Select and option from the menu below by typing the corresponding number and pressing enter")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write ("1.Addition")
            Console.WriteLine()
            Console.Write ("2.Subtraction")
            Console.WriteLine()
            Console.Write ("3.Division")
            Console.WriteLine()
            Console.Write ("4.Multiplication")
            Console.WriteLine()
            Console.Write ("5.Exit")
            Console.WriteLine()
            Console.Write ("6.Change Calculator Mode")
            Console.WriteLine()
            Console.WriteLine()
            Console.Write ("Please enter and option:")
            Uchoice = CDec(Console.ReadLine)
            'On Error GoTo 4
            If IsNumeric(Uchoice) = False Then GoTo 4
            End If
            If Uchoice = 1 Then
                'On Error GoTo 4
                Console.Clear()
                Console.Write ("Simple Calculator: Integer mode: Addition")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please Enter your first value:")
                Dim value1 As Decimal
                value1 = CDec(Console.ReadLine)
                Console.Write("What would you like to add to {0}? {0} + ", value1)
                Dim value2 As Decimal
                value2 = CDec(Console.ReadLine)
                Dim answer As Decimal
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} + {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
            If Uchoice = 2 Then
                'On Error GoTo 4
                Console.Clear()
                Console.Write ("Simple Calculator: Integer mode: Subtraction")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please Enter your first value:")
                Dim value1 As Decimal
                value1 = CDec(Console.ReadLine)
                Console.Write("What would you like to subtract {0} by? {0} - ", value1)
                Dim value2 As Decimal
                value2 = CDec(Console.ReadLine)
                Dim answer As Decimal
                answer = value1 - value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} - {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
            If Uchoice = 3 Then
                'On Error GoTo 4
                Console.Clear()
                Console.Write ("Simple Calculator: Integer mode: Division")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please Enter your first value:")
                Dim value1 As Decimal
                value1 = CDec(Console.ReadLine)
                Console.Write("What would you like to divide {0} by? {0} / ", value1)
                Dim value2 As Decimal
                value2 = CDec(Console.ReadLine)
                Dim answer As Decimal
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} / {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
    
            End If
            If Uchoice = 4 Then
                'On Error GoTo 4
                Console.Clear()
                Console.Write ("Simple Calculator: Integer mode: Multiplication")
                Console.WriteLine()
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please Enter your first value:")
                Dim value1 As Decimal
                value1 = CDec(Console.ReadLine)
                Console.Write("What would you like to multiply {0} by? {0} * ", value1)
                Dim value2 As Decimal
                value2 = CDec(Console.ReadLine)
                Dim answer As Decimal
                answer = value1 + value2
                Console.WriteLine()
                Console.WriteLine()
                Console.Write("{0} * {1} = {2}", value1, value2, answer)
                Console.WriteLine()
                Console.WriteLine()
                Console.Write ("Please press enter to go back to the main menu.")
                Console.ReadLine()
                Console.Clear()
                GoTo 1
            End If
    
        End Sub
    
    End Module

    Alan.
    Germany

  4. #4
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    32,351
    Post Thanks / Like
    Mentioned
    25 Post(s)
    Tagged
    3 Thread(s)

    Default Re: On Error GoTo only works once

    I'd be surprised if he's still waiting for an answer to be honest...

  5. #5
    Banned user
    Join Date
    May 2014
    Location
    Germany
    Posts
    1,336
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: On Error GoTo only works once

    Quote Originally Posted by RoryA View Post
    I'd be surprised if he's still waiting for an answer to be honest...
    Just hit it coincidentally while searching for and trying to remember me Error stuff. Seemed a good thing to warm up on.

    I confesse i was curious myself why i answered a Visual Basic thing which i know even less about than VBA...
    _ maybe........

    Someone famous in the world said
    “Because it is there”

    Someone famous in this forum said
    “Because he ( meaning me ) can “

    Someone asked me once why i was counting things. ( I was counting things at the time and confess i did not know why... ) I said:
    “Someone’s got to do it”

    Not sure if that helps...
    Alan.DE

    P.s.
    Maybe....
    Predictability is the hobgoblin of small minds.

    Or On A serious note....The title could lead someone here on a Google search or wotever who could benefit from the Links i gave
    Last edited by DocAElstein; Dec 1st, 2015 at 12:03 PM.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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