set dbs = currentdb()和直接使用currentDB()之间有什么重要的区别吗?

时间:2014-01-28 21:08:40

标签: ms-access

我继承了许多基本上像这样的代码:

dim dbs as dao.database
set dbs = currentdb()
dbs.execute "Some SQL string"
set dbs = nothing

有没有理由不将其重新编码为:

currentdb().execute "some  SQL string"

(我知道如果我想使用.recordsaffected,currentdb()。recordsaffected将不会产生可用的结果。)

除了简化代码之外,重新编码有什么好处吗?

1 个答案:

答案 0 :(得分:8)

简单地使用CurrentDb.Whatever是一个诱人的捷径,但在很多情况下它会导致奇怪的行为。那是因为CurrentDb不是Object本身,它是一个返回当前Database对象副本的Function。

多年前,我发誓尝试使用CurrentDb,因为它是一个对象,在我无法调试代码之后,我知道是“正确的”,它是......我创建了一个合适的DAO.Database对象(Set cdb = CurrentDb)并使用cdb.Whatever代替CurrentDb.Whatever