exiiting sub是否发布了记录集? [英] Does exiiting a sub release a recordset?
问题描述
在Sub或Function中,我打开一个记录集。 如果我不自行关闭,Access Access会在Exit Sub上清理吗?
In a Sub or Function, I open a recordset. Will Access clean things up upon Exit Sub if I don't close it myself?
如果我有一个"On Error GoTo XXX"我在哪里输出Err.Message然后输出Sub。 如果Access没有使用Exit Sub清理,我怎样才能确定是否关闭了一个记录集变量而没有触发On Error?
If I have an "On Error GoTo XXX" where I output the Err.Message and then Exit Sub. If Access doesn't clean things up with Exit Sub, how can I determine if a recordset variable is closed without triggering an On Error?
我是否需要添加其他丑陋的代码,如:
Will I have to add additional ugly code like:
Dim rsOpenFlag as Boolean
Dim rsOpenFlag as Boolean
Dim rs as Recordset
Dim rs as Recordset
On Error GoTo ErrorHere
On Error GoTo ErrorHere
设置rs = CurrentDb.OpenRecordset(" Datafile",dbOpenDynaset)
Set rs = CurrentDb.OpenRecordset("Datafile", dbOpenDynaset)
rsOpenFlag = True
rsOpenFlag= True
< blah ...>
<blah...>
rs.Close
rs.Close
rsOpenFlag = False
rsOpenFlag= False
退出子
ErrorHere:
ErrorHere:
MsgBox(Err.Message)
MsgBox(Err.Message)
if(rsOpenFlag = true)然后
if (rsOpenFlag = true) Then
rs.Close
rs.Close
结束如果
结束子
推荐答案
这取决于记录集对象变量的范围。 如果它是全局或表单级别,当您退出子而不关闭它时,记录集var将保持打开状态 - 除非您明确执行rs.Close命令。 另一方面,如果
记录集var仅在子本身内声明,当你退出sub时,记录集var将被销毁(超出范围)并自动关闭。 但最佳做法是明确关闭它(rs.Close),然后在执行rs.Open之前始终检查你的记录集var
是否已关闭......
This depends on the scope of your recordset object variable. If it is global or Form level, when you exit a sub without closing it, the recordset var will remain open -- unless you explicitly do an rs.Close command. If, on the other hand, the recordset var is only declared within the sub itself, when you exit the sub, the recordset var will be destroyed (out of scope) and automatically closed. But best practice is to explicitly close it (rs.Close) and then always check if your recordset var is closed before you do an rs.Open...
这篇关于exiiting sub是否发布了记录集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!