Help error with loop!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Help error with loop!

  1. #1
    Join Date
    Feb 2010
    Posts
    5

    Help error with loop!

    For j = lvOrderList.ListItems.Count To 1 Step -1

    If rsProducts!Product_ID = lvOrderList.ListItems(j).Text Then
    Do
    orderQnty = lvOrderList.ListItems(j).ListSubItems(2).Text
    rsProducts.Edit
    rsProducts!Units_InStock = rsProducts!Units_InStock + orderQnty
    rsProducts.Update

    Loop Until rsProducts.EOF
    End If
    Next j

    next without for error

    the unit in stock should be updated with the data in listview(orderQnty). the productID from listview should first match with ProductID in database.
    should then move on to next listview and update...

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,959

    Re: Help error with loop!

    You can't subtract 1 from 0, the last time thru your loop.

    Either add one to the counter, and subtract in code, or the opposite
    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!

  3. #3
    Join Date
    Feb 2010
    Posts
    5

    Re: Help error with loop!

    could you explain please

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,959

    Re: Help error with loop!

    Notice that you didn't get the FIRST item?




    Code:
    For j = lvOrderList.ListItems.Count-1 To 0 Step -1
    
      If rsProducts!Product_ID = lvOrderList.ListItems(j).Text Then
        Do
          orderQnty = lvOrderList.ListItems(j).ListSubItems(2).Text
          rsProducts.Edit
          rsProducts!Units_InStock = rsProducts!Units_InStock + orderQnty
          rsProducts.Update
    Loop Until rsProducts.EOF
    End If
    Next j
    Last edited by dglienna; February 24th, 2011 at 12:19 AM. Reason: Actually that should be count -1 to 0
    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!

  5. #5
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: Help error with loop!

    Actually that should be count -1 to 0

    Code:
    Do
    orderQnty = lvOrderList.ListItems(j).ListSubItems(2).Text
    rsProducts.Edit
    rsProducts!Units_InStock = rsProducts!Units_InStock + orderQnty
    rsProducts.Update
    
    Loop Until rsProducts.EOF
    Why the loop here?

    It would appear that this would be endless as you are looping until EOF but not moving the record pointer???

    Looks liek you are only needing to update one record and if so no loop is needed. If you want to update more than one record or even if you only want to update a single record and update where query would be a better choice.
    Last edited by DataMiser; February 23rd, 2011 at 01:33 PM.
    Always use [code][/code] tags when posting code.

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,959

    Re: Help error with loop!

    Didn't catch that. Correct.
    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!

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center