Results 1 to 9 of 9
  1. #1
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154

    checking if recordset is empty

    I use the following piece of code to check if my recordset is empty:

    Code:
    If rs.RecordCount > 0 Then
        MissingLineIds = True
    Else
        MissingLineIds = False
    End If
    But the problem is that even though my recordset should theoretically be empty, it somehow satisfies the first if condition (i.e. rs.RecordCount > 0).


    And when I do debug.print rs.fields(0) it prints 0.

    So I am beginning to wonder that if you want to check if a recordset is empty it should be recordcount > 1 ??

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,369
    "debug.print rs.fields(0) it prints 0". Then in the table, that field is a number field with a default of 0 and there is in fact a record? Because you are examining the value in a field with that statement. If the output is "0" then you have that as a value in that field somewhere. Examine all the fields in the recordset or run the query it's based on and check that. Also possible that you allow zero length ("") in a field and have a record with one or more fields with zls as a value. You can always run a query on those fields where myField = "" and see how many records you get.
    The logical conclusion is that you have at least one record, regardless of what you think you're seeing.
    Last edited by Micron; 03-27-2019 at 10:16 AM. Reason: clarification
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    I'm seeing this when I replicate what my SQL query is trying to do:

    Click image for larger version. 

Name:	Capture.PNG 
Views:	10 
Size:	12.2 KB 
ID:	37948

    Just a blank record.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    I agree that if the debug is printing 0, there is a record being returned. My test would be:

    If Not rs.EOF Then
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,369
    remove the filters and see if anything changes.

  6. #6
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    no I need the filters there.

  7. #7
    mp3909 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2018
    Posts
    154
    silly me, my sql query is a count!!
    even if there are no records, the result will be a count of 0, right?

  8. #8
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    Would you mind sharing the line of code where you set the recordset object?

    EDIT: It depends on how the SQL statement is written. However you have it written now, a record is being returned with 0 being the value of the first column of the first record.
    Last edited by thebigthing313; 03-27-2019 at 10:49 AM. Reason: OP posted before I finished posting

  9. #9
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,369
    even if there are no records, the result will be a count of 0, right?
    Ja, your result is a calculation, hence query returns 1 record. If you really need to know when there's no actual data returned, you'd have to set the comparison to > 1 but I think it would be best to alter the query to return only where some count is greater than 0 if possible.

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

Similar Threads

  1. Recordset empty. Why?
    By john134 in forum Programming
    Replies: 3
    Last Post: 02-08-2017, 05:39 PM
  2. Checking Empty comboboxes
    By dylcon in forum Forms
    Replies: 8
    Last Post: 06-17-2013, 01:52 PM
  3. Error on AddNew to an empty recordset
    By kowalski in forum Programming
    Replies: 7
    Last Post: 05-22-2013, 06:17 PM
  4. Replies: 3
    Last Post: 03-06-2013, 03:52 PM
  5. Access 2003 returns empty recordset
    By Leelers in forum Queries
    Replies: 0
    Last Post: 03-20-2009, 11:11 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