jwcolby
jwcolby at colbyconsulting.com
Mon Oct 26 12:29:02 CDT 2009
Second class - the supervisor:
Option Compare Database
Option Explicit
Private colClsCtlsSorted As Collection
Private Sub Class_Initialize()
Set colClsCtlsSorted = New Collection
End Sub
Private Sub Class_Terminate()
Set colClsCtlsSorted = Nothing
End Sub
Function mInit(frm As Form)
mScanFormForSections frm
End Function
'
'Returns a string of all controls and what object they belong to
'
Property Get pCtlNames() As String
Dim lclsCtlsSorted As clsCtlsSorted
Dim strCtlNames As String
For Each lclsCtlsSorted In colClsCtlsSorted
strCtlNames = strCtlNames & lclsCtlsSorted.pCtlNames() & vbCrLf
Next lclsCtlsSorted
pCtlNames = strCtlNames
Debug.Print strCtlNames
End Property
'
'The class factory creates an instance of clsCtlsSorted,
'passing in the name of the object in the form
'that contained the controls collection
'
'After the class is instantiated and initialized,
'the instance is stored in colClsCtlsSorted
'keyed on the name of the object that contained the controls collection
'
Private Function mClassFactory(lstrObjName As String, colControls As Object, blnIsSection As Boolean)
Dim lclsCtlsSorted As clsCtlsSorted
Set lclsCtlsSorted = New clsCtlsSorted
lclsCtlsSorted.mInit lstrObjName, colControls, blnIsSection
colClsCtlsSorted.Add lclsCtlsSorted, lstrObjName
End Function
'
'Return the entire collection of all instances of clsCtlSorted
'
Property Get colClsControlsSorted() As Collection
Set colClsControlsSorted = colClsCtlsSorted
End Property
'
'Return a clsCtlSorted instance for a specific object (tab, form section etc)
'
'strObjName is the name of the tab page, control section etc.
'
Function cControlsSorted(strObjName As String) As clsCtlsSorted
On Error Resume Next
Set cControlsSorted = colClsCtlsSorted(strObjName)
End Function
'
'Finds every section of a form
'Having found any valid section, it creates an
'instance of clsCtlsSorted for that section
'
'---------------------------------------------------------------------------------------
' Procedure : mScanFormForSections
' Author : jwcolby
' Date : 10/26/2009
' Purpose :
'---------------------------------------------------------------------------------------
'
Function mScanFormForSections(frm As Form)
Dim sec As Section
Dim intSection As Integer
On Error GoTo Err_mScanFormForSections
On Error Resume Next
For intSection = 0 To 4 'There are only 5 sections in a form
Set sec = frm.Section(intSection) 'Test to see if the section exists
If Err = 0 Then 'The section exists so
On Error GoTo Err_mScanFormForSections
mClassFactory sec.Name, sec.Controls(), True 'Get an instance of lclsCtlsSorted
mScanSectionForControls sec 'Now scan the section for tab controls
Else
Err.Clear
End If
Next intSection
Exit_mScanFormForSections:
On Error Resume Next
Exit Function
Err_mScanFormForSections:
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_mScanFormForSections
End Select
Resume 0 '.FOR TROUBLESHOOTING
End Function
'
'Sections can contain tabs
'which contain pages which contain controls
'
Function mScanSectionForControls(sec As Section)
Dim ctl As Control
For Each ctl In sec.Controls 'Iterate the section's contyrol collection
If ctl.ControlType = acTabCtl Then 'Look for tab controls
mScanTabForPages ctl 'Look for pages of the tab control
End If
Next ctl
End Function
'
'Find all pages of a tab control
'
Function mScanTabForPages(ctlTab As Control)
Dim pg As page
For Each pg In ctlTab.Pages 'Iterate the page collection
mClassFactory ctlTab.Name & "." & pg.Name, pg.Controls, False 'Get an instance of
lclsCtlsSorted for the page
Next pg
End Function
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)