【发布时间】:2015-01-31 01:53:17
【问题描述】:
这是用于创建 DAO 记录集的typical example:
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 的解决方案,我担心每次调用都会循环遍历该数组的资源。但我会试一试,做一些测试。
【问题讨论】: