Use a string to refer to a defined recordset object (1 Viewer)

Bilbo_Baggins_Esq

Registered User.
Local time
Today, 08:50
Joined
Jul 5, 2007
Messages
586
Is there I can use a built string to refer to an existing recordset object?

For example, I build a string "rs_Char4"
I need to be able to associate that string with an existing recordset object (.OpenRecordset, .MoveNext, .Close, etc)

I've built a function which has a parameter "rsToManage as Recordset"
But I can't seem to use my built strings for that parameter when I call that function.
The function works FINE when I manually type the defined recordset's name.
But I need to have a way to use the strings.

I thought about building an extra function which evaluates as a recordset, with a long select case statement to read the strings and pass a recordset as the function's result.

But I can't help but think there's got to be a more elegant solution.
 

Minty

AWF VIP
Local time
Today, 14:50
Joined
Jul 26, 2013
Messages
10,371
Have a read of this thread
 

Bilbo_Baggins_Esq

Registered User.
Local time
Today, 08:50
Joined
Jul 5, 2007
Messages
586
Have a read of this thread
Thanks but no, Eval doesn't work for this.
Eval only returns strings or numbers, not objects.

To clarify (sometimes I suck at explaining)

I have several recordsets declared as public: rs_Char1, rs_Char2, rsChar3, etc.
I have a counter that runs 1 through 12.

I obviously can easily build the strings but I need to be able to efficiently connect that string value to the recordset object so I can take actions with the recordset(s).
 

June7

AWF VIP
Local time
Today, 05:50
Joined
Mar 9, 2014
Messages
5,472
Can use string variable to refer to form and report objects by referencing with collection. Example: Forms(strForm). AFAIK, there is not a Recordsets collection. I just tested.
 

Josef P.

Well-known member
Local time
Today, 15:50
Joined
Feb 2, 2023
Messages
826
You can store recordset references in a collection or dictionary and assign a name to it.
Maybe an array is enough.

But why do you want something like that?
 

Josef P.

Well-known member
Local time
Today, 15:50
Joined
Feb 2, 2023
Messages
826
Code:
Dim RsCollection as Collection
set RsCollection = new Collection
RsCollection.Add YourRecordsetReference, YourKey

Call:
set rs = RsCollection(YourKey)

Array:
Code:
dim RsArray(1 to 12) as DAO.Recordset
dim i as long
set rs(1) = db.Openrecordset(...)
...
for i = 1 to 12
    debug.print RsArray(i).RecordCount
next
 

Bilbo_Baggins_Esq

Registered User.
Local time
Today, 08:50
Joined
Jul 5, 2007
Messages
586
Code:
Dim RsCollection as Collection
set RsCollection = new Collection
RsCollection.Add YourRecordsetReference, YourKey

Call:
set rs = RsCollection(YourKey)
I assume "YourKey" would be the string (or number) by which I want to call it?
 

Josef P.

Well-known member
Local time
Today, 15:50
Joined
Feb 2, 2023
Messages
826
I assume "YourKey" would be the string (or number) by which I want to call it?
Key = a string ... not a number.
You can also retrieve a collection entry via the index.

Code:
RsCollection.Add YourRecordsetReference  ' possible without key
...

Call:
set rs = RsCollection(1)
' or
for each rs in RsCollection
..
next

 

Bilbo_Baggins_Esq

Registered User.
Local time
Today, 08:50
Joined
Jul 5, 2007
Messages
586
Key = a string ... not a number.
You can also retrieve a collection entry via the index.

Code:
RsCollection.Add YourRecordsetReference  ' possible without key
...

Call:
set rs = RsCollection(1)
' or
for each rs in RsCollection
..
next

Hmmm, I can't seem to get this to work

Code:
Public obj_Acc_WS As Workspace
Public obj_Acc_DB As Database
Public rs_Char1 As Recordset
Public rs_Char2 As Recordset
Public rs_Char3 As Recordset
Public rs_Char4 As Recordset
Public rs_Char5 As Recordset
Public rs_Char6 As Recordset
Public rs_Char7 As Recordset
Public rs_Char8 As Recordset
Public rs_Char9 As Recordset
Public rs_Char10 As Recordset
Public rs_Char11 As Recordset
Public rs_Char12 As Recordset
Public rsCollection as Collection
Public str_RS as string

Sub TestingRsCollection()

Set obj_Acc_WS = DBEngine.Workspaces(0)
Set obj_Acc_DB = obj_Acc_WS.Databases(0)

Set rsCollection = New Collection

rsCollection.Add rs_Char1, "rs_Char1"
rsCollection.Add rs_Char2, "rs_Char2"
rsCollection.Add rs_Char3, "rs_Char3"
rsCollection.Add rs_Char4, "rs_Char4"
rsCollection.Add rs_Char5, "rs_Char5"
rsCollection.Add rs_Char6, "rs_Char6"
rsCollection.Add rs_Char7, "rs_Char7"
rsCollection.Add rs_Char8, "rs_Char8"
rsCollection.Add rs_Char9, "rs_Char9"
rsCollection.Add rs_Char10, "rs_Char10"
rsCollection.Add rs_Char11, "rs_Char11"
rsCollection.Add rs_Char12, "rs_Char12"

