+ Reply to Thread
Results 1 to 6 of 6

killing all procedures?

  1. #1
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100

    killing all procedures?

    Hi all,

    How do you kill all procedures on an error?

    I have a procedure that calls a series of other procedures. I have error code in each of those sub procedures that will kill that sub procedure if there is an error within that sub procedure, but the main procedure thread still keeps firing off.



    Please Login or Register  to view this content.

    But SubP2 still fires off, as well as anything else downstream of SubP1 in MasterList even if SubP1 errors out and is killed properly.

    So what can I insert into the error handler in SubP1 that will kill all procedures entirely?

  2. #2
    Harald Staff
    Guest

    Re: killing all procedures?

    You want the Sub to return something. That's a Function, not a Sub. Or
    rather; a Sub is a Fubctionthat returns nothing. Try this, testrun
    MasterList_click a few times until it makes sense:

    Private Sub MasterList_click()
    If P1 = False Then Exit Sub
    If P2 = False Then Exit Sub
    'and so on
    End Sub

    Private Function P1() As Boolean
    Dim R As Long
    On Error GoTo Handler1
    R = 2 / (Second(Now) Mod 2)
    P1 = True
    MsgBox "Yo from P1"
    Exit Function
    Handler1:
    P1 = False
    MsgBox Error
    End Function

    Private Function P2() As Boolean
    MsgBox "Yo from P2!"
    End Function

    HTH. Best wishes Harald


    "Ouka" <Ouka.1webmi_1128459933.1506@excelforum-nospam.com> skrev i melding
    news:Ouka.1webmi_1128459933.1506@excelforum-nospam.com...
    >
    > Hi all,
    >
    > How do you kill all procedures on an error?
    >
    > I have a procedure that calls a series of other procedures. I have
    > error code in each of those sub procedures that will kill that sub
    > procedure if there is an error within that sub procedure, but the main
    > procedure thread still keeps firing off.
    >
    >
    >
    >
    > Code:
    > --------------------
    > Private Sub MasterList_click()
    >
    > 'procedure code here'
    >
    > Call SubP1
    > Call SubP2
    >
    > End sub
    >
    >
    > Private SubP1()
    >
    > On Error Goto Handler1
    > Exit Sub
    > Handler1:
    > MsbBox "An error has occured, exiting procedure"
    >
    > 'sub procedure code here'
    >
    > End Sub
    > --------------------
    >
    >
    >
    > But SubP2 still fires off, as well as anything else downstream of SubP1
    > in MasterList even if SubP1 errors out and is killed properly.
    >
    > So what can I insert into the error handler in SubP1 that will kill all
    > procedures entirely?
    >
    >
    > --
    > Ouka
    > ------------------------------------------------------------------------
    > Ouka's Profile:

    http://www.excelforum.com/member.php...o&userid=23988
    > View this thread: http://www.excelforum.com/showthread...hreadid=473148
    >




  3. #3
    Walt
    Guest

    Re: killing all procedures?

    Hi Ouka,

    >From the Excel VBA Help system:

    Statement: End
    Description: Terminates execution immediately. Never required by
    itself but may be placed anywhere in a procedure to end code execution,
    close files opened with the Open statement and to clear variables.

    Meaning this should do it:
    Private SubP1()
    On Error Goto Handler1
    'sub procedure code here'
    Exit Sub
    Handler1:
    MsbBox "An error has occurred, exiting procedure"
    'EXIT code here, if any, besides the following End statement'
    End <<<<<<<<< This shuts down VBA execution at this point
    End Sub

    NOTE: The closing of VBA opened files and clearing of variables may
    require accommodation in your program flow.

    Best Regards,
    Walt


  4. #4
    Myrna Larson
    Guest

    Re: killing all procedures?

    True, but not considered "good programming practice" by the purists, and it
    has the drawback you note re open files, etc.

    On 4 Oct 2005 15:41:24 -0700, "Walt" <xlswalt@aol.com> wrote:

    >Hi Ouka,
    >
    >>From the Excel VBA Help system:

    >Statement: End
    >Description: Terminates execution immediately. Never required by
    >itself but may be placed anywhere in a procedure to end code execution,
    >close files opened with the Open statement and to clear variables.
    >
    >Meaning this should do it:
    >Private SubP1()
    > On Error Goto Handler1
    > 'sub procedure code here'
    >Exit Sub
    >Handler1:
    > MsbBox "An error has occurred, exiting procedure"
    > 'EXIT code here, if any, besides the following End statement'
    > End <<<<<<<<< This shuts down VBA execution at this point
    >End Sub
    >
    >NOTE: The closing of VBA opened files and clearing of variables may
    >require accommodation in your program flow.
    >
    >Best Regards,
    >Walt


  5. #5
    Walt
    Guest

    Re: killing all procedures?

    Hi Myrna,

    I try to never say never, but there's usually a way to avoid the
    necessity of violating a best practice - point taken. Given that Ouka
    wants his main to quit on failure in SubP1, would something like this
    be better?:

    'Declare a public variable
    Public OKGo as Boolean

    'The Main
    Private Sub MasterList_click()
    'procedure code here'
    OKGo = True
    Call SubP1
    If OKGo then Call SubP2
    End sub

    'The First Sub
    Private SubP1()
    On Error Goto Handler1
    'sub procedure code here'
    Exit Sub
    Handler1:
    MsbBox "An error has occurred, exiting procedure SubP1"
    'EXIT code here, if any, besides the following OKGo setting'
    OKGo = False
    End Sub

    Best Regards,
    Walt


  6. #6
    Forum Contributor
    Join Date
    06-02-2005
    Posts
    100
    Thanks all! Both ways worked like a charm. Just trying to decide which result I like better.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1