在VBA编程中我试图理解记录集在将函数内的数据传递到另一个记录集时的状态
例如
Sub Test()
Dim Recordset1 as new ABODB.Recordset
Set RecordSet1 = BringDataFromRecordset2()
Do while not Recordset1.EOF
'data do something
Recordset1.movenext
Loop
End Sub
Function BringDataFromRecordset2() as ADODB.Recordset
dim RecordSet2 as new ADODB.Recorset
RecordSet2.Open "Select * from DUAL", Connectionstring
BringDataFromRecordset2 = RecordSet2
End Function
当RecordSet2将数据传递给“Set RecordSet1 = BringDataFromRecordset2()”中的RecordSet1时会发生什么?
它是否自动关闭?如果RecordSet2仍处于打开状态,我该如何关闭它?
答案 0 :(得分:2)
您编写的代码有几个问题。在您需要使用的功能
Set BringDataFromRecordset2 = RecordSet2
因为您的返回值是对象类型。
在Test()中,您不需要Recordset1声明中的New
,因为Function负责创建记录集,然后将其传递给Test。
Recordset2(或至少它指向的对象)即使在函数BringDataFromRecordset
完成后仍然在范围内,因为现在Test
中的Recordset1变量指向同一个对象。