Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

ms access - Is there any important difference between set dbs=currentdb() and using currentDB() directly?

I have inherited a lot of code that is essentially like this:

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

Is there any reason not to recode it as:

currentdb().execute "some  SQL string"

(I know that if I want to use .recordsaffected, currentdb().recordsaffected won't yield usable results).

Are there any benefits from recoding it, other than simplifying the code?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simply using CurrentDb.Whatever is a tempting shortcut, but there are quite a few circumstances where it causes strange behaviour. That's because CurrentDb is not an Object itself, it is a Function that returns a copy of the current Database object.

Years ago I swore off trying to use CurrentDb like it was an Object after the umpteenth time I was debugging code that I knew was "right", and it was... once I created a proper DAO.Database object (Set cdb = CurrentDb) and used cdb.Whatever instead of CurrentDb.Whatever.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...