Results 1 to 6 of 6
  1. #1
    behnam is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    72

    How to loop through every record in query called "estimates"

    Hi,


    I found the following code online and it allows me to loop through each record based on the results of a query and make changes to the field which is exactly what i want to do.

    Code:
     Dim rs As DAO.Recordset
    Dim db As Database
    Dim strSQL as String
    
    Set db=CurrentDB
    
    strSQL = "select * from table where some condition"
    
    Set rs = db.OpenRecordset(strSQL)
    
    Do While Not rs.EOF
    
        rs.Edit
        rs!SomeField = "Abc"
        rs!OtherField = 2
        rs!ADate = Date()
        rs.Update
    
        rs.MoveNext
    Loop
    how can i define strSql to a query builder query I created. Thanks.

  2. #2
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    Instead of using SQL code in a string, you can open your query directly:

    Code:
    Dim rs As DAO.Recordset
    Dim db As Database
    
    Set db = CurrentDB
    
    Set rs = db.OpenRecordset("nameofqueryincludingquotes", dbOpenDynaset)
    
    Do While Not rs.EOF
        rs.Edit
        rs!SomeField = "Abc"
        rs!OtherField = 2
        rs!ADate = Date()
        rs.Update
        rs.MoveNext
    Loop

  3. #3
    behnam is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    72
    Code:
    Set rs = db.OpenRecordset("Redeployed-Estimate", dbOpenDynaset)
    I get an error message on this line saying, to few parameters, expected 1.
    Do you have any idea why?

    thanks

  4. #4
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    Does your query have any parameters? As in, does it request for user input before running the query?

    Also, is there a specific reason you are doing this in VBA? I just read the code in the Do...Loop and it looks like something you can do with an Update Query.

  5. #5
    behnam is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    72
    it dosent request for input, it however uses the field in a textbox for comparison.

  6. #6
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    I haven't tried something like this before, but in any case, if a saved query requires data from a form control, that form needs to be open for the query to run, or else, the query wouldn't know where to get the information, and you usually would get prompted for them when you run the query. Did you try running the VBA code while your form was open?

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

Similar Threads

  1. Replies: 2
    Last Post: 10-10-2012, 02:51 PM
  2. Replies: 4
    Last Post: 08-17-2012, 05:27 PM
  3. Replies: 4
    Last Post: 04-18-2012, 12:09 PM
  4. Replies: 4
    Last Post: 01-06-2011, 10:52 AM
  5. Replies: 0
    Last Post: 05-04-2010, 06:39 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