Results 1 to 6 of 6
  1. #1
    AccessGeek is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    19

    Testing Recordset for Records

    I have long used .recordcount to test a recordset for records before doing some sort of processing.



    For example:

    If rst.recordcount = 0 then
    exit sub
    else
    Do some process
    End If

    This usually does the trick but I am wondering....is there another method I might try to see if a recordset contains records?

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    how about dcount()? one line of code, DONE.

  3. #3
    AccessGeek is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    19
    Yes...I love that idea!
    I just tested it and it does the job perfectly!
    Thanks!

  4. #4
    SteveH2508 is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Sep 2010
    Location
    Chelsea, London, England
    Posts
    117
    I believe that DCount can be slow.

    I use If rst.EOF Then

    If it is True then there are no records.

  5. #5
    AccessGeek is offline Novice
    Windows XP Access 2007
    Join Date
    Dec 2010
    Posts
    19
    I have seen rst.eof (and rst.bof) used as a test and when I test it does work. However. if there are no records, I get the "No current record" error. Do you leave that as your out message to the user or do you trap that so you can custom a message?

  6. #6
    SteveF is offline Generally AccessAble
    Windows XP Access 2010 32bit
    Join Date
    Nov 2010
    Location
    Fourth Corner
    Posts
    123
    Quote Originally Posted by AccessGeek View Post
    I have long used .recordcount to test a recordset for records before doing some sort of processing.

    For example:

    If rst.recordcount = 0 then
    exit sub
    else
    Do some process
    End If

    This usually does the trick but I am wondering....is there another method I might try to see if a recordset contains records?
    Here's an alternative using a recordset:

    If rst.BOF = True And rst.EOF = True then
    exit sub
    else
    Do some process
    End If

    You must test for both conditions; the recordset is empty only if both BOF and EOF are true at the same time. I tend to prefer this over rst.recordcount because the recordcount only really returns an accurate count if you execute a .MoveLast first. If there are no records it returns an error; the .BOF/.EOF test works regardless.

    Also, if you only need to test for the presence of records, this works well:

    If Not(rst.BOF = True And rst.EOF = True) then
    Do some process
    End If

    Steve

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

Similar Threads

  1. Testing Upsizing Wizard with FreeSQL.org?
    By pretzelz in forum Access
    Replies: 2
    Last Post: 03-15-2011, 01:58 PM
  2. Testing Task - Forms
    By collwill in forum Access
    Replies: 1
    Last Post: 03-09-2011, 10:25 PM
  3. Replies: 3
    Last Post: 11-08-2010, 11:09 AM
  4. select records in recordset
    By sksriharsha in forum Programming
    Replies: 3
    Last Post: 09-05-2009, 11:40 AM
  5. Testing links
    By piflechien73 in forum Forms
    Replies: 0
    Last Post: 06-01-2009, 05:09 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