Consulting

Results 1 to 8 of 8

Thread: Efficient: HasRecords?

  1. #1
    VBAX Regular
    Joined
    Jun 2007
    Posts
    67
    Location

    Efficient: HasRecords?

    Hi everyone?

    Does someone know an efficient way to find out if a table has records?

    I don't like that one since it might run slowly!

    [vba]qry= "SELECT Date_stock FROM KILN;"
    Set RS = CurrentDb().OpenRecordset(consulta)
    If RS.RecordCount > 0 Then
    HasRecords= True
    Else
    HasRecords= False
    End If[/vba]

  2. #2
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    [VBA]
    Dim HasRecords As Boolean

    HasRecords = (DCount("*", "[NameOfTable]") > 0)
    [/VBA]
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  3. #3
    VBAX Regular
    Joined
    Jun 2007
    Posts
    67
    Location

    In SQL

    Thanks, it's also possible in SQL,

    [vba] qry="SELECT Count([NameOfTable].Field) AS CountElements FROM [KILN];"
    Set RS = CurrentDb().OpenRecordset(qry)
    HasRecords=RS.Fields("CountElements")>0
    [/vba]

  4. #4
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    If you're going to do it with SQL, you can do it even more efficiently than that:

    [VBA]
    Dim rs As DAO.Recordset
    Dim HasRecords As Boolean

    Set rs = CurrentDb.OpenRecordset("SELECT Count(*) AS NumRec FROM SomeTable")
    HasRecords = (rs!NumRec > 0)
    rs.Close
    Set rs = Nothing
    [/VBA]
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  5. #5
    VBAX Regular
    Joined
    Jun 2007
    Posts
    67
    Location
    Cool!

    But why is it more efficient?
    Is it the use of * what makes it go faster?

    Thanks

  6. #6
    VBAX Master stanl's Avatar
    Joined
    Jan 2005
    Posts
    1,141
    Location
    Quote Originally Posted by kunguito
    Thanks, it's also possible in SQL,
    or

    [vba]
    Dim rs As DAO.Recordset
    Dim HasRecords As Boolean

    Set rs = CurrentDb.OpenRecordset("SELECT Date_stock FROM KILN;")
    HasRecords = rs.eof
    rs.Close
    Set rs = Nothing
    [/vba]

  7. #7
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    stanl,

    Touch?. Of course, we can make that better still:

    [VBA]
    Dim rs As DAO.Recordset
    Dim HasRecords As Boolean

    Set rs = CurrentDb.OpenRecordset("SELECT TOP 1 Date_stock FROM KILN;")
    HasRecords = (Not rs.eof)
    rs.Close
    Set rs = Nothing
    [/VBA]

    No need to fetch all the records, or even aggregate them (like I did), if we use your elegant suggestion
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

  8. #8
    VBAX Expert
    Joined
    Jul 2004
    Location
    Wilmington, DE
    Posts
    600
    Location
    Quote Originally Posted by kunguito
    Cool!

    But why is it more efficient?
    Is it the use of * what makes it go faster?

    Thanks
    My thought was that, if all we need is to see if there are any data at all, then there is no need to fetch all the records; one will do. My original SQL statement did a simple aggregation. My "SELECT TOP 1" suggestion should go even faster than that.

    Of course, if you actually do need all the data for some other purpose, then neither of my suggestions are worthwhile...
    Regards,

    Patrick

    I wept for myself because I had no PivotTable.

    Then I met a man who had no AutoFilter.

    Microsoft MVP for Excel, 2007 & 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •