Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Office - Vba Code Optimization

Office - Vba Code Optimization



|Views: 995 |Likes:
Published by api-3766251

More info:

Published by: api-3766251 on Oct 16, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 1 von 19
http: / / www.microsoft.com/ officedev/ articles/ movs101.htm
December 10,
1998\ue000\ue000\ue000\ue000Back to Microsoft Office Articles and Sam ple Apps
VBA Code Optimisation
By Ken Getz
This article originally appeared in the conference
proceedings for the Microsoft Office and VBA Solutions

Conference and Exposition, held in London, England, July 12-15, 1998, and is reprinted with permission of Informant Communications Group, Inc. Please visit their Web site at

http: / / www.informant.com/ mod/ index.asp
Download the sample code discussed in this article.
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.
Tuning Your Application's
Perform ance
The faster your application and each of its components perform,
the more usable it will be. No user (or developer) likes a slow

application. Getting extra performance sometimes requires you to make trade-offs and may affect other aspects of the application's usability, stability, and maintainability. Thus, it's important to keep the following issues in mind as you tune your applications for

Some of the aspects of performance tuning:

lHardware and memory
lAccess configuration
lDatabase design
lQuery design

\ue000\ue000All Products\ue000 |\ue000\ue000Support\ue000 |\ue000\ue000Search\ue000 |\ue000\ue000micr
o so ft.co m
G u i
\ue000Office\ue000\ue000| \ue000Access\ue000\ue000| \ue000Excel\ue000\ue000| \ue000FrontPage\ue000\ue000| \ue000Outlook\ue000\ue000| \ue000Pow erPoint\ue000\ue000| \ue000Word\ue000\ue000| \ue000VBA\ue000\ue000|
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 2 von 19
http: / / www.microsoft.com/ officedev/ articles/ movs101.htm

lForms design
lReports design
lSingle-user versus multi-user, file-server versus client/ server

application design
lVBA coding

To create applications that perform well, you will have to address many, if not all, of these areas. In this paper, I'll present methods you can use to test your hypotheses to solve any particular VBA question, and a series of suggestions about steps to take (and not to take) to make your VBA code work as well as possible.

Speeding Up VBA: Testing Hypotheses
As in any programming language, VBA has many ways to
accomplish the same task. Because you're dealing not only with a

language but also with the interface and the underlying data, all
tied together in the programming environment, the choices are
often even more complicated than with other, more standard

languages. The following sections propose a series of selected
optimisations, some more potent than others, and some that are
based on incorrect assumptions (that is, theyd o n ' t help at all and
perhaps even hurt). Probably no single application will be able to
use each of these, but you can add the ones that help to your
"bag of tricks" as you program in Access. You'll also find a method
for timing those optimisations so you can create your own test
Creating a Stopw atch
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

Note:\ue000\ue000\ue000Not that it matters for testing purposes, but
Timer "rolls over" every 24 hours. timeGetTime keeps on

ticking for up to 49 days before it resets the returned tick count to 0. Most likely, if you're timing something that runs for 49 days, you're not interested in

milliseconds, but that's what you get. One more
benefit, you'll see later: the act of calling timeGetTime
is significantly faster than calling Timer.
To test each of the proposed optimisations, you need some
mechanism for starting and stopping the clock. The subroutine
StartTimer stores the current return value from timeGetTime into a
Microsoft Office Developer Forum - Office - VBA Code Optimi.. Seite 3 von 19
http: / / www.microsoft.com/ officedev/ articles/ movs101.htm
StartTim er stores the current return value from
t im e G e t T im e in t o a
global variable, lngStartTime. You must call this subroutine directly
before any code you want to have timed. When you're done with
the critical section of code, call the function EndTimer, which
returns the difference between the current time and the time when
you called StartTimer, or the elapsed time. Figure 1 shows the
declarations and code for the timer functions.

Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Dim lngStartTime As Long
Sub StartTimer()

lngStartTime = timeGetTime()
End Sub
Function EndTimer()

EndTimer = timeGetTime() - lngStartTime
End Function
Figure 1: Code to perform timings, using the timeGetTime API function.
Note:\ue000\ue000\ue000Most Windows programmers have used the

GetTickCount function in previous versions of Windows
to perform their high-resolution timing. Although that
function returned its result in milliseconds, it was never
more accurate than the clock timer in your PC, which

measures time in increments of 1/ 18 second. The
timeGetTime function, introduced in Windows 3.0 as
part of the multimedia extensions, uses a different
hardware timer and can actually measure time with
millisecond accuracy. Before Windows 95 and Windows
NT, you couldn't have been sure your users had the
correct multimedia .DLLs on their system. With the new
operating systems, you're assured that all users will
have the necessary DLLs, and you can use

timeGetTime without worry.
Getting Reasonable Results
You will find that running any given test only once doesn't provide
reliable results. There are just too many external forces in play
when you're running under Windows. To get a reasonable idea of

the benefit of a given optimisation test, you need to run the test code many times within the given test case and then run the test case many times, averaging the results. For simplicity, each of the tests in this paper takes as its only parameter a Long value

indicating the number of times you want to run the test. Each
function loops the specified number of times with the clock running
and provides the elapsed time as the return value of the function.
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.

Activity (14)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Marcos Santos liked this
VECTORSPACE liked this
act_64 liked this
wpilon liked this
agupta12345 liked this
mytraining liked this
vaslasu liked this
Kaelta liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->