+ Reply to Thread
Results 1 to 3 of 3

Stopping Code

  1. #1
    Nigel
    Guest

    Stopping Code

    I have a 4 functions that work together

    Function 1 will prompt the user for a yes\no answer and if yes will run the
    function and then move to function2 and if no will move straight to function
    2 and so on, the problem is function 4, if I answer no it returns to function
    3 and runs from where it was called from.

    Is there anyway I can have the code just stop if the user says no in
    function 4

    thanks

  2. #2
    Edward Ulle
    Guest

    Re: Stopping Code

    Nigel,

    There are several options available.

    1) Set a global variable in your project for example.

    Dim CONTINUE_FUNCTIONS As Boolean

    Depending on the user selection set the variable to true or false and
    test it upon returning from each level of functions.

    Function1 ' Call the first level
    If Not CONTINUE_FUNCTION Then Exit Function

    2) Set the return type of the function to Boolean and set it to true or
    false testing it upon return from the function. This is my prefered
    method.

    Function Function1(...) As Boolean

    Then in the calling procedure.

    If Not Function1(...) Then Exit Function

    3) Or you could use error handling as shown in the following example.
    This assumes you have no other error handling in your functions. Notice
    the error handling is actually set at the highest level of your
    functions. It remains in scope throughout the calling of the sub
    functions, and it is envoked by the Err.Raise in Function4. Caution,
    some error numbers are reserved by VB but 1001 to 30000 are open.

    Option Explicit

    Sub Test()
    Function1
    MsgBox "Returned from Function1"
    End Sub

    Function Function1()

    On Error GoTo Return_To_Main

    If MsgBox("Continue", vbYesNo) = vbYes Then
    MsgBox "Doing something in Function1"
    End If
    Function2
    MsgBox "Returned from Function2"

    Return_To_Main:

    End Function

    Function Function2()

    If MsgBox("Continue", vbYesNo) = vbYes Then
    MsgBox "Doing something in Function2"
    End If
    Function3
    MsgBox "Returned from Function3"

    End Function

    Function Function3()

    If MsgBox("Continue", vbYesNo) = vbYes Then
    MsgBox "Doing something in Function3"
    End If
    Function4
    MsgBox "Returned from Function4"

    End Function

    Function Function4()

    If MsgBox("Continue", vbYesNo) = vbYes Then
    MsgBox "Doing something in Function4"
    Else
    Err.Raise 1001
    End If

    End Function



    *** Sent via Developersdex http://www.developersdex.com ***

  3. #3
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    you can just put an "end " in

    if a=1 then
    end
    else
    dosomething
    end if

    but such poorly structured code would, in my eyes, earn you your cowboy boots, hat and spurs.

+ 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