here you go
put in a separate module and use in your code something like
dim frm as form
frm=theActiveForm
So you may need to adapt it in some way.
Code:
Option Compare Database
Option Explicit
'form management
'does not require references or modules
Function theActiveForm() As Form
'finds and returns the active form object
Dim frmActive As Form, ctlActive As Control
Dim hWndParent As Long
' Clear the control variable.
'theActiveForm = Nothing
' Assume a subform is not active.
'Got_ScreenActiveForm = False
' Get the active form and control.
On Error Resume Next
Set frmActive = Screen.ActiveForm
Set ctlActive = Screen.ActiveControl
If err = 0 Then
' 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
Set theActiveForm = Screen.ActiveForm
'Got_ScreenActiveForm = True
Else
' Find a subform control that has a window handle matching the
' .. window handle of the form the active control is on.
Set theActiveForm = theActiveSubform(frmActive, hWndParent)
End If
End If
End Function
Private Function theActiveSubform(frmSearch As Form, hWndFind As Long) As Form
'finds the active form if a subform
Dim i As Integer
On Error GoTo noSubform
' 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 theActiveSubform = frmSearch(i).Form
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 Not theActiveSubform(frmSearch(i).Form, hWndFind) Is Nothing Then Exit Function
End If
End If
Next i
noSubform:
Set theActiveSubform = Nothing
End Function