Do Loop in Access Code Runs to Infinity

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bterri50
    New Member
    • May 2014
    • 5

    #1

    Do Loop in Access Code Runs to Infinity

    I have inherited someone else's database and process. There is a report that would not work. The problem lies in the code but I do not know enough to fix it.

    I turned on all Warnings to help me determine where the problem is. The queries loop continuously as expected but when 0 rows are returned the loop needs to stop.

    I have determined this because if I hit No when it asks to return 0 records the report appears in perfect condition. Can someone help me with the EscapeEndlessLo op that they appear to be using but is not working.

    Code:
    Private Sub cmdPrintLabels_Click()
    
    DoCmd.SetWarnings True
    DoCmd.OpenQuery "qdClearLabelQuantities"
    DoCmd.OpenQuery "qdClearLabels"
    DoCmd.OpenQuery "qaLabelQuantity"
    DoCmd.SetWarnings True
    
    Dim cmd As ADODB.Command
    Dim rst As ADODB.Recordset
    Dim prm As ADODB.Parameter
    
    Set cmd = New ADODB.Command
    Set cmd.ActiveConnection = CurrentProject.Connection
    cmd.CommandText = "qsLabelQuantityNotZeroNew"
    cmd.CommandType = adCmdTable
    
    cmd.Parameters.Refresh
    For Each prm In cmd.Parameters
        prm.Value = Eval(prm.Name)
    Next prm
    
    Set rst = cmd.Execute
    
    DoCmd.SetWarnings True
    
    Do
        DoCmd.OpenQuery "qaPartNumberLabels"
        DoCmd.OpenQuery "quQuantityMinusOneNew"
        On Error GoTo EscapeEndlessLoop
        rst.MoveFirst
    Loop
    
    EscapeEndlessLoop:
    rst.Close
    Set rst = Nothing
    Set cmd = Nothing
    
    DoCmd.SetWarnings True
    
    DoCmd.OpenReport "rlPartNumbers", acViewPreview
    
    End Sub
    Last edited by Rabbit; May 4 '14, 06:50 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    No error is thrown when a query has no records. Instead, loop until the query returns no records by using a DCount on the query.

    Comment

    • bterri50
      New Member
      • May 2014
      • 5

      #3
      Thank you for responding but I don't really know how to write code. What would that DCount look like and where within the Loop command would I put it?

      Thanks in advance for any assistance.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Since you don't know how to write code, you will probably want to learn how to before taking on a project like this.

        Here is a link to a tutorial: http://www.functionx.com/vbaccess/Lesson01.htm

        And a link to the DCount documentation: http://office.microsoft.com/en-us/ac...001228817.aspx

        Comment

        • bterri50
          New Member
          • May 2014
          • 5

          #5
          Thank you. I was told it was just a simple Access database or I would have turned it down. Thanks again for your help. I think I'll go take a VBA class ASAP.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Once you have some of the basics down, let us know if you run into trouble with the DCount. You can post the code you used and any error messages you get.

            Comment

            Working...