In the whole discussion and in the presented solutions it has not yet been considered that one can work with several instances of the same form at the same time - and that is anything but confusing if I already have a good and universally usable form.
There can be different design options for these different instances:
- Multiple instances as main form (input form versus data display form)
- Use as a main form and as a subform of another form
- Use as a multiple subform with different data displays in the same main form
If there are several instances, addressing the form by name is not an option. For targeted work, addressing by reference is necessary. It's also important to note that a form is a class and its instance must be able to determine for itself what it is, where it resides, and what it's called as a control, to perform specific actions or not to execute.
With that in mind, I've added something to the DB file of @arnelgp to allow for easy testing.
The simple function:
Code:
' sample call
Private Sub Form_Open(Cancel As Integer)
Dim SourceObjectName As String
Dim ParentFormName As String
Dim UFoControlName As String
Debug.Print
Debug.Print "Is SubForm: " & IsSubform(Me, SourceObjectName, ParentFormName, UFoControlName)
Debug.Print "SourceObjectName: " & SourceObjectName, _
"ParentFormName: " & ParentFormName, _
"UFoControlName: " & UFoControlName
End Sub
Code:
Public Function IsSubform(AnyForm As Access.Form, _
Optional ByRef SourceObjectName As String, _
Optional ByRef ParentFormName As String, _
Optional ByRef UFoControlName As String) As Boolean
SourceObjectName = AnyForm.Name
If SysCmd(acSysCmdGetObjectState, acForm, AnyForm.Name) = 0 Then
IsSubform = True
ParentFormName = AnyForm.Parent.Name
UFoControlName = SubformControlName(AnyForm)
Else
IsSubform = False
End If
End Function
Public Function SubformControlName(SubformOBJ As Access.Form) As String
Dim ctl As Access.Control
For Each ctl In SubformOBJ.Parent
If ctl.ControlType = acSubform Then
If ctl.SourceObject = SubformOBJ.Name Then
If ctl.Form.Hwnd = SubformOBJ.Hwnd Then
SubformControlName = ctl.Name
Exit For
End If
End If
End If
Next
End Function
Public Function IsLoaded(Search As String, Optional Scan As Object) As Boolean
Dim i As Object
Dim First As Boolean
If Scan Is Nothing Then First = True: Set Scan = Application.Forms
For Each i In Scan
If Not First Then
If i.ControlType = 112 Then
If Search = i(0).Parent.Name Then IsLoaded = True: Exit Function
IsLoaded = IsLoaded(Search, i(0).Parent)
If IsLoaded Then Exit Function
End If
Else
If Search = i.Name Then IsLoaded = True: Exit Function
IsLoaded = IsLoaded(Search, i)
If IsLoaded Then Exit Function
End If
Next
End Function
How many versions of code to detect a subform do we really need in this thread?
I gave code to detect if a form is open as a subform in post #20, then realised @cheekybuddha had given similar code in post #2
It seems to me that we are going around in circles...or am I still missing something?
@Efer and @isladogs, I have introduced and formulated an extension of requirements, which is also relevant to practice, at least in a better practice.
If a form instance is to check and recognize itself, questions as to whether the instance is loaded or whether the form even exists have long been answered and therefore do not need to be asked again.
So my answer is quite different from yours or any previous one. Let's see if anyone recognizes this on their own.
Now we are offering puzzles instead of solving problems.
It's at least your second try to offer a code and say go figure yourself what it does.
It was your answer to @Pat Hartmanhere
Do I have to be surprised if a description as text plus a complete open code plus a demo DB appears as a puzzle to a viewer who perceives this as a whole?
The video of Alan Cossey's excellent presentation to last week's Access Europe meeting is now available on YouTube: The Standard Module Object Generation (SMOG) add-in and supporting files are also now available free to all Access Europe subscribers from...
www.access-programmers.co.uk
The simultaneous use of multiple instances of the same form is familiar to some.
Do I have to be surprised if a description as text plus a complete open code plus a demo DB appears as a puzzle to a viewer who perceives this as a whole?
The video of Alan Cossey's excellent presentation to last week's Access Europe meeting is now available on YouTube: The Standard Module Object Generation (SMOG) add-in and supporting files are also now available free to all Access Europe subscribers from...
www.access-programmers.co.uk
The simultaneous use of multiple instances of the same form is familiar to some.
@ebs17 My English is not that good, so I really can't understand what you mean by that. Maybe if I watch the video and read the linked you sent I can find a relation between my comment, your reply and the contents, but for now, I'm terribly busy with my job.