jwcolby
jwcolby at colbyconsulting.com
Mon Oct 26 12:26:07 CDT 2009
A.D wrote:
> There could be an interesting way to handle this requirement conveniently by adoption of a
single collection where each of its elements is itself a collection,
Ick!!!
Can you say CLASSES?
I used to use collections of collections, and while they do work it gets UGLY VERY QUICKLY!
OK A.D, you have thrown down the gauntlet. ;)
Here is a class implementation.
Two classes - this is the first:
clsCtlSorted - the collection of controls in sorted order for some object (section or tab page)
Option Compare Database
Option Explicit
Private mstrObjName As String
Private mcolCtlsSorted As Collection
Private Sub Class_Initialize()
Set mcolCtlsSorted = New Collection
End Sub
Private Sub Class_Terminate()
Set mcolCtlsSorted = Nothing
End Sub
'
'Pass in any collection of controls to be stored in sorted order
'
Function mInit(lstrObjName As String, colControls As Object, blnIsSection As Boolean)
If blnIsSection Then
mStoreSectionCtls lstrObjName, colControls
Else
mStoreNonSectionCtls lstrObjName, colControls
End If
End Function
'
'Tests for and stores section controls not on tabs
'
Private Function mStoreSectionCtls(lstrObjName As String, colControls As Object)
Dim ctl As Control 'Dim a ctl object to use in the ofr each loop
Dim intTabIndex As Integer 'Dim a variable to use to generate an error if the control does
NOT have a tabindex property
Dim ctlParent As Control 'Dim a variable to test whether the parent is a control
mstrObjName = lstrObjName
For Each ctl In colControls 'For each iterator to grab controls from collection
On Error Resume Next
intTabIndex = ctl.TabIndex 'Test for the TabIndex property
If Err = 0 Then 'If no error then process control (store in the collection)
Set ctlParent = ctl.Parent
If Err = 0 Then
Else
mcolCtlsSorted.Add ctl, CStr(ctl.TabIndex)
Err.Clear
End If
Else
Err.Clear
End If
Next ctl
End Function
'
'Tests for and stores controls not directly on the section (on a tab)
'
Private Function mStoreNonSectionCtls(lstrObjName As String, colControls As Object)
Dim ctl As Control 'Dim a ctl object to use in the ofr each loop
Dim intTabIndex As Integer 'Dim a variable to use to generate an error if the control does
NOT have a tabindex property
Dim ctlParent As Control 'Dim a variable to test whether the parent is a control
mstrObjName = lstrObjName
For Each ctl In colControls 'For each iterator to grab controls from collection
On Error Resume Next
intTabIndex = ctl.TabIndex 'Test for the TabIndex property
If Err = 0 Then 'If no error then process control (store in the collection)
Set ctlParent = ctl.Parent
If Err = 0 Then
mcolCtlsSorted.Add ctl, CStr(ctl.TabIndex)
Else
Err.Clear
End If
Else
Err.Clear
End If
Next ctl
End Function
Property Get pObjName() As String
pObjName = mstrObjName
End Property
'
'Iterate the collection of controls getting each control name
'and appending it to a string
'
Property Get pCtlNames() As String
Dim ctl As Control
Dim strCtlNames As String
strCtlNames = pObjName & ":: " & vbCrLf
For Each ctl In mcolCtlsSorted
strCtlNames = strCtlNames & vbTab & ctl.Name
Next ctl
pCtlNames = strCtlNames
End Property
'
'Return the collection of controls in sorted order (keyed on TabIndex)
'
Property Get pCtlsSorted() As Collection
Set pCtlsSorted = mcolCtlsSorted
End Property
John W. Colby
www.ColbyConsulting.com
A.D.Tejpal wrote:
> Run time handling of form controls in the order of tab index.
> ========================================
>
> Use of a collection as suggested by J.C., appears to be the optimum
> approach, provided the point raised by Ken is addressed suitably. A form
> having three tab controls each with 4 pages, would need 15 collections as
> each form section or tab control page has its own set of tab indices for
> controls located therein.
>
> There could be an interesting way to handle this requirement
> conveniently by adoption of a single collection where each of its elements
> is itself a collection, permitting a generic subroutine to handle unlimited
> number of tab control pages apart from the three form sections (detail,
> header & footer)