[AccessD] Find First in an Array?

jwcolby jwcolby at colbyconsulting.com
Wed Feb 25 08:00:47 CST 2009


Stuart,

You are correct of course, that is the *total* (I assume you mean *minimum*) code required.

I ran tests last week and from my testing the time to get the strings out of one big collection was 
a lot greater than to get the strings out of a collection for each form.  By my testing the time 
dropped from 20 ms to "0" ms (so small it didn't see a timer tick).

Refactor your code to use a collection for each form and I will be happy.  Hint, use a collection to 
hold each form's strings, and store those collections in another collection, keyed on form name. 
BTW, before I discovered classes I often used "collections of collections".  It works quite well, it 
is just that the programming can get messy.

Now I understand that some will say "20 ms, why go further".  There is a very good reason, simply 
being that there are many forms out there that are much more complex than Rocky's examples, and as 
the number of controls rise, so does the time to do just this one process.  Additionally this one 
process is ONLY one of potentially many processes that may have to run as the form opens.  The "that 
is fast enough" attitude ignores that fact.

Code is never fast enough, and when you are writing code for generic processes that is doubly true. 
  Of course you have to make decisions as to when the extra effort to make it faster is worth said 
effort.  OTOH it took me just a few minutes of coding to create a solution that dropped the time 
from 20 ms to under 1 ms.  That effort will now be applied to hundreds of forms of unknown 
complexity in as many projects as I choose.

It appears to me from your posts that you are a minimalist, the *total* (IOW least possible) code 
required is very important to you and that's fine but the least code possible is not at the top of 
my personal priorities.  Horses for courses.

I am looking for minimal code of course.  I also want fast code.  I also want organized code.  I 
want code that is easy (for me) to read.  These things are occasionally in conflict.  I choose 
classes for my solution because (in order of importance TO ME):

1) It organized my code the way I like it organized.
2) The very creation of classes provided me with the constructs to easily speed up my code.
3) The increased speed cost me almost nothing.
4) The increased speed will be applied thousands or even millions of times across projects, further 
amortizing even the minimal time I spent.

I like your solution.  It clearly shows how to use a collection to achieve the results.  It caches 
the results so that the next time the loads will be faster.  And one simple change (a collection of 
collections) will make you code a LOT faster.

John W. Colby
www.ColbyConsulting.com


Stuart McLachlan wrote:
> In the absence of the sample DB, function Startup() below may need a 
> minor modification.
> 
> Here's the *total* code required to do the translations with a Collection, 
> which is indexed on Form,Control and Language (other than to add 
> "SetCaptions Me.Name" in each forms on_open).
> 
> 
> Option Compare Database
> Option Explicit
> Global gcolTranslation As New Collection
> Global glngLanguage As Long
> 
> Function StartUp()
> 'Code which is run when the application starts
> Dim rs As DAO.Recordset
> Dim x As Long
> 'Fill Collection
> Set rs = CurrentDb.OpenRecordset("tblLanguages")
> While Not rs.EOF
>     For x = 2 To rs.Fields.Count - 1
>     gcolTranslation.Add rs(x), _
>          rs!FormName & Chr$(0) _
>          & rs!ControlName & Chr$(0) _
>          & Format(x - 2, "00")
>     Next
>     rs.MoveNext
> Wend
> 'Get current default language
> glngLanguage = DLookup("Language", "uSysDefaults")
> 
> End Function
> 
> Function SetCaptions(frm As Form)
> Dim ctl As Control
> 'skip labels which don't have a translation
> On Error Resume Next
> For Each ctl In frm
>   For Each ctl In frm.Controls
>     If ctl.ControlType = acLabel Then
>       ctl.Caption = gcolTranslation(frmName _
>            & Chr$(0) & ctl.Name _
>            & Chr$(0) & Format(glngLanguage, "00"))
>     End If
> Next ctl
> End Function
> 
> Function SetLanguage(Language As Long)
> Dim frm As Form
> CurrentDb.Execute "Update uSysDefault Set Language = " & Language
> 'update all open forms
> For Each frm In Application.Forms
>    SetCaptions frm
> Next
> End Function
> 



More information about the AccessD mailing list