How-to Check If A RecordSet Is Open Or Closed?

I was helping out in a forum where someone asked the question of:

… check to determine the the recordset is closed or still open.

Now, in their specific case, the real issue was logic and reorganizing the code would entirely eliminate this need, but I still wanted to share a simple solution for anyone else out there that wanted to do the same thing.
 

A Property Validation

The simplest solution to determining if a RecordSet is opened or closed is to try to use a RecordSet property. If you can successfully exploit a property, then it is open, but if it generates and error, then the RecordSet is closed.

Armed with this idea, we can then translate it into a simple function like:

Function IsRecordsetOpen(ByVal oRs As DAO.Recordset) As Boolean
    Dim lRecCounter           As Long

    On Error Resume Next
    lRecCounter = oRs.RecordCount 'Try to use the RecordSet property
    IsRecordsetOpen = (Err.Number = 0)
    On Error GoTo 0
End Function

So now if ever you find yourself needing to check if a RecordSet is still open or not you can test it with a single line of code, like:

    Dim oDb                   As DAO.Database
    Dim oRs                   As DAO.Recordset

    Set oDb = CurrentDb
    Set oRs = oDb.OpenRecordset("...")

    ' Your Code, do things, close the RecordSet if you would like, ...
    '...
    'oRs.Close

    ' Check and see if the RecordSet is still open before doing other things
    If IsRecordsetOpen(oRs) Then
        ' The RecordSet is Open
        Debug.Print "The RecordSet is Open"
    Else
        ' The RecordSet is Closed
        Debug.Print "The RecordSet is Closed"
    End If

So there you have it, a simple solution for validating whether a RecordSet is open, or not.