[AccessD] Classes and Events - clsXlateSupervisor

jwcolby jwcolby at colbyconsulting.com
Sun Feb 22 22:42:37 CST 2009


clsXlateSupervisor:

This class will be the supervisor for the translation process.  The supervisor really just holds a 
collection that can hold instances of clsXlateFrm so that the data is cached, and a method to call 
to perform the translation of a form.  If the cache is not desired then clsXlateFrm would be 
instantiated directly in the form’s class in the Open event and the instance of the class is 
discarded after performing the translation.

If caching is desired then this class will perform the caching of the form classes for us.

•	Open the demo database.
•	In the menu click Insert / Class
•	Immediately save the class as clsXlateSupervisor.
•	In the header of the class add the following code:

Option Compare Database
Option Explicit

Private mcolClsXlateFrm As Collection
Private mlngCacheSize As Long
Private mstrLanguageFld As String

This code creates a collection to hold the clsXlateFrm instances, keyed on the form name.  It also 
creates a cache size variable for the total cache size for all translation phrases for all forms 
loaded so far, and a place to save the language field name.

Private Sub Class_Initialize()
     Set mcolClsXlateFrm = New Collection
End Sub

Private Sub Class_Terminate()
     Set mcolClsXlateFrm = Nothing
End Sub

The Initialize and terminate events are used to set up and tear down a pointer to the collection.

'
'Get the cache size for all forms cached so far
'
Property Get pCacheSize() As Long
     pCacheSize = mlngCacheSize
End Property
'
'Return a pointer to colClsXlateFrm
'
Property Get colClsXlateFrm() As Collection
     Set colClsXlateFrm = mcolClsXlateFrm
End Property
The class properties return the private variables in the header of the class.

'---------------------------------------------------------------------------------------
' Procedure : mTranslateFrm
' Author    : jwcolby
' Date      : 2/21/2009
' Purpose   : Performs the translation for one form passed in.
'---------------------------------------------------------------------------------------
'
Function mTranslateFrm(lfrm As Form, lstrLanguageFldName As String)
Dim lclsXlateFrm As clsXlateFrm
On Error GoTo Err_mTranslateFrm

     '
     'If the language changes then purge the collection of clsXlateFrm instances
     '
     If lstrLanguageFldName <> mstrLanguageFld Then
         mstrLanguageFld = lstrLanguageFldName   'Save the new language field name
         '
         'Setting a collection to nothing causes the contents of the collection to be deleted.
         Set mcolClsXlateFrm = New Collection    'Empty the collection and get a new collection
     End If

     On Error Resume Next
     '
     'Try to get an instance of the clsXlateFrm from the collection for this form
     '
     Set lclsXlateFrm = mcolClsXlateFrm(lfrm.Name)
     If Err Then
         '
         'If that fails then this is the first time so perform the initialize
         '
         Set lclsXlateFrm = New clsXlateFrm
         '
         'Now that we have an instance for this form perform the load and translation
         '
         lclsXlateFrm.mInit lfrm, lstrLanguageFldName
         '
         '
         '
         mlngCacheSize = mlngCacheSize + lclsXlateFrm.pCacheSize
         Debug.Print lclsXlateFrm.pName & ":" & lclsXlateFrm.pCacheSize
         '
         'And save the instance to the collection for the next time
         '
         mcolClsXlateFrm.Add lclsXlateFrm, lfrm.Name
     Else
         '
         'We have already loaded this form once so perform the translation
         '
         lclsXlateFrm.mXlateFrm lfrm
         '
         '
         '
         Debug.Print lclsXlateFrm.pName & ":" & lclsXlateFrm.pCacheSize
     End If

Exit_mTranslateFrm:
     On Error Resume Next
     Exit Function
Err_mTranslateFrm:
     Select Case Err
     Case 0      '.insert Errors you wish to ignore here
         Resume Next
     Case Else   '.All other errors will trap
         Beep
         MsgBox Err.Number & ":" & Err.Description
         Resume Exit_mTranslateFrm
     End Select
     Resume 0    '.FOR TROUBLESHOOTING
End Function

So far, there is only a single method in this class.  mTranslate form accepts a pointer to a form 
and the name of a language field in language table.  The first thing that the code does is test 
whether the language field is the same as the last time the method was called.  If not then it 
creates a new instance of the collection.  Doing this to a collection pointer causes any contents of 
the collection to be gathered by the VBA garbage collection, in other words, it empties the 
collection, closes it, and then creates a brand new collection object.

Doing this is not a good idea of the collection contains any pointers to Access objects such as 
recordsets, controls or forms, or contains classes which contain pointers to these objects.  Access’ 
garbage collector is infamously flakely about correctly cleaning up such objects.  In our case 
however the collection contains classes which only store simple types such as strings, and as far as 
I can tell the garbage collector cleans up objects like that just fine.  So for our purposes, simply 
dimensioning the collection as a new instance is perfectly safe.

Once we test for a new language, we attempt to get a clsXlateFrm instance from the collection for 
the current form.  If none exists, we create a new instance for this form, and initialize the 
instance by calling mInit().  mInit() completely translates the form and caches the translation 
phrases for this form in a collection in clsXlateFrm.  We then simply call a property of clsXlateFrm 
to get the cache size and add it to a variable in our header.  Finally we add the new clsXlateFrm to 
our collection so that it is saved for the next time the form opens.

If this is the second or subsequent time the form was opened, the collection already contains an 
instance of clsXlateFrm for this form.  In this case we just call the mXlateFrm () method of that 
instance in order to perform the translation for this form using the already loaded and cached 
translation phrases.

In this lecture we have discussed the operation of the supervisor class for the translation system. 
  This class is extremely simple, with three properties and a single mTranslateFrm function.  As we 
shall see in the next lecture, this class is used just by calling the single function, passing in a 
pointer to the form to translate and the field name that holds the translation language phrases.



-- 
John W. Colby
www.ColbyConsulting.com



More information about the AccessD mailing list