Debug.Print rsCollection.Count 'returns 12 correctly

str_RS = "rs_Char12"

'this below line throws "Object variable or With block variable not set" error.
Set rsCollection.Item(str_RS) = obj_Acc_DB.OpenRecordset("Files", dbOpenDynaset)

End Sub
 
Last edited:

June7

AWF VIP
Local time
Today, 05:50
Joined
Mar 9, 2014
Messages
5,472
AFAIK, have to Add a recordset object that was already declared and set, not Add empty recordset object and then Set it.
 

Bilbo_Baggins_Esq

Registered User.
Local time
Today, 08:50
Joined
Jul 5, 2007
Messages
586
AFAIK, have to Add a recordset object that was already declared and set, not Add empty recordset object and then Set it.
Yeah, I seem to have just figured that out as you were typing the above.

I've decided to go ahead and open all 12 recordsets up front, then add them to the collection.
I may be able to figure out a process to add the recordsets to the collection (once declared and set) but I need to clear my head for now.

THANKS!
 

June7

AWF VIP
Local time
Today, 05:50
Joined
Mar 9, 2014
Messages
5,472
Collection must be a global object available from any procedure so as to reference its elements. In which case can manipulate it from anywhere to add and remove elements.
 

Josef P.

Well-known member
Local time
Today, 15:50
Joined
Feb 2, 2023
Messages
826
If you briefly describe what you actually want to do, we might find a suitable approach together.
Maybe you want to manage these recordsets in a class.
 
Last edited:

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:50
Joined
Feb 28, 2001
Messages
27,186
This SOUNDS like you are trying to index a group of open recordsets by name to perform a type of "pointer" logic. There is already a Recordsets collection that occurs naturally as Workspace(n1).Database(n2).Recordsets(n3) but I don't think you can index that collection by name. On the other hand, you surely could try it. NOTE: ONLY returns already-opened recordsets. Doesn't know and doesn't care about closed ones.

 

June7

AWF VIP
Local time
Today, 05:50
Joined
Mar 9, 2014
Messages
5,472
Okay, learned something new. There is a Recordsets collection but have to use database object prefix. I tried some code but can't get it to work.
Can't figure out Workspace syntax.

CurrentDb.Recordsets shows up in intellisense but when I try to reference collection item I get "does not exist" error.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 08:50
Joined
Feb 28, 2001
Messages
27,186
For what it is worth, CurrentDB provides a "snapshot" of Workspace(0).Databases(0) - and you might have had a problem referencing it if you went through Workspace(0) because there the reference is actually Databases ( 0 ) ... a PLURAL whereas I inadvertently typed it as though it were singular. In that context, it is not. My bad. (But it IS late here in my time zone. Anything I type after midnight local time should be suspect.)

Not going to swear to it, but I think this collection (which is only the OPEN recordsets) could be used to help in a catastrophic cleanup error trap handler that was going to "gracefully" shut down a database.
 

Josef P.

Well-known member
Local time
Today, 15:50
Joined
Feb 2, 2023
Messages
826
[a little OT]
Database.Recordsets works only with the Database instance that was used to create the recordsets.

Code:
Private Sub TestDatabaseRecordsets()

   Dim dbA As DAO.Database
   Dim dbB As DAO.Database
   Dim rs(1 To 3) As DAO.Recordset
   Dim i As Long

   Set dbA = CurrentDb
   Set dbB = CurrentDb ' new instance!

   Set rs(1) = dbA.OpenRecordset("select * from TestTable1")
   Set rs(2) = dbA.OpenRecordset("select * from TestTable2")

   Set rs(3) = dbB.OpenRecordset("select * from TestTable1 where 1=0")

   Debug.Print "dbA", dbA.Recordsets.Count, dbA.Recordsets(0).Name, dbA.Recordsets("select * from TestTable2").Name
            ' dbA.Recordsets("select * from TestTable2") ... call by name as key value
   Debug.Print "dbB", dbB.Recordsets.Count, dbB.Recordsets(0).Name
   Debug.Print "CurrentDb", CurrentDb.Recordsets.Count
   Debug.Print "DBEngine", DBEngine(0)(0).Recordsets.Count

   For i = 1 To 3
      rs(i).Close
   Next

   Debug.Print "db(rs closed)", dbA.Recordsets.Count, dbB.Recordsets.Count

End Sub
output:
Code:
dbA            2            select * from TestTable1    select * from TestTable2
dbB            1            select * from TestTable1 where 1=0
CurrentDb      0
DBEngine       0
db(rs closed)  0             0


[back to topic]
I suspect that using an array instead of the many Recordset variables might be a suitable variant for the OP.

replace:
Code:
Public rs_Char1 As Recordset
Public rs_Char2 As Recordset
...
Public rs_Char12 As Recordset

with
Code:
Pubic rs_CharArray(1 to 12) as DAO.Recordset
(I find the public declaration questionable.)

But without knowing the intended use, I'm not going to keep guessing. ;)
Riddle for 'Char' in rs_Char1 - rs_Char12: What could be a character recordset?
 
Last edited:

Users who are viewing this thread

Top Bottom