Office - Vba Code Optimization
Office - Vba Code Optimization
Seite 1 von 19
Why Optimise?
As with any large Windows development environment, you can
make choices when writing your own Access applications that will
affect the performance of your application. How you create your
queries, how you organise your tables, and how you write VBA
code can all affect your application's speed. This paper provides
some suggestions about what you can do to make your Access
applications work as well as possible, focusing on the choices you
make when writing VBA code.
Top
l Access configuration
l Database design
l Query design
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 2 von 19
l Forms design
l Reports design
l VBA coding
Creating a Stopwatch
Although you could use the VBA Timer function to calculate the
time a specific process requires, it's not the wisest choice.
Because it measures time in seconds since midnight in a single-
precision floating-point value, it's not terribly accurate. Even
though you'll most likely be timing intervals larger than a single
second, you'll want a bit more accuracy than the Timer function
can provide. The Windows API provides the timeGetTime function
(aliased as timeGetTime in the sample code), which returns the
number of milliseconds that have passed since Windows was
started.
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 3 von 19
Top
If you want to add your own tests to this test mechanism, you
must follow those constraints when planning your tests. In
addition, for each test case, you need two versions: a "slow"
version (labelled Test1a in this example) and a "fast" version
(labelled Test1b in this example). Once you've provided the two
functions, you can call the function RunTests, which, in turn, calls
both functions the specified number of times. RunTests averages
the elapsed times the functions return and reports on the
comparative speed of the two functions. Figure 2 shows the
RunTests function.
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 4 von 19
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 5 von 19
Top
Scoping rules are always an issue. Eval can't interpret any local
variables, nor can it handle private functions. You can remember
the rules this way: any object you want to send to Eval must also
work in macros. Just as VBA variables aren't available in macros,
they aren't available once Eval gets hold of the string to be
executed. RunTests uses local variables, but they become part of
the string passed to Eval and are passed as values, not as variable
names.
The string you pass to Eval must represent a function that returns
a value of some sort. That value will be the return value from the
call to Eval. Neither subroutines nor expressions are allowed.
Top
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 6 von 19
Figure 4: frmRunTests allows you to choose a specific test and run it,
resulting in a comparison between the slow and fast versions.
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 7 von 19
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 8 von 19
*These items really aren't going to affect your code one way or the
other!
You're very unlikely to use the specific code I've written in your
own applications; it's the concepts that count. For each test case,
the name of the procedure in basTests in the sample database is
Test Na (the presumed slow version) or TestNb (the supposedly
faster version), where N is the test number. For example, the code
corresponding to test case 5 is Test5a and Test5b.
Top
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 9 von 19
Top
The results of this test were decidedly mixed. Using Integer division
made almost no difference (and it did make a difference in Access
2.0). In some other examples, working with forms, for instance, this
has made a difference. It may be that VBA is smart enough to use
Integer math internally if it can tell that that's what will work most
quickly.
Top
If x = 5 Then
y = True
Else
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 10 von 19
Else
y = False
End If
Top
This code is wordier than it needs to be. The intent is to set the
variable y to True if x is equal to 5 and False otherwise. The
expression (x = 5) has a truth value of its own- -that is, it's either
True or False. You can assign that value directly to y in a single
statement:
y = (x = 5)
y = (x == 5)
with the "=" performing the assignment and the "==" checking the
equality.
If you find these logical assignments hard to read, you may choose
to skip using them, because the improvement in performance is
slight. If, however, logical assignments seem natural to use and
read, then by all means use them.
Top
Top
If x = True Then
x = False
Else
x = True
End If
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 11 von 19
End If
If x Then
x = False
Else
x = True
End If
or:
x = Not x
Top
With that declaration in place, you can call it from any module in
your application, just as though it was an internal Access function.
Top
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 12 von 19
strCaption = Forms!frmTest!cmdButton1.Caption
Top
If x = True Then
and:
If x Then
Leaving out the explicit comparison will make only a small difference
in the speed of your code. You'd have to use this construct many,
many times before this optimisation made any measurable
difference in the speed of your code, but every little bit helps.
Top
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 13 von 19
such as:
strName = rst.Fields!LastName
strName = rst.Fields("LastName")
Top
Top
Dim db As Database
Set db = DBEngine(0)(0)
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 14 von 19
Top
VBA will call both Function1 and Function2. Not only can this lead
to undesired side effects, it can just plain slow down your program.
In a case like this you're better off using the standard
If...Then...End If construct, which will execute only the portions of
the statement that fall within the appropriate clause. Given the
statement:
If fFlag Then
varValue = Function1()
Else
varValue = Function2()
End If
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 15 von 19
Top
The sample code shows two ways to accomplish the same goal:
deleting all the rows from tblContacts. The slower method uses the
RunSQL action to run the SQL string:
Top
Dim db As Database
Dim qdf As QueryDef
Set db = CurrentDb()
Set qdf = db.CreateQueryDef("", _
"SELECT * FROM tblCustomers WHERE Age > 30;")
Top
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 16 von 19
Top
Top
Top
References?
When working with DAO and its objects and collections, you always
have the option of writing out complete object references or
leaving out default collections in the references. Although leaving
out the default collections in full references may help speed your
code, it does make the code harder to read and its intent less
obvious. The following examples compare using a full reference to a
field against using a shortened version:
strName = DBEngine.Workspaces(0).Databases(0).TableDefs
(0).Fields(0).Name
or:
strName = DBEngine(0)(0)(0)(0).Name
Top
Top
If Chr$(y) = "x"
or:
If Asc("x") = y
It turns out the second method, using Asc rather than Chr$, is
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 18 von 19
significantly faster. Because the Chr$ function must take the steps
to fabricate a new string value (a costly operation), the Asc
function can perform the comparison much faster.
Top
strItem = ""
This creates a new string, and copies it into the strItem variable. It
turns out that a more expeditious solution is to use the vbNullString
constant, a pointer to an empty string. By using:
strItem = vbNullString
VBA doesn't have to create a new string each time you make the
assignment, but uses its own internal pointer to an empty string,
saving a substantial amount of work each time you initialise a string
variable.
Top
Top
Summary
This paper has presented a variety of suggestions for improving the
performance of your Access applications. The following topics were
covered:
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 19 von 19
which you can improve the performance of the VBA code in your
applications. Much of the text and examples in this session have
been extracted from:
Top
This image means that the link points to servers that are not under Microsoft's
control. Please read our official statement regarding other servers.
Top
Submit
Write us at [email protected].
http://www.microsoft.com/officedev/articles/movs101.htm 20.04.00