Web Analytics Made Easy -
StatCounter Breaking out of a While Loop... - CodingForum

Announcement

Collapse
No announcement yet.

Breaking out of a While Loop...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Breaking out of a While Loop...

    Just wanted to ask a quick question on if this will work or not.

    I have a while not EOF look I use when reading through a recordset. The first thing I check is to see if a certain field has a certain value and if it does, I want to skip that record and start the while loop over again.

    Can I have a WEnd command in an IF statement to force the loop to break out early? So basically have a WEnd at the normal end of the loop but also embed one in the IF statement so it breaks starts the loop over early sometimes?

    If not, what's the best way to do something like this? Normally I could just skip the record and move on but there's alwas the chance of getting two next to each other and it would only skip one. And if one is the last record and I movenext and continue I could be EOF and then that'd kill the rest of the stuff.

    Just brainstorming this one but looking for advice.

    Thanks.

  • #2
    I'd do it like this:
    Code:
    While Not RS.EOF
      If [the current record doesn't need to be skipped] Then
        ' do what you have to
      End If
      RS.MoveNext
    Wend
    shmoove

    Comment


    • #3
      Yeah, see... I always come up with the difficult way of doing things first.... I knew I was overlooking something very obvious.

      heh heh.

      Thanks.

      Comment


      • #4
        The other way...

        ...what if you needed to terminate the loop to keep the current recordset position? In other words, in the If, I validate this IS the record I want - how do I terminate the loop?
        Brad Eck
        www.sitesdynamic.com

        Comment


        • #5
          You'd be better off just selecting the record you want in the first place!

          That's what SQL is for... or perhaps I'm not following your logic here...?
          Former ASP Forum Moderator - I'm back!

          If you can teach yourself how to learn, you can learn anything. ;)

          Comment


          • #6
            GOOD POINT - DOH!

            But, on a syntax understanding note, say it wasn't a recordset - how do I terminate the loop?
            Brad Eck
            www.sitesdynamic.com

            Comment


            • #7
              On a continued note, because I am actually needing info from all the records and then, later, a specific one, I do need to be able to terminate it in the loop of the RS. Any help is appreciated.
              Brad Eck
              www.sitesdynamic.com

              Comment


              • #8
                Use other loops such as Do Until...Loop, Do While...Loop, For...Next, For Each...Next and then use Exit Do or Exit For to break out of the loop. But if you really need to use While...Wend, then put additional terminating condition in the While statement.

                Example:
                Code:
                i=0
                bFound=false
                While i<10 and not bFound
                  if (some condition) then 
                     bFound=true
                  end if
                  i=i+1
                Wend
                Glenn
                vBulletin Mods That Rock!

                Comment


                • #9
                  That's what I did - just didn't know if VBScript provided a special keyword to break the loop. Thanks!
                  Brad Eck
                  www.sitesdynamic.com

                  Comment


                  • #10
                    I'm puzzled why there is no Exit While statement.
                    Glenn
                    vBulletin Mods That Rock!

                    Comment


                    • #11
                      Originally posted by glenngv
                      I'm puzzled why there is no Exit While statement.
                      I suspect that it's probably there for backwards compatibility, and so they (Microsoft's scripting developers) didn't provide a While...Wend exit statement, because they wanted to encourage the use of the newer loop constructs instead. And I think it's a good idea!
                      Marcus Tucker / www / blog
                      Web Analyst Programmer / Voted SPF "ASP Guru"

                      Comment


                      • #12
                        You mean they actually may have tried to promote things that are similar to other programming languages?!?

                        lol... these must be the guys that came up with C# then.
                        Former ASP Forum Moderator - I'm back!

                        If you can teach yourself how to learn, you can learn anything. ;)

                        Comment

                        Working...
                        X