# Thread: Breaking out of a loop in a function

1. ## Breaking out of a loop in a function

Hi

I am using a For Next loop in an Action, to call 5 functions per data table row.

//Code...
<font color="green">
For i = 1 to LastRow
datatable.SetCurrentRow i

'Call functions
A
B
C
D
E

Next
</font>

I need to be able to break out of the loop in certain conditions - e.g if I get to function B and the creation fails, there is no value in running functions C, D &amp; E for that iteration, so I want to break out and process the next row of data.

I tried the "Exit For" code within the function, but get error "Description: Invalid 'exit' statement".

What is the correct way to break out of the function and the loop?

Thanks

Steve

2. ## Re: Breaking out of a loop in a function

Something like this...

//Code...
Function A()
A = "FAIL"
End Function

For i = 1 to LastRow
datatable.SetCurrentRow i

'Call functions
ReturnA = A

If ReturnA = "FAIL" Then
Exit For
End If

B
C
D
E

Next

3. ## Re: Breaking out of a loop in a function

I would do something similar to Rajkumar I would use a flag that can be set in B,C,D,E and interrogated between the calls to those functions, something like:

errFlag = False
For i = 1 to LastRow
datatable.SetCurrentRow i

'Call functions
Call A(errFalg)
If Not errFlag Then
Call B(errFlag)
End If
If Not errFlag Then
Call C(errFlag)
End If
If Not errFlag Then
Call D(errFlag)
End If
If Not errFlag Then
Call E(errFlag)
Else
Exit For
End If
Next

Function B(errFlag)
.... Do Function Process
.... If there's an error Then
errFlag = True
End If
End Function

Mark Smith.

4. ## Re: Breaking out of a loop in a function

If your using a do..while type loop you can exit this too.

do while true
'do something
if test="FAIL" then
exit do
end if
'something else
loop

Handy if you dont know how many times it will loop.

5. ## Re: Breaking out of a loop in a function

Ok thanks guys, I'll try these out!

Steve

#### Posting Permissions

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

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.