VBA在退出时查询和清理字符串

时间:2015-10-29 15:20:51

标签: vba ms-access

如果我有使用VBA创建的查询:

dim SQL as string
dim rs as recordset
dim db as database

SQL = "SELECT ... FROM ..."

Set db = CurrentDb
Set rs = db.OpenRecordset(SQL, dbOpenDynaset)

在我的小结尾,我总是会做以下事情:

rs.close
set rs = nothing

我的问题是,我需要SQL =""或类似的东西吗?我认为我的困惑最初来自于我在代码中没有使用set SQL这一事实。

如果我确实清除了这些字符串,那么,是否有最好的'方式是什么?

1 个答案:

答案 0 :(得分:1)

Since you're not opening a connection to either CurrentDb or the SQL string, there's no need to close them. However, you are opening a recordset, so that should be closed. It wouldn't harm anything to set SQL = "", but it's not going to actually do anything constructive.

As far as a "best way", I think you've already got it. At the end of your sub, or before any code that might prematurely exit it, just put:

rs.close
set rs = nothing