Results 1 to 7 of 7
  1. #1
    yianni is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    May 2017
    Posts
    10

    Question VBA Loop Problem

    I am trying to loop through my table but when I run the program it starts on ID_fk 171 instead of 1 and I do not know why. Hope you can help!
    It starts on 171 and goes thru 361 which is the end.






    '--------------------------------




    Private Sub cmdADOlooping_Click()
    On Error GoTo ErrorHandler


    Dim strSQL As String
    Dim rs As New ADODB.Recordset


    'we will be opening our table
    strSQL = "SELECT UniqueCustomersWithPickups.ID_fk, UniqueCustomersWithPickups.PC, UniqueCustomersWithPickups.FirstVisit, UniqueCustomersWithPickups.ClientType FROM UniqueCustomersWithPickups ORDER BY UniqueCustomersWithPickups.ID_fk, UniqueCustomersWithPickups.PC;" 'define the SQL result that you want to loop

    rs.Open strSQL, CurrentProject.Connection, adOpenDynamic, adLockOptimistic


    With rs


    'Ensure recordset is populated
    If Not .BOF And Not .EOF Then



    'not necessary but good practice
    .MoveLast
    .MoveFirst
    Debug.Print rs.Fields("ID_fk") & " " & rs.Fields("PC") ' placed here to see if I am on first record in my table I Am!



    While (Not .EOF)
    'print info from fields to the immediate window

    Debug.Print rs.Fields("ID_fk") & " " & rs.Fields("PC")

    .MoveNext
    Wend

    End If
    .Close
    End With


    ExitSub:
    Set rs = Nothing
    Exit Sub
    ErrorHandler:
    Resume ExitSub
    End Sub

  2. #2
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,973
    Not sure why you are creating a recordset just so you can print the output to the immediate window.
    Why not just view this in the table or a query?

    Anyway, try removing the PC field from the ORDER BY
    Do you need the lock optimistic?

    Have you considered using a DAO recordset instead?
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,409
    ID_fk
    fk implies the field is a family (or foreign) key which would not be the PK for the field (though it could start from 1) - are you sure you are ordering by the right field?

  4. #4
    yianni is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    May 2017
    Posts
    10
    Yes it is the correct key. Suggestions did not work. Thanks anyway.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,892
    Are you sure the query returns the desired records? Did you test it in Access query object?

    Set a breakpoint on the first Debug. Step through the line so it executes. What is the value of ID_FK?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    John_G is offline VIP
    Windows 10 Access 2016
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    I am trying to loop through my table but when I run the program it starts on ID_fk 171 instead of 1 and I do not know why. Hope you can help!
    The immediate pane has only a limited number of lines that it will display at any one time, and it is not very many (about 200). Additional lines are scrolled upward and disappear.
    I think what is happenening is that the rows for ID_fk 1 - 170 are being written to the immediate pane, but are scrolling upward out of the display area, so you do not see them. You can see only the last 200 or so lines that were written.

  7. #7
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,973
    Cross posted with answers at https://www.access-programmers.co.uk...d.php?t=298732
    Please follow forum guidelines. Say you have cross posted and provide the link yourself
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

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

Similar Threads

  1. Replies: 9
    Last Post: 03-07-2017, 02:49 PM
  2. Replies: 5
    Last Post: 12-21-2013, 06:26 PM
  3. Do While Loop Problem
    By jhrBanker in forum Import/Export Data
    Replies: 8
    Last Post: 02-16-2012, 09:51 PM
  4. Loop Problem
    By JDPrestige in forum Forms
    Replies: 3
    Last Post: 11-14-2010, 06:48 PM
  5. Replies: 9
    Last Post: 04-28-2010, 11:20 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