Article ID: 139040
Article Last Modified on 1/19/2007
Syntax:
Function Set_Screen_ActiveSubformControl()
Return:
True: Screen_ActiveSubformControl was set to the subform control
that contains Screen.ActiveControl.
False: Screen.ActiveControl does not exist or is not in a subform.
Option Explicit
Dim Screen_ActiveSubformControl As Control
Function Set_Screen_ActiveSubformControl()
Dim frmActive As Form, ctlActive As Control
Dim hWndParent As Long
' Clear the control variable.
Set Screen_ActiveSubformControl = Nothing
' Assume a subform is not active.
Set_Screen_ActiveSubformControl = False
' Get the active form and control.
On Error Resume Next
Set frmActive = Screen.ActiveForm
Set ctlActive = Screen.ActiveControl
If Err <> 0 Then Exit Function
' Get the unique window handle identifying the form
' .. the active control is on.
hWndParent = ctlActive.Parent.Properties("hWnd")
' If the active form window handle is the same as the window
' handle of the form the active control is on, then we are on the
' mainform, so exit.
If hWndParent = frmActive.hWnd Then Exit Function
' Find a subform control that has a window handle matching the
' .. window handle of the form the active control is on.
Set_Screen_ActiveSubformControl = FindSubform(frmActive, _
hWndParent)
End Function
Function FindSubform(frmSearch As Form, hWndFind As Long)
Dim i As Integer
On Error GoTo Err_FindSubForm
' Assume we will find a subform control with a window
' .. handle matching hWndFind.
FindSubform = True
' Visit each control on the form frmSearch.
For i = 0 To frmSearch.Count - 1
' If the control is a subform control...
If TypeOf frmSearch(i) Is SubForm Then
' .. does the window handle match the one we are looking
' for?
If frmSearch(i).Form.hWnd = hWndFind Then
' We found it! Set the global control variable and exit.
Set Screen_ActiveSubformControl = frmSearch(i)
Exit Function
Else
' Otherwise, search this subform control (recursively)
' .. to see if it contains a sub-subform control
' .. with a window handle matching the one we are
' .. interested in.
' If we found a subform control, then exit.
If FindSubform(frmSearch(i).Form, hWndFind) Then
Exit Function
End If
End If
End If
Next i
Bye_FindSubform:
' If we didn't exit the function earlier, then there is no
' .. subform or sub-subform control on this form that has a window
' .. handle matching the one we are interested in, so return false.
FindSubform = False
Exit Function
Err_FindSubForm:
MsgBox Error$, 16, "FindSubform"
Resume Bye_FindSubform
End Function
Macro Name Action
-------------------
{F2} RunCode
AutoKeys Actions
--------------------------------------------
RunCode
Function Name: =DisplayActiveSubformName()
Function DisplayActiveSubformName()
Dim Msg As String
Dim CR As String
CR = Chr$(13) ' Carriage Return.
If Set_Screen_ActiveSubformControl() = False Then
Msg = "There is no active subform!"
Else
Msg = "Active Form Name = " & Screen.ActiveForm.Name
Msg = Msg & CR
Msg = Msg & "Active ControlName = " & Screen.ActiveControl.Name
Msg = Msg & CR
Msg = Msg & "Active Subform ControlName = "
Msg = Msg & Screen_ActiveSubformControl.Name
Msg = Msg & CR
Msg = Msg & "Active Subform Form Name = "
Msg = Msg & Screen_ActiveSubformControl.Form.Name
End If
MsgBox Msg
End Function
Name: Testing 123
SourceObject: Orders
Keywords: kbprb KB139040