- Local time
- Today, 05:32
- Joined
- Jul 9, 2003
- Messages
- 16,859
### Subject: Seeking Elegant Solution for Traversing Control Hierarchy to Find Parent Form
Hi All...
I've been working on a VBA function to traverse the hierarchy of controls in Microsoft Access to find the parent form. While I have a working solution, I'm aiming for a more elegant and streamlined approach. Here’s the scenario:
### The Challenge
I need to traverse the hierarchy of controls to find the top-level form. My current solution involves checking the name of each parent object and verifying it against the forms collection to avoid type mismatch errors (error 13) and invalid reference errors (runtime error 2452). Here’s the working code:
While this approach works, it feels a bit clunky and verbose. I’m looking for a more elegant solution that can dynamically handle any depth of control hierarchy without hitting type mismatch or invalid reference errors.
### Attempted So Far
- I’ve tried using a `Do While` loop with `TypeName`, but encountered runtime errors and type mismatches.
- Variations of using `Object` types instead of `Control`, but these also resulted in errors or failed to address the core issue.
Is there a better way to traverse the control hierarchy in VBA to find the top-level form? I’m looking for a solution that avoids the pitfalls of type mismatches and invalid references, ideally with a touch of elegance.
TIA
Thank you in advance for your expertise and time.
Cheers Tony - Uncle Gizmo
Hi All...
I've been working on a VBA function to traverse the hierarchy of controls in Microsoft Access to find the parent form. While I have a working solution, I'm aiming for a more elegant and streamlined approach. Here’s the scenario:
### The Challenge
I need to traverse the hierarchy of controls to find the top-level form. My current solution involves checking the name of each parent object and verifying it against the forms collection to avoid type mismatch errors (error 13) and invalid reference errors (runtime error 2452). Here’s the working code:
Code:
' Function to get the parent form of a control
Private Function fGetParentForm(ctrlActiveControl As Control) As Form
Dim ctlToTest As Control
Set ctlToTest = ctrlActiveControl
Dim X As Integer
For X = 1 To 20
If Not fParentIsaForm(ctlToTest.Parent.Name) Then
Set ctlToTest = ctlToTest.Parent
Else
Set fGetParentForm = ctlToTest.Parent
Exit Function
End If
Next X
If X >= 20 Then
MsgBox "ERROR 20 Levels EXCEEDED, change 20 to a higher figure", vbExclamation, conAppName
Set fGetParentForm = Nothing
End If
End Function
Code:
' Function to check if the parent is a form
Private Function fParentIsaForm(strIsaForm As String) As Boolean
fParentIsaForm = False
Dim obj As AccessObject, dbs As Object
Set dbs = Application.CurrentProject
For Each obj In dbs.AllForms
If obj.Name = strIsaForm Then
fParentIsaForm = True
Exit For
End If
Next obj
End Function
While this approach works, it feels a bit clunky and verbose. I’m looking for a more elegant solution that can dynamically handle any depth of control hierarchy without hitting type mismatch or invalid reference errors.
### Attempted So Far
- I’ve tried using a `Do While` loop with `TypeName`, but encountered runtime errors and type mismatches.
- Variations of using `Object` types instead of `Control`, but these also resulted in errors or failed to address the core issue.
Is there a better way to traverse the control hierarchy in VBA to find the top-level form? I’m looking for a solution that avoids the pitfalls of type mismatches and invalid references, ideally with a touch of elegance.
TIA
Thank you in advance for your expertise and time.
Cheers Tony - Uncle Gizmo
Last edited: