Results 1 to 14 of 14
  1. #1
    AndyRob1973 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2020
    Posts
    81

    OpenRecordset with select query not looping

    Hello,

    I am able to loop through a table called 'tblStudents' with the code below. Using OpenRecordset("tblStudents"). This works ok.

    Click image for larger version. 

Name:	Capture1.PNG 
Views:	36 
Size:	12.2 KB 
ID:	48608

    However ultimately, I want to use a query OpenRecordset("SELECT * FROM tblStudents") like below, but this doesn't loop through in the same way as above. I have seen it done this way in examples online. The reason is that I want to add criteria to the query once i get it working.



    Click image for larger version. 

Name:	Capture2.PNG 
Views:	33 
Size:	12.8 KB 
ID:	48609


    I've tried declaring a variable sqlstring as string and using OpenRecordset(sqlstring), but this doesn't work either.



    Could anyone help?

    Thankyou.

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You probably need to specify an method of opening the record set, try

    Code:
    Set rs = OpenRecordset("SELECT * FROM tblStudents" , dbOpenDynaset)
    

    Also go back to using your Sql string variable, it's much easier to read and debug.

    And I would declare the type of recordset;

    Code:
    Dim  rs  As DAO.Recordset
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,446
    what does 'but this doesn't loop through in the same way as above' mean

    and why loop through your recordset when you can update specific record with an update query?

    for looping you might be better using .eof.

    For the future please copy/paste your code and use the code tags to preserve indentation. I'm not going to waste time retyping your code - so my example is

    Code:
    while not rs.EOF
        'do something
        rs.movenext
    wend

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,007
    Do not bother using record counts, just use EOF.
    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
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    5,007
    I would also just use a query where last name = 'Ramos' ?
    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

  6. #6
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Agreed with CJ. I could be wrong but if i remember correctly I don't think a recorset's record count property is reliable until one has actually moved to the end of the recordset.

    To expand on CJ's code here is my template (typing this from memory without any intellisense, sorry in advance for typos)
    Code:
    On Error GoTo ErrHandler
        
        Dim db as DAO.Database
        Dim rs as DAO.Recordset
    
        Set db = CurrentDb
        Set rs = db.OpenRecordset("MY_QUERY", dbOpenSnapshot)    'Use dbOpenDynaset if you may need to edit the values within the recordset
    
        If not (rs.BOF and rs.EOF) Then    'Make sure the recordset has records otherwise we'll get errors if we try to move through the recordset
            rs.MoveFirst    'this line may not be necessary but couldn't hurt
            Do While not rs.EOF   'begin looping through the recordset
                'Do Work Here
                    
                rs.MoveNext    'Don't forget to move to the next record in the recordset or you'll get stuck in an infinite loop
            Loop
        Else
            'Code to run if the recordset is empty goes here
    
        End If
        rs.close
    
    ExitHandler:
        Set db = Nothing    ' Clean up all of the objects that we previously set
        Set rs = Nothing
    
        Exit Sub
    
    ErrHandler:
        MsgBox err.description, , "Error #" & err.Number
        Resume ExitHandler
    Please review http://allenbrowne.com/ser-29.html
    Last edited by kd2017; 08-31-2022 at 07:27 PM.

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,446
    but if i remember correctly I don't think a recorset's record count property is reliable until one has actually moved to the end of the recordset.
    True - perhaps that is what the OP meant by 'this doesn't loop through in the same way '

  8. #8
    AndyRob1973 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2020
    Posts
    81
    Quote Originally Posted by Minty View Post
    You probably need to specify an method of opening the record set, try

    Code:
    Set rs = OpenRecordset("SELECT * FROM tblStudents" , dbOpenDynaset)
    

    Also go back to using your Sql string variable, it's much easier to read and debug.

    And I would declare the type of recordset;

    Code:
    Dim  rs  As DAO.Recordset
    Thank you Minty - i shall try that.

  9. #9
    AndyRob1973 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2020
    Posts
    81
    Quote Originally Posted by CJ_London View Post
    what does 'but this doesn't loop through in the same way as above' mean

    and why loop through your recordset when you can update specific record with an update query?

    for looping you might be better using .eof.


    For the future please copy/paste your code and use the code tags to preserve indentation. I'm not going to waste time retyping your code - so my example is

    Code:
    while not rs.EOF
        'do something
        rs.movenext
    wend

    Thank you CJ_London. What I was trying to say is that when i put a select statement it doesn't seem to loop through the records at all, whereas when i just put the table name it does.

    At the moment I'm practicing with looping through recordsets so yes in this example i could just do an update. But ultimately I want to update a field in one record based on a field in the previous record.

    Also thank you for your point on pasting code. I'll take that on board.

  10. #10
    AndyRob1973 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2020
    Posts
    81
    Quote Originally Posted by Welshgasman View Post
    I would also just use a query where last name = 'Ramos' ?
    Thanks Welshgasman. Normally i would use an update query but i'm trying to get to grips and practicing with looping through recordsets before moving onto a job where I'll be using a value in a field in a previous row to update a row in the current record. I'll try your EOF suggestion too.

  11. #11
    AndyRob1973 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2020
    Posts
    81
    Quote Originally Posted by kd2017 View Post
    Agreed with CJ. I could be wrong but if i remember correctly I don't think a recorset's record count property is reliable until one has actually moved to the end of the recordset.

    To expand on CJ's code here is my template (typing this from memory without any intellisense, sorry in advance for typos)
    Code:
    On Error GoTo ErrHandler
        
        Dim db as DAO.Database
        Dim rs as DAO.Recordset
    
        Set db = CurrentDb
        Set rs = db.OpenRecordset("MY_QUERY", dbOpenSnapshot)    'Use dbOpenDynaset if you may need to edit the values within the recordset
    
        If not (rs.BOF and rs.EOF) Then    'Make sure the recordset has records otherwise we'll get errors if we try to move through the recordset
            rs.MoveFirst    'this line may not be necessary but couldn't hurt
            Do While not rs.EOF   'begin looping through the recordset
                'Do Work Here
                    
                rs.MoveNext    'Don't forget to move to the next record in the recordset or you'll get stuck in an infinite loop
            Loop
        Else
            'Code to run if the recordset is empty goes here
    
        End If
        rs.close
    
    ExitHandler:
        Set db = Nothing    ' Clean up all of the objects that we previously set
        Set rs = Nothing
    
        Exit Sub
    
    ErrHandler:
        MsgBox err.description, , "Error #" & err.Number
        Resume ExitHandler
    Please review http://allenbrowne.com/ser-29.html

    Thankyou kd2017

  12. #12
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,446
    have you checked out post #6? Edit - crossed in posting

    But ultimately I want to update a field in one record based on a field in the previous record.
    You are aware there is no concept of Previous, Next, First or Last in databases unless you specify an order?

    Sounds suspiciously like you are attempted to store a calculated value such as a running sum which is not recommended - all sorts of potential issues can arise. Might help if you explained what you are trying to do and why - we may be able to suggest a better way

  13. #13
    AndyRob1973 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2020
    Posts
    81
    Quote Originally Posted by CJ_London View Post
    have you checked out post #6? Edit - crossed in posting

    You are aware there is no concept of Previous, Next, First or Last in databases unless you specify an order?

    Sounds suspiciously like you are attempted to store a calculated value such as a running sum which is not recommended - all sorts of potential issues can arise. Might help if you explained what you are trying to do and why - we may be able to suggest a better way


    I’ve attached an access dbase with a table in that I’m hoping helps to illustrate what I’m trying to do.

    Click image for larger version. 

Name:	Capture111.PNG 
Views:	15 
Size:	34.3 KB 
ID:	48644


    Each person on each course has a series of appointments. I would like to be able to put the date in for their first appointment and then the rest of the dates calculate using the ‘Days to next appointment’. Obviously this calculation needs to stop at their last appointment and a new calculation starts for the next person eg below.



    Click image for larger version. 

Name:	Capture112.PNG 
Views:	15 
Size:	42.0 KB 
ID:	48645


    I am aware that access doesn’t have a concept of order, so I was hoping that would be taken care of with an order by in the select statement in the OpenRecordset.

    Also, I think that there needs to be a loop for each distinct Course/Name combination, within a loop that goes through all the records.
    My previous attempts and posts were me trying to start simply and get to grips with the concept and then move on.
    Does this make sense?

    Thankyou

    Andy

  14. #14
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,046
    Hi,

    this seems an attempt to search the slowest and most difficult way to update/manage data in Access. I assume you have a table with courses, one with students and then a third with appointments. Why not, on the form where you manage the appointments, put a button "new appointment" that looks up the previous appointment of that person and uses the days-to_next appointment to automatically calculate the date for the next appointment and creates that new appointment for that person?

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

Similar Threads

  1. OpenRecordset with Query error
    By scor1pion in forum Modules
    Replies: 22
    Last Post: 01-29-2021, 06:58 AM
  2. Replies: 1
    Last Post: 09-26-2018, 11:00 AM
  3. Replies: 4
    Last Post: 01-06-2016, 10:32 AM
  4. Looping through a multi select listbox - how do I do it?
    By shabbaranks in forum Programming
    Replies: 4
    Last Post: 04-02-2012, 11:56 AM
  5. OpenRecordset vs. Query
    By crispy-bacon in forum Programming
    Replies: 7
    Last Post: 07-04-2011, 09:52 AM

Tags for this Thread

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