0

I have a function in which a recordset is opened using module level declared variable rst. The variable is not closed on the completion of the function as it is used by another sub. It may be possible that before closing of the variable and setting it to nothing, the same function may also be called again which sets rst to new recordset causing the previously open recordset to become unavailable for use in the aforementioned sub.

Is it possible (and if yes then how) to store/add the recordset into collection or dictionary so that it can be used later on even if function is called multiple time. I tried using collection method and the recordset is stored into it successfully but can't be recalled or used. I used in the following manner:

Private m_ColRS as collection

In the Function Used:

If m_ColRS Is Nothing Then Set m_ColRS = New Collection

m_ColRS.Add rst, sTableName

where rs is the declared variable for recordset and sTableName is variable table name for opening recordset.

In the Add Watch window, it shows the collection containing added recordsets but I don't know how to use the recordset from the collection and how to close/set to Nothing on completion of the purpose of opening it.

Best Regards

3
  • Open recordsets are already in a collection. If you want to set rst to a new set of data but still be able to use the original set, then that is two recordsets, not one. You use a recordset from your custom collection by setting another recordset variable: Set rs = m_ColRS(sTableName).
    – June7
    Commented Mar 18, 2023 at 4:58
  • @June7, Thanks for reply. How do I close the recordset in my custom collection.
    – Pacman
    Commented Mar 18, 2023 at 6:01
  • 1
    I've never used custom collections. I think it and its elements should be destroyed automatically when db closes. However, in my test, I simply closed and set rst to nothing same as normal. Then it was no longer in the custom collection.
    – June7
    Commented Mar 18, 2023 at 6:59

1 Answer 1

0

You can, but as soon as the close the original recordset, so will it be in the collection.

To "keep" it, use the Clone method:

Public Sub TestCollection()

    Dim rst         As DAO.Recordset
    Dim rstColl     As DAO.Recordset
    
    Dim sTableName  As String
    
    sTableName = "Table1"
    Set rst = CurrentDb.OpenRecordset(sTableName)

    If m_ColRS Is Nothing Then Set m_ColRS = New Collection
    m_ColRS.Add rst, sTableName
    
    ' Set rstColl = m_ColRS(sTableName)
    Set rstColl = m_ColRS(sTableName).Clone
    
    Debug.Print "rst", rst.RecordCount
    Debug.Print "rstColl", rstColl.RecordCount
    
    rst.Close
    
    ' Will fail
    ' Debug.Print "rst", rst.RecordCount
    ' Will fail if method Clone has not been used.
    Debug.Print "rstColl", rstColl.RecordCount
       
End Sub

It might be simpler to hold the recordset in a public or static variable.

2
  • Hi Gustav, thanks for replying. The whole purpose of using collection is to use single function to use multiple times and fetch close required rst. But if I have to use new rs declarations for each recordset then there is no point of using collection. Then I think, as June7 mentioned, I have to declare new RS each time and not to use collection.
    – Pacman
    Commented Mar 20, 2023 at 2:45
  • Hmm ... I don't follow, sorry. I have to use new rs declarations for each recordset - yes, but that is always the case, putting the recordset in a collection or not. I have to declare new RS each time - no, only if you close it and it wasn't a clone.
    – Gustav
    Commented Mar 20, 2023 at 7:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.