Results 1 to 6 of 6
  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727

    looping thru query


    All, using access 2010. I have some code to loop thru a table in a module. The following is a snippet:
    Code:
    Set rstCusts = CurrentDb().OpenRecordset("EA01_qryCust_email")
                        With rstCusts
                            ' Get the total e-mail count.
                            .MoveLast
                            intNumEmailsToCreate = Nz(.RecordCount, 0)
                            .MoveFirst
    This code loops thru a table in my database, but the table has records and it causes duplication. I would like to instead use the query which I thought I was using but it still pulls from the table. How do I get the loop thru the query "EA01_qryCust_email" instead?
    Thanks

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    ??? Your code seems to indicate you want the recordcount of the recordset.

    Your post says you want to loop through the recordset.
    Generic loop:
    Code:
     dim db as dao.database
    dim rs as dao.recordset
    set db = currentdb
    set rs = db.openrecordset ("your table or query name")
    Do while not rs.eof
        ------------------------
        --do something here
        ------------------------
    rs.movenext
    Loop

    I'm surprised that you have 500+ post and haven't programmed a Do -Loop with a recordset???
    Did you try Google?

    Good luck

  3. #3
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I appreciate your reply. I have and this is what I have now. I was just asking is there a way to loop thru the query and not the table which this seems to be doing in conjunction with the query. I thought maybe something like:

    Code:
    Set qdf = db.QueryDef
    or something.

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    What I posted was a method of looping thru a recordset. The recordset could be based on a Table or a Query (which Access treats as a "table").

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Don't have to loop through a recordset just to get the record count.

    Since the code references the query as source for recordset, that is what is being looped.
    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
    Snayjay is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Mar 2014
    Location
    Gulf of Mexico
    Posts
    10

    DCount...

    If all you want is a record count, can't you just do a DCOUNT on the primary key of the table in question?

    Code:
    Dim x
    x = DCount("[PRIMARYKEY_FIELD]","TABLE_NAME")
    And if you only want to count certain records, use the WHERE part of the DCount.

    Code:
    Dim x
    x=DCount("[PRIMARYKEY_FIELD]","TABLE_NAME","[TEXTFIELD] = '" & SEARCHWORD & "'")
    Or if you have a Query made already, just change the Table Name to the query.

    Code:
    Dim x
    x=DCount("[PRIMARYKEY_FIELD]","QUERY_NAME")
    Hope that helps.

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

Similar Threads

  1. Replies: 12
    Last Post: 05-05-2014, 09:23 PM
  2. Looping through fields of a record (query or table)
    By PlamenGo in forum Programming
    Replies: 2
    Last Post: 04-02-2014, 05:44 AM
  3. Looping through results of a query to run a report
    By peter.nelson in forum Access
    Replies: 1
    Last Post: 05-29-2013, 04:31 PM
  4. Looping
    By ddrew in forum Forms
    Replies: 8
    Last Post: 10-08-2012, 01:48 AM
  5. Looping query
    By jaykappy in forum Queries
    Replies: 13
    Last Post: 02-24-2012, 03:05 PM

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