Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Stopping Code

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nigel - 20 Jan 2006 14:39 GMT
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
Edward Ulle - 20 Jan 2006 15:53 GMT
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
tony h - 20 Jan 2006 16:39 GMT
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.

Signature

tony h

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.