Hello,
I have a script that goes through a RecordSet containing user details
and creates them in Active Directory.

So I have Main Loop something like

Do Until adoRecordset.EOF

<user creation statements, clauses, conditions>

adoRecordSet.MoveNext
Loop

Within this Main Loop there are lots of conditions/clauses IF Else...
Do While...Do Until etc

Is there a way if certain condition is not met or an error occurs I
can just skip that user and move down to the end to
adoRecordSet.MoveNext? I know I can use Exit Do, Exit IF etc but if I
say that I dont want the script to process any of the following
statements loops etc for that failed user and Exit to the main Do
Until adoRecordset.EOF Loop and move to the next RecordSet (user).

so exit to the last statement in the Main Loop (adoRecordSet.MoveNext)


I hope I've explained my question correctly :-)

Thanks in advance

Yas

Re: VBScript Exit Loops by ekkehard

ekkehard
Sun Jul 29 11:06:46 CDT 2007

Yas schrieb:
> Hello,
> I have a script that goes through a RecordSet containing user details
> and creates them in Active Directory.
>
> So I have Main Loop something like
>
> Do Until adoRecordset.EOF
>
> <user creation statements, clauses, conditions>
>
> adoRecordSet.MoveNext
> Loop
>
> Within this Main Loop there are lots of conditions/clauses IF Else...
> Do While...Do Until etc
>
> Is there a way if certain condition is not met or an error occurs I
> can just skip that user and move down to the end to
> adoRecordSet.MoveNext? I know I can use Exit Do, Exit IF etc but if I
> say that I dont want the script to process any of the following
> statements loops etc for that failed user and Exit to the main Do
> Until adoRecordset.EOF Loop and move to the next RecordSet (user).
>
> so exit to the last statement in the Main Loop (adoRecordSet.MoveNext)
>
>
> I hope I've explained my question correctly :-)
>
> Thanks in advance
>
> Yas
>
There is no GOTO <Label> in VBScript and you can't use Exit <Do|For>
if you have nested Loops of different types; so the best method is
to call a Sub or Function in your top level loop and Exit from that:

...
Do Until adoRecordset.EOF
doTheWorkForUser ...
adoRecordset.MoveNext
Loop
...

Sub doTheWorkForUser( ... )
...
Do Until ...
For Each ...
Do While ...
If <Panic> Then Exit Sub
Loop
Next
Loop
End Sub

Re: VBScript Exit Loops by Richard

Richard
Sun Jul 29 12:55:42 CDT 2007

Yas wrote:

> I have a script that goes through a RecordSet containing user details
> and creates them in Active Directory.
>
> So I have Main Loop something like
>
> Do Until adoRecordset.EOF
>
> <user creation statements, clauses, conditions>
>
> adoRecordSet.MoveNext
> Loop
>
> Within this Main Loop there are lots of conditions/clauses IF Else...
> Do While...Do Until etc
>
> Is there a way if certain condition is not met or an error occurs I
> can just skip that user and move down to the end to
> adoRecordSet.MoveNext? I know I can use Exit Do, Exit IF etc but if I
> say that I dont want the script to process any of the following
> statements loops etc for that failed user and Exit to the main Do
> Until adoRecordset.EOF Loop and move to the next RecordSet (user).
>
> so exit to the last statement in the Main Loop (adoRecordSet.MoveNext)
>

One solution is to nest the If Then statements, but that doesn't handle Do
Until loops well. Another solution is to call a subroutine in the recordset
loop. If any condition is wrong, simply exit the sub. Place all processing
for each user in the Sub. You would need to retrieve values from the
recordset before calling the sub. You can pass these values as parameters.
For example:
==============
Do Until adoRecordset.EOF
strVar1 = adoRecordset.Fields("Var1").Value
strVar2 = adoRecordset.Fields("Var2").Value
strVar3 = adoRecordset.Fields("Var3").Value
strVar4 = adoRecordset.Fields("Var4").Value
Call CreateUser(strVar1, strVar2, strVar3, strVar4)
adoRecordset.MoveNext
Loop
adoRecordset.Close

Sub CreateUser(strVar1, strVar2, strVar3, strVar4)
' Process values from recordset.
If (strVar1 = "") Then
' Necessary condition not met.
Exit Sub
End If
' More processing.
End Sub
===========
Or you could use a Function and return a non-zero value if an error is
encountered, a zero if no errors.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--



Re: VBScript Exit Loops by Anthony

Anthony
Sun Jul 29 13:36:58 CDT 2007

"ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
news:46acbb17$0$5714$9b4e6d93@newsspool2.arcor-online.net...
> Yas schrieb:
> > Hello,
> > I have a script that goes through a RecordSet containing user details
> > and creates them in Active Directory.
> >
> > So I have Main Loop something like
> >
> > Do Until adoRecordset.EOF
> >
> > <user creation statements, clauses, conditions>
> >
> > adoRecordSet.MoveNext
> > Loop
> >
> > Within this Main Loop there are lots of conditions/clauses IF Else...
> > Do While...Do Until etc
> >
> > Is there a way if certain condition is not met or an error occurs I
> > can just skip that user and move down to the end to
> > adoRecordSet.MoveNext? I know I can use Exit Do, Exit IF etc but if I
> > say that I dont want the script to process any of the following
> > statements loops etc for that failed user and Exit to the main Do
> > Until adoRecordset.EOF Loop and move to the next RecordSet (user).
> >
> > so exit to the last statement in the Main Loop (adoRecordSet.MoveNext)
> >
> >
> > I hope I've explained my question correctly :-)
> >
> > Thanks in advance
> >
> > Yas
> >
> There is no GOTO <Label> in VBScript and you can't use Exit <Do|For>
> if you have nested Loops of different types;

I'm not sure what you mean by that. Why can't you use Exit Do or Exit For
in a nested loops of different types?

--
Anthony Jones - MVP ASP/ASP.NET



Re: VBScript Exit Loops by ekkehard

ekkehard
Sun Jul 29 14:07:48 CDT 2007

Anthony Jones schrieb:
> "ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
> news:46acbb17$0$5714$9b4e6d93@newsspool2.arcor-online.net...
>> Yas schrieb:
>>> Hello,
>>> I have a script that goes through a RecordSet containing user details
>>> and creates them in Active Directory.
>>>
>>> So I have Main Loop something like
>>>
>>> Do Until adoRecordset.EOF
>>>
>>> <user creation statements, clauses, conditions>
>>>
>>> adoRecordSet.MoveNext
>>> Loop
>>>
>>> Within this Main Loop there are lots of conditions/clauses IF Else...
>>> Do While...Do Until etc
>>>
>>> Is there a way if certain condition is not met or an error occurs I
>>> can just skip that user and move down to the end to
>>> adoRecordSet.MoveNext? I know I can use Exit Do, Exit IF etc but if I
>>> say that I dont want the script to process any of the following
>>> statements loops etc for that failed user and Exit to the main Do
>>> Until adoRecordset.EOF Loop and move to the next RecordSet (user).
>>>
>>> so exit to the last statement in the Main Loop (adoRecordSet.MoveNext)
>>>
>>>
>>> I hope I've explained my question correctly :-)
>>>
>>> Thanks in advance
>>>
>>> Yas
>>>
>> There is no GOTO <Label> in VBScript and you can't use Exit <Do|For>
>> if you have nested Loops of different types;
>
> I'm not sure what you mean by that. Why can't you use Exit Do or Exit For
> in a nested loops of different types?
>
What I wanted to say is: If your outmost loop is of type X (Do|For) and you
use Exit X in your innermost loop, you'll have a problem if there is a loop
of type X in between:

For Each ...
Do While ..
Do until ...
Exit For ' ok
Loop
Loop
Next

but

For Each ...
Do While ..
For Each ...
Exit For ' not leaving the Do loop
Next
Loop
Next


Re: VBScript Exit Loops by ekkehard

ekkehard
Sun Jul 29 14:08:12 CDT 2007

Anthony Jones schrieb:
> "ekkehard.horner" <ekkehard.horner@arcor.de> wrote in message
> news:46acbb17$0$5714$9b4e6d93@newsspool2.arcor-online.net...
>> Yas schrieb:
>>> Hello,
>>> I have a script that goes through a RecordSet containing user details
>>> and creates them in Active Directory.
>>>
>>> So I have Main Loop something like
>>>
>>> Do Until adoRecordset.EOF
>>>
>>> <user creation statements, clauses, conditions>
>>>
>>> adoRecordSet.MoveNext
>>> Loop
>>>
>>> Within this Main Loop there are lots of conditions/clauses IF Else...
>>> Do While...Do Until etc
>>>
>>> Is there a way if certain condition is not met or an error occurs I
>>> can just skip that user and move down to the end to
>>> adoRecordSet.MoveNext? I know I can use Exit Do, Exit IF etc but if I
>>> say that I dont want the script to process any of the following
>>> statements loops etc for that failed user and Exit to the main Do
>>> Until adoRecordset.EOF Loop and move to the next RecordSet (user).
>>>
>>> so exit to the last statement in the Main Loop (adoRecordSet.MoveNext)
>>>
>>>
>>> I hope I've explained my question correctly :-)
>>>
>>> Thanks in advance
>>>
>>> Yas
>>>
>> There is no GOTO <Label> in VBScript and you can't use Exit <Do|For>
>> if you have nested Loops of different types;
>
> I'm not sure what you mean by that. Why can't you use Exit Do or Exit For
> in a nested loops of different types?
>
What I wanted to say is: If your outmost loop is of type X (Do|For) and you
use Exit X in your innermost loop, you'll have a problem if there is a loop
of type X in between:

For Each ...
Do While ..
Do until ...
Exit For ' ok
Loop
Loop
Next

but

For Each ...
Do While ..
For Each ...
Exit For ' not leaving the Do loop
Next
Loop
Next