Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,367

    Loop Issue


    Hi Guy's frustrating this one as I have plenty of Recordset loops going on and for some reason I am sure I have got the sequence correct here but coming up with Compile Error Loop With out Do

    I do Have a Do, a bit like ABBA, I do I do I Do I Do I Do have a Do

    Code:
            Set rs = CurrentDb.OpenRecordset("Select * From tblEdit WHERE Status = '" & strStatus & "' And Source = '" & strSrc & "' And Customer = '" & strCustomer & "' ORDER BY DelTo;")
    
            Do While Not rs.EOF
                If rs.Fields("Status") = "Delivery" Then
                    dtDelDate = DLookup("DeliveryDate", "tblAssign", "[Status] = '" & strStatus & "'")
                    strDriver = DLookup("Driver", "tblAssign", "[Status] = '" & strStatus & "'")
                    strVehicle = DLookup("Vehicle", "tblAssign", "[Status] = '" & strStatus & "'")
                    strBody = strBody & rs.Fields("DelTo") & " - " & rs.Fields("Town") & " - " & rs.Fields("PostCode") & " - " & rs.Fields("LiftType") & " - " & _
                rs.Fields("SONumber") & " - " & rs.Fields("LiftNo") & " - " & rs.Fields("Status") & "Delivered: " & Format(dtDelDate, "ddd-dd-mmm-yyyy") & " Driver: " & strDriver & " Vehicle: " & strVehicle & "|"
                End If
                
                If rs.Fields("Status") = "Collection" Then
                    dtDelDate = DLookup("CollectedDate", "tblCollections", "[Status] = '" & strStatus & "'")
                    srtBody = strBody & rs.Fields("DelTo") & " - " & rs.Fields("Town") & " - " & rs.Fields("PostCode") & " - " & rs.Fields("LiftType") & " - " & _
                    rs.Fields("SONumber") & " - " & rs.Fields("LiftNo") & " - " & rs.Fields("Status") & "Collected: " & Format(dtDelDate, "ddd-dd-mmm-yyyy") & "|"
                End If
                
                If rs.Fields("Status") <> "Delivery" Then
                    If rs.Fields("Status") <> " Collection" Then
                    srtBody = srtBody & rs.Fields("DelTo") & " - " & rs.Fields("Town") & " - " & rs.Fields("PostCode") & " - " & rs.Fields("LiftType") & " - " & _
                    rs.Fields("SONumber") & " - " & rs.Fields("LiftNo") & " - " & rs.Fields("Status") & "|"
                End If
                
            rs.MoveNext
            Loop
    
    
            Debug.Print rs

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,938
    use

    Code:
    While Not rs.EOF
        ....
         ....
    
    Wend
    or it could be due to an incomplete loop in other parts of your code which you have not shown

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    You're missing an 'End If' in the last 'If'

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,567
    Quote Originally Posted by davegri View Post
    You're missing an 'End If' in the last 'If'
    Not that I could see?
    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

  5. #5
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    Quote Originally Posted by Welshgasman View Post
    Not that I could see?
    Third paragraph has 2 IFs and one End If

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,567
    Quote Originally Posted by davegri View Post
    Third paragraph has 2 IFs and one End If
    Aha, nice catch. Missed that completely , as did the O/P

    That is what can happen when you do not indent correctly , hard to spot errors like that.
    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

  7. #7
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,742
    Quote Originally Posted by Welshgasman View Post
    Aha, nice catch. Missed that completely , as did the O/P

    That is what can happen when you do not indent correctly , hard to spot errors like that.
    I like 'Select Case' instead of 'If's to avoid such confusion - much easier for me to visualize.

  8. #8
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,938
    good spot - agree a case statement would be simpler

  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    Saw the issue right away. This is because of improper indentation IMO which should have made it obvious - that and the error message almost always means that some sort of code block (With, IF, etc.) has not been terminated.
    Code:
    Do ...
       If rs.Fields("Status") <> "Delivery" Then  
         If rs.Fields("Status") <> " Collection" Then
           srtBody = srtBody & rs.Fields("DelTo") & " - " & rs.Fields("Town") & " - " & rs.Fields("PostCode") & " - " & rs.Fields("LiftType") & " - " & _
           rs.Fields("SONumber") & " - " & rs.Fields("LiftNo") & " - " & rs.Fields("Status") & "|"
         End If
         rs.MoveNext << line was improperly indented
       Should now be more obvious that End If is missing here <<
    Loop
    @DMTDave - HTH
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  10. #10
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,367
    Thank you guy's, will now look at the woods through trees, sometimes you cant see it,

    Select case would be something like ?

    Select Case rs.Fields("Delivery")
    strBody = strBody etc
    Do OtherStuff
    End Select

    Select Case rs.Fields("Collection")
    strBody = strBody etc
    Do OtherStuff
    End Select


    ?

  11. #11
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,367
    Micron, yes sometimes it isn't the exact procedure that pops up

    ie: i have noticed many a time if a "If" is missing, a compile error can highlight loop as the error and not the fact that fat fingers Dave can miss the "End If"

  12. #12
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    You'd think that a missing If where there is an End If or a missing End If inside of any block (doesn't have to be a loop) would highlight the missing component. Instead, it "breaks" the hierarchy of one of the outer blocks. If memory serves, this would also raise an error about a missing End With:

    Code:
    With something
      do this with it
      do that with it
      If this Then
        do something
    End With
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  13. #13
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,567
    Quote Originally Posted by DMT Dave View Post
    Thank you guy's, will now look at the woods through trees, sometimes you cant see it,

    Select case would be something like ?

    Select Case rs.Fields("Delivery")
    strBody = strBody etc
    Do OtherStuff
    End Select

    Select Case rs.Fields("Collection")
    strBody = strBody etc
    Do OtherStuff
    End Select


    ?
    No.
    You do not have fields by that name.
    You would Select Case on Status field
    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

  14. #14
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,424
    Missing the Case statements there?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  15. #15
    DMT Dave is online now VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,367
    OK thank you all who has input opinions, always greatly appreciated

    Did miss the end If which cured the problem, of course i know can't open if you open something you have to close it or or starting loops has to loop etc....

    Last question while i have used both in the past as CJ London suggested

    Code:
    While Not rs.EOF    ....
         ....
     Wend
    Is this the same or is Wend more reliable than Loop or does it depend on what your code is doing (looking for) (looping) (Cases) (If's) etc...

    Code:
     Do Until rs.EOF
    do stuff
    Loop
    So i guess i am asking Wend vs Loop ?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Loop issue
    By onlylonely in forum Programming
    Replies: 35
    Last Post: 07-18-2017, 07:48 PM
  2. Replies: 13
    Last Post: 06-06-2017, 01:43 PM
  3. Replies: 9
    Last Post: 03-07-2017, 02:49 PM
  4. Loop issue
    By dragon_sun252 in forum Programming
    Replies: 25
    Last Post: 04-29-2012, 12:27 AM
  5. Issue with while loop and Access 2007 datasheet
    By jermaine123 in forum Programming
    Replies: 2
    Last Post: 01-17-2010, 10:09 AM

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