使用 With...End 在使用 DAO 的 Ms Access 中打开记录集时

Using With...End when opening recordsets in MsAccess using DAO

根据这个answer,一旦变量超出范围,对象的资源就会被VB自动释放。问题:如果我在打开 DAO.Recordset 时使用 With...End 可以吗,因为当 Sub 超出范围时应该清理资源,或者我是否仍需要明确 Close 并将对象设置为 Nothing?

例如,

Sub Test()
   With CurrentDb.OpenRecordset("SELECT * FROM Table1", dbOpenForwardOnly)
      IsThisAGoodPractice()
   End With
End Sub

should I still need to explicitly Close and set objects to Nothing?

With 块中,您没有引用记录集的对象变量,因此无法设置它 = Nothing。在 End With 之后,记录集消失了,所以您也不能在那里设置它 = Nothing --- 但它已经消失了,所以那将毫无意义。

关于它的 .Close 方法,我会把它称为包含 .Close 方法的任何对象。但是无论您是否调用它,VBA 仍将处理 End With 处的记录集。