If it's inside the loop, just move it outside the loop!
If it's inside the loop, use a SQL statement that does
the whole transaction, instead of a loop that calls
repeated SQL statements.
If you need to have code for every line, make a SQL
statement that calls code, rather than code that calls
SQL statements.
I avoid all global variables, so although there is nothing
wrong with:
set rs=GlobalVariable.OpenRecordset(strSQL)
I avoid it anyway. CurrentDB is a global object, so
I'm not avoiding the global object, I'm just avoiding
having a second global object.
Using CurrentDB forces a refresh of the application
databases collection. Since I only have one database
in my databases collection, this does not take long. It
is a measurable delay, but since my code logic minimises
the number of times I have to get a database object, it
makes no difference to my application.
Some people use tabledef objects and indexes to get
very fast 'find' actions. To do this, they need a direct
connection to the back-end database (not CurrentDB).
Creating and destroying that connection is much slower,
and there is no existing global database object, so they
create and maintain a global database object, and do
their own check and refresh each time the object is used.
I don't use tabledef objects, because the SQL Server
equivalent is quite different, and I want to be able to use
either a SQL Server BE or a Jet MDB BE interchangeably
with minimum work. So again, I don't need a special
global database object for my BE database.
The bottom line is, I have complex queries that take 20
seconds to run, and code that takes 15 minutes to run,
and reports that take 10 minutes to run, (instead of 20
minutes, 15 hours, 10 hours), all because I have heavily
optimised everything, and I still haven't optimised out
the "set db=codedb" in my DLookup replacement, -
because it adds less than a second to that 20, and is just
one more thing that might break.
(david)
Post by Stefan Hoffmannhi Tony,
Post by Tony Toews [MVP]I just use set rs = currentdb.openrecordset(sql). While Stefan's
approach is very likely to be more efficient I haven't noticed any
performance problems with the above.
In this simple case it is just milliseconds. But in the long run, and
especially when called in a loop, it sums up to a nice amount. Maybe
seconds only, but you know just 2 cents :)
mfG
--> stefan <--