[AccessD] Classes and Events - basXlate

jwcolby jwcolby at colbyconsulting.com
Sun Feb 22 23:35:00 CST 2009


Class systems have to be initialized somewhere.  Often this is a form, or another class, but often 
we will be creating a module to hold a pointer to the base class of the system, as well as code to 
initialize and tear down the class system and a function to get a pointer to the base class.

This pattern will become routine as we build systems of classes that perform a function for the 
application.  You will see it over and over, an initialization module, a base class, and other 
classes called by the base class, perhaps even instances stored in collections in the base class.

This lecture builds the initialization module for the translation system.  Because the system caches 
the data to perform form translations, the base class clsXlateSupervisor is “global” to the system. 
  The clsXlateSupervisor is called by each form as it opens, and clsXlateSupervisor then causes the 
form to be translated into the language requested.

•	Open the demo database.
•	In the menu click Insert / Module (NOT a class).
•	Immediately save the module to basXlate.
•	In the header of the class insert the following code:

'---------------------------------------------------------------------------------------
'Functions to initialize, terminate and return a pointer to clsXlateSupervisor
'as well as a global variable for the translation string field name.
'---------------------------------------------------------------------------------------
Const cstrModule As String = "basXlate"
Option Compare Database
Option Explicit

Private mclsXlateSupervisor As clsXlateSupervisor
'
'Set this variable to the name of the field holding the translation language strings
'
'fldLanguageEnglish
'fldLanguageChineseComplex
'etc
'
Public mstrLanguageFldName As String

This code creates two variables.  mclsXlateSupervisor is private, and holds a pointer to the single 
instance of clsXlateSupervisor.  We cause this variable to be private so that it can only be created 
by a call to an initialization function, and can only be torn down by a call to a terminate 
function.  In other words no “accidental” manipulation of the pointer.

mstrLanguageFldName is public.  This variable must be set to the name of a valid language field in 
the translation table before the class is initialized the first time.  If that is not done, the 
results are undetermined, that is a correct translation will not be performed.

Function mXlateSupervisorInit()
     If mclsXlateSupervisor Is Nothing Then
         Set mclsXlateSupervisor = New clsXlateSupervisor
     End If
     Set mXlateSupervisorInit = mclsXlateSupervisor
End Function

This function initializes the supervisor class.  The pointer to the class is tested to see if it is 
already initialized.  If not then a new instance to clsXlateSupervisor is created and saved in 
mclsXlateSupervisor.  The pointer to the class is then returned from the function.

Function mXlateSupervisorTerm()
     Set mclsXlateSupervisor = Nothing
End Function

This function simply destroys the supervisor class instance.  Call it when you terminate your 
application.

Function cXS() As clsXlateSupervisor
     Set cXS = mXlateSupervisorInit()
End Function

cXS() is a function which calls the initialization code every time to make sure it is initialized, 
and returns a pointer to the now initialized supervisor class.

This lecture discusses the initialization module for the translation system.  The initialization 
module for any class system is usually exactly as you see here.  It consists of a private pointer to 
the base class, code to initialize and terminate the class, and a function to call the Init and 
return a pointer to the initialized base class.  If the bass class already exists, the init simply 
grabs a pointer to the base class instance; otherwise it initializes the base class and returns the 
pointer.

There is not much in this module, and what is here will be seen over and over as we add class 
systems to perform tasks for the application.

The last piece to discuss is the form to be translated.  In the class behind the form simply add the 
following code:

Private Sub Form_Open(Cancel As Integer)
     '
     'Call the mTranslateFrm method of the translation supervisor and
     'pass in a reference to this form, and a language field name
     '
     cXS.mTranslateFrm Me, mstrLanguageFldName
End Sub
This code calls the function that returns a pointer to the clsXlateSupervisor.  It passes in a 
pointer to the form itself (me) and the string that contains the name of the desired language field 
in the translation table.  By the time this cXS call returns the form is translated and the 
translation data is cached for the next time this form opens.

To watch the system perform a translation of a form, place a break point in cXS and then step the 
code.  By now you should be able to step through the code and follow along as classes are 
instantiated, methods called and so forth.

In this lecture we discovered how simple the initialization code for class systems is.  In most 
cases there is a private pointer to the base class, an init and term function and a function that 
returns a pointer to the base class.  There isn’t much to see but we discussed the first time 
through the initialization function where the base class is initialized.  We then discussed the 
single line of code that is called from the form’s Open event that causes the form to be translated.

Class are often used in systems.  You have seen such systems in Access, for example recordsets have 
records, which have fields, which have properties.  All of these things are classes, and all of 
these things exist in collections of a class above them.  We are now creating class systems of our 
own.  This is a simple two class system, consisting of a translation supervisor class and a form 
translation class.  You will see more and more class systems as I write more of these lectures.

Eventually I will create a framework base class which will hold instances of the base classes for 
application systems such as form translations.  At that point we will have only a single 
initialization module, which will initialize the framework.  The framework base class will 
initialize all other classes.  When we create the framework class, you too will be a framework 
developer.

-- 
John W. Colby
www.ColbyConsulting.com



More information about the AccessD mailing list