Ms access 声明一个新的DAO.Recordset by(动态)名称

Ms access 声明一个新的DAO.Recordset by(动态)名称,ms-access,vba,dao,Ms Access,Vba,Dao,以下是用于创建DAO记录集的示例: Private Sub OpenOneRecordset() Dim dbExercise As DAO.Database Dim rsEmployees As DAO.Recordset Set dbExercise = CurrentDb Set rsEmployees = dbExercise.OpenRecordset("Employees") dbExercise.

以下是用于创建DAO记录集的示例:

Private Sub OpenOneRecordset()

        Dim dbExercise As DAO.Database
        Dim rsEmployees As DAO.Recordset

        Set dbExercise = CurrentDb
        Set rsEmployees = dbExercise.OpenRecordset("Employees")

        dbExercise.Close
        Set dbExercise = Nothing

End Sub
我想创建多个记录集,数组中的每个名称对应一个记录集:

Public Sub OpenAllRecordsets()

    Dim dbExercise As DAO.Database
    Set dbExercise = CurrentDb

    For Each varTable In arrTables

        strRecordsetName = "rst" & varTable

        ''Above is a string...
        ''How can I use the string to declare this object?

        ''  !!!!!!!!! OBVIOUSLY THIS WON'T WORK...
        Dim strRecordsetName As DAO.Recordset
        Set strRecordsetName = dbExercise.OpenRecordset(varTable)

    Next

    dbExercise.Close
    Set dbExercise = Nothing

End Sub
我看不出如何动态声明名称,然后使用它来创建记录集。我认为这类似于
TableDefs
,在这里我调用集合并添加一个成员。你认为呢

更新以下第一个发布的答案: 我在递归函数中使用这些记录集。它可以工作,但我想减少运行时间。我一直在为所需的新记录重新创建每个记录集

If nodeThis.hasChildNodes Then

    strTable = nodeThis.parentNode.nodeName

    Dim rsNewChild As DAO.Recordset         ' ***
    Set rsNewChild = cnn.OpenRecordset(strTable, dbOpenDynaset)  ' ***

    rsNewChild.AddNew

    '' ...populate fields
    For Each ...
        strName = nodeThis.nodeName            
        rsNewChild(strName) = nodeThis.Text
    Next

    rsNewChild.Update

    rsNewChild.close             ' ***
    Set rsNewChild = Nothing     ' ***

End If
但是我知道需要什么记录集,所以我宁愿一开始就打开它们,然后根据需要调用它们。这样可以删除标记为
***
的行。接下来的问题是如何使用字符串(在函数中可用)调用给定的记录集

为了更正确、更有用地重新声明目标:我需要获取一个字符串并使用它调用所需的记录集:

[ BASED ON STRING ].AddNew 

对于Barranka的解决方案,我关心的是在每个调用中循环通过该数组的资源。但我会尝试一下,做一些测试

我将创建一个记录集数组:

dim rs() as DAO.Recordset
' You must redim the array with something like: ReDim rs(1 to UBound(arrTables))
dim i as Integer
' ...
i = 1
for each varTable in arrTables
    Set rs(i) = dbExcercise.openRecordset(varTable)
    i = i + 1;
next varTable

正如
TableDefs
TableDef
对象的内置
集合
,您可以创建自己的
集合
记录集
对象,并按名称引用它们,如下所示:

Dim cdb作为DAO.Database,rst作为DAO.Recordset,myrecordset作为Collection
Dim testArray(1)作为字符串,tblName作为变量
"测试数据,
testArray(0)=“人”
testArray(1)=“其他人”
'创建集合
Set myrecordset=新集合
设置cdb=CurrentDb
对于testArray中的每个tblName
Set rst=cdb.OpenRecordset(tblName,dbOpenTable)
我的记录集。添加rst,tblName
设置rst=无
下一个
'使用集合的成员
调试。打印MyRecordset(“人员”).Fields(“LastName”).Value
我的记录集(“人”)。下一步
调试。打印MyRecordset(“人员”).Fields(“LastName”).Value
调试。打印我的记录集(“其他人”).Fields(“LastName”).Value
Set myrecordset=Nothing
设置cdb=无
(请注意,
集合
对象的
.Add
方法的参数是
值、键
,这与添加新条目时
字典
和其他关联数组对其参数排序的方式相反。)

编辑回复:问题更新

这也行

添加新记录 MyRecordset(“其他人”).AddNew MyRecordset(“OtherPeople”).Fields(“LastName”).Value=“NewPerson” 我的记录集(“其他人”)。更新
听起来不错。为了将记录添加到特定的记录集中,我是否必须遍历所有成员并对照
rs(I).name
?我很确定这是VBA数组通常需要的…但是你知道一个快捷方式吗?这是一个运行速度问题。我有很多写操作。如果我可以独立地定义一个记录集,而不是运行一个循环来获得所需的记录集,这似乎更有效。而不是一个数组,考虑一个集合或字典来保存以表名作为关键字的记录集。然后您可以通过表名键直接引用任何特定的记录集。。。我自己从来没有用过,但这似乎是个好主意。你能找到一种方法让Intellisense在
MyRecordset(“人”)之后显示记录集方法和属性吗?
?@HansUp在VBA下我不认为Intellisense是一个选项,因为VBA
集合
是任意对象的集合。恐怕没有办法像在VB.NET中那样键入值(或键),我们可以使用
Dim MyRecordset作为新字典(字符串、记录集)
。接收read-to-go测试代码是一种特权。复制、粘贴、运行。@HansUp,通过编写自定义集合类,可以让Intellisense显示底层对象的成员。此类必须是
集合
对象的包装器,在文本编辑器中进行操作以允许默认成员,在这种情况下,集合的
属性重新转换为
记录集
对象。不过这很麻烦!