Results 1 to 13 of 13

Thread: Exiting outer subs

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    3

    Exiting outer subs

    I have a sub, which calls a sub, which calls a sub (i.e. 3 levels). If an error occurs in the level 3 sub, the error handler exits the inside/level 3 sub, but the rest of the program continues....I want the entire thing to shut down, rather than just exiting the inside/level 3 sub, because the nature of the error requires the user to fix an excel file causing the error and run the entire thing again.

    Is there a way for an inner sub to send a message to all outer subs to shut down after an error occurs in the inner sub, rather than continue running the outer sub(s)?

  2. #2
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950
    Do boolean functions instead and return false if an error occurs.

    From sub 2

    If Function3 = false then
    MsgBox "Error occured in Function 3"
    end if

  3. #3
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    create a public variable that the middle sub sets then check its value after the sub is called

    like if the error happens

    blnError = True

    then after the code calling the sub

    If blnError = True Then
    Exit Sub
    End If

    set it to false later to make sure the value is reset for when it goes through those subs again
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  4. #4
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    yup, dead easy.


    create an error handler in Sub1,

    and dont have an error handler in sub2 or sub3,

    then sub1 will handle it...

    error-handling is handles heiracally (i think that's the right word!)
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  5. #5
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Try using a global Boolean variable set it to True
    If an error occurs set it to false
    Then in each sub that calls another sub next line check the Bool if false exit this sub and so on.

  6. #6
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Snap
    Snap
    Snap

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Smack
    Smack
    Smack.

    you gotta be faster then that Glen
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8
    GUUS
    Guest
    Declare a boolean as true or false in the level 2 and 3 subs before exiting them and check them in the level 1 and 2 subs....

    GUUS

  9. #9
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    I know guess its my age

  10. #10
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Snap
    Snap
    Snap


    damn it took me over half an hour to type that...
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    3

    Thanks

    I decided to go with the advice to eliminate error handling for the inner subs so that the error bubbled to the outside sub.

    Since the application imports data from several linked excel files, my next dilemma was to capture the name of the excel file which the inner sub was acting upon when the error occured, so the user would know which excel file to fix/format correctly. To deal with that, I created a global variable (outside of all the subs) and set it to the name of the linked excel file it was trying to import at that moment from the inner sub which acted upon the excel file.

    Thanks for all your help.

  12. #12
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  13. #13
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479
    Why not use this file name variable for error checking
    ?
    Set it to ""
    If it gets a name in it then theres an error.
    Save a little bit space

Posting Permissions

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



Click Here to Expand Forum to Full Width