Results 1 to 14 of 14
  1. #1
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110

    Do While Endless Loop

    I use the following code to print labels to a Dymo label printer. When I step through it with F8 is works perfectly. When I click the command button to print the labels it prints an endless number of labels for the first record in the record set. Any ideas why this happens?



    DoCmd.OpenForm "frmLabelHalf"
    If Forms!frmLabelHalf.Recordset.EOF Then
    MsgBox ("There are no Std-Half WallTers on this order.")
    DoCmd.Close acForm, "frmLabelHalf"
    Exit Sub
    End If


    Dim NumLabels As Integer


    Do While IsNull(Forms!frmLabelHalf!Description) = False
    NumLabels = Forms!frmLabelHalf!QtyOrdered
    Do While NumLabels > 0
    DoCmd.OpenReport "rptLabelHalf"
    NumLabels = NumLabels - 1
    Loop
    On Error Resume Next
    DoCmd.GoToRecord , , acNext
    Loop


    DoCmd.Close acForm, "frmLabelHalf"

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Well the number appears to come from the form record?
    I would have thought you would also need to close the report each time?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    Click image for larger version. 

Name:	Label report.JPG 
Views:	25 
Size:	30.0 KB 
ID:	47895
    This is the report in design view. It prints the data in 2 fields from the first record. It prints the number of labels shown in the "QtyOrdered" field on the form, then the form moves to the next record and prints again, etc. until it has moved past the last record.

  4. #4
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    Double post. See next post.

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    While debugging, On Error Resume Next will hide errors. Comment that out and see what happens.
    Docmd.gotorecorrd,,acNext is obviously failing and you stay at the first record forever.

  6. #6
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    You don't specify the object type in the GoTo so maybe when you're stepping through it's the form (the code window can make the object related to the code the active object) whereas when you just run it, it's the report that's the active object.
    EDIT - forgot to mention that this sort of thing is better done with a recordset IMO. Then you don't have to keep hitting an object (the rs would be faster, albeit perhaps not so much that you'll notice), nor do you need to worry about which window/object is active.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    I removed On Error Resume Next and now I get run-time error 2105 You can't go to the specified record. I've tried it with the form in form view and in datasheet view. I also changed the query to a make table query and used the resulting table as the form's record source. I KNOW there is another record. I've used this same code with variations in other places with no issues.

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Just noticed Microns edit as I was about to post. I also wondered why you were moving through the form rather than the forms recordset.

    I would try something along the lines of
    Code:
        Dim rs As DAO.Recordset
        Dim i As Integer
    
    
        Set rs = Forms("frmLabelHalf").Recordset
    
    
        If rs.BOF And rs.EOF Then MsgBox "There are no Std-Half WallTers on this order.": GoTo MyExit
    
    
        rs.MoveFirst
    
    
        Do Until rs.EOF
    
    
            For i = 1 To rs.QtyOrdered
                DoCmd.OpenReport "rptLabelHalf"   'where clause?
            Next i
    
    
            rs.MoveNext
        Loop
    
    
    MyExit:
    
    
        Set rs = Nothing
    
    
        DoCmd.Close acForm, "frmLabelHalf"
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by cebrower View Post
    I've used this same code with variations in other places with no issues.
    As soon as you change something, you can introduce an error. The number of times I've read 'but it works elsewhere'
    Yes, but not as you have it now, or in the same circumstances.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  10. #10
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    Thanks moke123. That worked.

  11. #11
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    Oops, not quite solved. Using the code from moke123 when I try to compile it stops on the line "For i = 1 To rs.QtyOrdered" and I get the error message "Method or data member not found".

  12. #12
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,741
    if QtyOrdered is a field in the recordset, then it should be

    For i = 1 to rs!QtyOrdered

    Notice bang instead of pop.

  13. #13
    cebrower is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2018
    Location
    Spring Lake, MI
    Posts
    110
    Yup, that fixed it.

  14. #14
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    Oops. Aircode
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

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

Similar Threads

  1. Replies: 11
    Last Post: 08-08-2018, 11:23 AM
  2. Replies: 9
    Last Post: 03-07-2017, 02:49 PM
  3. Replies: 12
    Last Post: 06-05-2015, 04:27 PM
  4. Replies: 13
    Last Post: 08-20-2014, 09:17 AM
  5. Replies: 17
    Last Post: 04-07-2014, 07:48 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