#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# STOOOPPP! No, not THAT!

Member Posts: 274
Hi,

I have a program with subs leading to subs, if I 'exit sub', then it will just go back to the one it came from won't it?
I want the code to stop, but the form to stay on screen and all.
what code is it?

Ta

----------------------------------------------------
The solution to the problem changes the problem

• Member Posts: 597
A simple solution could be to convert your [blue]Sub[/blue]s to [blue]Function[/blue]s returning a [blue]Boolean[/blue] value; make them return [blue]True[/blue] to signal success, or [blue]False[/blue] to signal failure. On failure, the calling function should return failure itself and [blue]Exit Function[/blue]; and so on.

The following code should help you get the idea. Func1 calls Func2, Func2 calls Func3, Func3 calls some other functions.

[code]
[blue]Function[/blue] Func1() [blue]As Boolean
If[/blue] Func2() = [blue]False Then[/blue]
Func1 = [blue]False[/blue] [green]' Let's return failure.[/green]
[blue]Exit Function
End If[/blue]
[green]'...[/green]
Func1 = [blue]True[/blue] [green]' No failure is a success.[/green]
[blue]End Function[/blue]

[blue]Function[/blue] Func2() [blue]As Boolean
If[/blue] Func3() = [blue]False Then[/blue]
Func2 = [blue]False[/blue] [green]' Let's return failure.[/green]
[blue]Exit Function
End If[/blue]
[green]'...[/green]
Func2 = [blue]True[/blue] [green]' No failure is a success.[/green]
[blue]End Function[/blue]

[blue]Function[/blue] Func3() [blue]As Boolean
If[/blue] ... [blue]Then[/blue] [green]' Suppose an error occurrs here.[/green]
Func3 = [blue]False[/blue] [green]' Let's return failure.[/green]
[blue]Exit Function
End If[/blue]
[green]'...[/green]
Func3 = [blue]True[/blue] [green]' No failure is a success.[/green]
[blue]End Function[/blue]
[/code]
________________
[size=1][b][grey]Cold[/grey][blue]Shine[/blue][/b][/size]

• Member Posts: 828
: A simple solution could be to convert your [blue]Sub[/blue]s to [blue]Function[/blue]s returning a [blue]Boolean[/blue] value; make them return [blue]True[/blue] to signal success, or [blue]False[/blue] to signal failure. On failure, the calling function should return failure itself and [blue]Exit Function[/blue]; and so on.
:
: The following code should help you get the idea. Func1 calls Func2, Func2 calls Func3, Func3 calls some other functions.
:
: [code]
: [blue]Function[/blue] Func1() [blue]As Boolean
: If[/blue] Func2() = [blue]False Then[/blue]
: Func1 = [blue]False[/blue] [green]' Let's return failure.[/green]
: [blue]Exit Function
: End If[/blue]
: [green]'...[/green]
: Func1 = [blue]True[/blue] [green]' No failure is a success.[/green]
: [blue]End Function[/blue]
:
: [blue]Function[/blue] Func2() [blue]As Boolean
: If[/blue] Func3() = [blue]False Then[/blue]
: Func2 = [blue]False[/blue] [green]' Let's return failure.[/green]
: [blue]Exit Function
: End If[/blue]
: [green]'...[/green]
: Func2 = [blue]True[/blue] [green]' No failure is a success.[/green]
: [blue]End Function[/blue]
:
: [blue]Function[/blue] Func3() [blue]As Boolean
: If[/blue] ... [blue]Then[/blue] [green]' Suppose an error occurrs here.[/green]
: Func3 = [blue]False[/blue] [green]' Let's return failure.[/green]
: [blue]Exit Function
: End If[/blue]
: [green]'...[/green]
: Func3 = [blue]True[/blue] [green]' No failure is a success.[/green]
: [blue]End Function[/blue]
: [/code]
: ________________
: [size=1][b][grey]Cold[/grey][blue]Shine[/blue][/b][/size]
:
:

Another method would be to raise an error Err.Raise .... and handle the error from the calling sub, or the sub that called that sub

For example: Sub1 calls sub2 which calls sub3:

[code]Public Sub Sub1()
On Error GoTo Finish:
Sub2
MsgBox "Sub Went through OK"
Exit Sub
Finish:
MsgBox "Sub Failed"
End Sub

Public Sub Sub2()
Sub3
End Sub

Public Sub Sub3()
If (.....) Then

Else
Err.Raise 4000, ........... 'Can't remember the order in which the arguments are in here.
End If
End Sub[/code]

• USAMember Posts: 0

_________ < http://forcoder.org /> free video tutorials and ebooks about // Scratch Visual Basic C# Visual Basic .NET R Java C++ Python Ruby JavaScript Objective-C Assembly PL/SQL MATLAB C Delphi Swift Perl Go PHP FoxPro D Alice LabVIEW Logo ML Rust Clojure F# Crystal Awk ABAP Ada Lua Fortran Erlang SAS Prolog Dart Scala Apex VBScript Transact-SQL Julia Lisp Kotlin Scheme Hack COBOL Bash //