Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 28: Object-Oriented Programming with VBA


985


to object-oriented programming techniques because they want more control over how the data is
used by their applications. Using bound forms negates many of the following advantages of using
object-oriented programming techniques without really adding anything of value to the project:

l Programming flexibility: Bound forms hide the data acquisition and display processes, so
you lose the opportunity to take control of these operations.
l Encapsulation: There’s nothing (data-wise) to encapsulate on a bound form. The data
exists in the form’s recordset, so you can’t add new properties or methods to a class that
acts as the data’s container.

Also, most developers using object-oriented programming techniques are fairly advanced and are
comfortable building unbound applications. The extra code involved in building classes containing
properties and methods is not a hindrance to the majority of advanced Access developers.

There are two cardinal rules that you must obey when applying object-oriented programming tech-
niques. I didn’t make up these rules, but I know from personal experience that you’re asking for trouble
when you fail to pay adequate attention to them.

l (^) Never reveal a user interface component, such as a message box, from a class module. This
rule is perhaps less important in Access applications than in other systems, but ignoring this
rule may cause problems later on.
Here’s why this is important: Consider a class that opens a message box to the user, indicat-
ing that a problem has arisen. Although this works fine in Access environments, this practice
may cause problems if the class is ported to other environments.
All Access applications run locally on the user’s computer. Therefore, opening a dialog box
from an Access class module is guaranteed to open on the user’s computer. In an Access
application, there’s no way to cause a message box to appear on another computer.
However, other development platforms support the notion of remoting (running code on an
application server). Most often, the remoted component is implemented as a set of compiled
classes, and if one of those classes opens a dialog box, the dialog box opens on the remote
application server.
In this case, the application freezes in front of the user, and the user has no idea what hap-
pened. All the user knows is that the code stopped running. The code on the remote machine
has stopped running, waiting for a response to the dialog box that has opened on the applica-
tion server.
For obvious reasons, you’re not going to make many friends if your application causes an
application server to stop running!
l (^) Preserve the class’s interface as the class is updated. You can add to the interface by intro-
ducing new properties, methods, and events, but you should never alter the data type of exist-
ing properties or method arguments, or remove an event from a class module.
continued
Following the rules

Free download pdf