Do while loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Do while loop

  1. #1
    Join Date
    Feb 2014
    Posts
    5

    Question Do while loop

    Can anyone please help me with a Do while Not loop.
    I'm very new to programming:

    I need to check and print a list, and limit it to 5 names.

    Do While Not rsP.EOF And iCount > 5
    sLT = sLT & rsP![Name] & vbNewLine
    iCount = iCount + 1
    rsP.MoveNext
    Loop

    If rsP.RecordCount > 5 Then
    sLT = sLT & "..."
    End If

    right now it will not print the list, if I take out iCount > 5, it prints, but I need to limit it to only 5 names maximum to print?
    Thanks

  2. #2
    Join Date
    Feb 2014
    Posts
    5

    Re: Do while loop

    The issue is with iCount > 5 , I just need to understand how can I get it to show if the sLT is less than 5 and if its over 5 than limits to show 5 names as maximum

  3. #3
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,987

    Re: Do while loop

    Separate it:

    Code:
    Do While Not rsP.EOF 
      sLT = sLT & rsP![Name] & vbNewLine
        iCount = iCount + 1
        rsP.MoveNext
        If rsP.RecordCount Mod 5 Then ' every 5th time only
           sLT = sLT & "..."
        End If
    Loop
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Do while loop

    Quote Originally Posted by salman afzal View Post
    The issue is with iCount > 5 , I just need to understand how can I get it to show if the sLT is less than 5 and if its over 5 than limits to show 5 names as maximum
    read your statement out loud...

    DO while NOT EOF and icount GREATER than 5..... erm... two choises here... not EOF as first and greater than 5 as second...

    you want : DO while NOT EOF and NOT icount GREATER than 5

    all your really missing is the brackets...

    Code:
    Do While Not (rsP.EOF And iCount > 5)
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,987

    Re: Do while loop

    Kind of confusing, still. I like my way.
    Plus, he wanted to print 5 per line, then a break, right?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,845

    Re: Do while loop

    Another option would be to test only the eof condition in the do while portion and then use an exit do when icount reaches 5
    Always use [code][/code] tags when posting code.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center