Results 1 to 6 of 6
  1. #1
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53

    Receiving Error that there is "Loop Without Do" WHY?!


    Here is the basic code. When I remove the Do Loop it works but when I try to put it in, the "Loop without Do" Error comes up. I have also tried using For-Next and the same kind of error occurs "Next without For".


    There are actually two "loops". One inside the other. The first loop is for the entire INVOICE whose data is being entered into the database using a Form. These are different items, each with it's own SKU. The second loop is for the quantity of the item. There are times when I update a table, that I create multiple records using the same data (other than the autonumber index for the DB) as I want an individual record created for each item of inventory. And I don't see any other way of writing the data from the form to the database other than this way.


    Code:
    QTY = 1 
    
    For ListOfItemsIndex = 1 to TotalNumberofItems 
    
      Do
    
        If QTY = TOTALQTY then Exit Do 
    
         With RECORDSET
    
         .AddNew
    
        !BarCode = BarCode(ListOfItemsIndex)
        !Brand = Brand(ListOfItemsIndex)
        !Cost = Cost!(ListOfItemsIndex)
        !Price = Price!(ListOfItemsIndex)
    
        .update
    
        QTY = QTY + 1
    
      Loop
    
    Next ListOfItemsIndex
    Why can't I use addnew/update inside a Do-Loop or For-Next Loop? When I rem out the recordset and .addnew and .update lines then there isn't an issue. But I need those loops. What solutions are there to accomplish adding these records (there can be up to 200 items and usually 1-10 of each item being added so up to 2000 individual records) to the database?

    Thanks again!

    Ken L .

  2. #2
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,413
    A bit misleading. There appears to be an End With missing

    Edit: I wasn't paying enough attention to Orange. Spot on.

  4. #4
    kleaverjr is offline Advanced Beginner
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2022
    Posts
    53
    Quote Originally Posted by orange View Post
    **HEADDESK** I have been staring at that code for 1.5 days and the answer was right there in front of me!!! I feel so foolish!

    :-(

    Ken L.

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Proper indentation will go a long way to you being able to keep more of your hair. Whether or not you start indenting after Sub() line, what comes after can help you spot mistakes before you even try to compile. Yours adapted:
    Code:
    For ListOfItemsIndex = 1 to TotalNumberofItems 
      Do
        If QTY = TOTALQTY then Exit Do 
        With RECORDSET
          .AddNew
          !BarCode = BarCode(ListOfItemsIndex)
          !Brand = Brand(ListOfItemsIndex)
          !Cost = Cost!(ListOfItemsIndex)
          !Price = Price!(ListOfItemsIndex)
          .update
          QTY = QTY + 1
      Loop
    Next ListOfItemsIndex
    Even if you didn't now know what the issue is, it should be obvious that one element of a pair is missing.
    EDIT - OK maybe not if you didn't know With / End With is a pair.
    Last edited by Micron; 08-25-2023 at 06:51 AM.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    orange's Avatar
    orange is online now Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Ken L.
    Treat it as a learning moment. Don't punish yourself. We've all been there.
    It's nice, as Micron alludes, to look for paired statements. But it isn't so obvious if you don't have some sort of colour coding or intimate knowledge of the statement/construct involved. Also, Loop without Do isn't so obvious either -it's really suggesting that there is some "missing element" preventing the Do and Loop to be processed. You'll get similar "confusing" messages when you omit a necessary End If etc.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 04-08-2021, 09:15 AM
  2. Replies: 1
    Last Post: 07-29-2019, 09:06 AM
  3. Replies: 3
    Last Post: 06-06-2018, 08:26 PM
  4. Replies: 13
    Last Post: 12-12-2016, 12:26 AM
  5. Suppress "Error" message following "Cancel = True"
    By GraeagleBill in forum Programming
    Replies: 7
    Last Post: 03-23-2014, 05:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums