Get current subform control (container) name (1 Viewer)

IvanM

Registered User.
Local time
Today, 15:31
Joined
Jul 15, 2016
Messages
20
I have a parent form with 2 subform objects, using all the same subform.

Parent Form = FRM_Report_Appointments
Subform Control 1 = App_Sub_01 (using form: FRM_Appointments_SUB)
Subform Control 2 = App_Sub_02 (using form: FRM_Appointments_SUB)

How can I find the name of the control (e.g. "App_Sub_01") in an OnLoad event in each subform ?

Any help would be greatly appreciated!!
 
If you want the reference for the control maybe one of these site will help.

http://access.mvps.org/access/forms/frm0031.htm
http://ss64.com/access/syntax-references.html

Also you can use the Intellisense in the query designer to help you get references right. To do that do the following.

  1. Open all the forms involved (Helps the Intellisense somehow)
  2. In the CREATE tab click on Query Design
  3. Click close on the Show Table dialog
  4. In any criteria field type forms! (a drop down will give you the list of forms)
  5. Proceed to build up your reference by making a selection and them typing ! or .

Whether you type a period or exclamation point depends on what you want next. Typing a exclamation point will give you what's in the collection and typing a period will give you properties. If you don't see anything at all after typing forms! then restart your application. Sometimes the Intellisense seem to quit for no reason.
 
Hi Sneuberg,

thank you for those references, I've already seen those links and neither provide the answer. I've also used the intellisense, to no avail.

The issue is that I need the container name of the subform (the subform's actual control name).

I have 2 instances of the same form within subforms on one main form, what differs is the container name (the parent forms controls).

I need to display 2 sets of data in these 2 subforms, I want to reference the container name so I know which set of data to display in which subform.

The only site I found which looks like it has the answer has a link which is dead...:(

To summarise, I need to reference the container name from within the subform... it must be me. something because I can't refer to it directly by name...
 
The subforms name is easy enough to get to in the main form. As shown in the attached database [Forms]![FRM_Report_Appointments]![App_Sub_01].[Name] will get you that but now I think I understand you want to know the parent subform control from the form contain in it. Let's assume a magic function GetParentControl that would do this then I'm guessing you want to do something like this in the form load of these forms.

Code:
If GetParentControl = "App_Sub_01" then
      Me.RecordSource = "Query1"
Else
      Me.RecordSource = "Query2"
End If

If this is the case why not do this in the form load of the main form where it's easy to do? It would be something like

Code:
Forms![FRM_Report_Appointments]![App_Sub_01].[Form].[RecordSource] =  "Query1"
Forms![FRM_Report_Appointments]![App_Sub_02].[Form].[RecordSource] =  "Query2"
 

Attachments

Last edited:
Hi Sneuberg,

you're exactly right, that's precisely what I was trying to achieve, to get the equivalent of a magical GetParentControl :)

I have indeed already used your suggested method of referring to it from the main form, but I have 3 reasons for wanting to find the answer -

1. I'm sure it's possible, and I want to find out regardless, I'm inquisitive like that.

2. If I can refer to the container from within the subform, if I use it anywhere else I don't need to change anything, whereas using the current method, if I place it within another form, I'll be re-entering the OnLoad code in that form too...

3. I don't like being beaten :D

Thanks for all your help, genuinely appreciated.
 
What about
Code:
Me.Parent.ActiveControl.Name
?

I've tested it by dropping a button on a subform, put the subform in a parent form, gave the subform control a wacky name, and had the button run this code:
Code:
MsgBox Me.Parent.ActiveControl.Name
The message box that pops up gives the name of the active subform control.

Of course, it's also quite possible I'm misunderstanding what you're going for...it's been one of those days.
 
Not sure I understand the problem but here's a couple of comments

me.name will give the name of the object that the code is run in. So if you ran this in the Onload event of of subform1 it would give "subform1" as the result.

me.parent will refer to a parent control.

Edit: Sorry, I see Frothingslosh already mentioned the parent thing.
 
What you could do is use the IS operator to compare form objects. Write code on the subform that enumerates the parent form's subform controls, and for each subform, evaluate an expression like this . . .

Code:
Me.Parent.<currentsubformcontrol>.Form IS Me

If that evaluates to "True", then Me.Parent.<currentsubformcontrol> contains the form, Me.

To make that process eaiser, you could write a public collection on the parent form that only exposes subform controls, like . . .

Code:
private m_sfms as VBA.Collection

Property Get Subforms As VBA.Collection
   If m_sfms is nothing then 
[COLOR="Green"]      'on first reference, if the object doesn't exist, create it[/COLOR]
      set m_sfms = new vba.collection
      dim ctrl as control
      for each ctrl in me.controls
[COLOR="Green"]         'find the subforms, and add them to the custom collection[/COLOR]
         if ctrl.controltype = acSubform then m_sfms.add ctrl
      next
   end if
   set subforms = m_sfms
End if

So then code on the subform is simplified to . . .

Code:
private function GetParentSubformControl As Control
   dim ctrl as control
   for each ctrl in me.parent.subforms
      if ctrl.form IS Me then
         set GetParentSubformControl = ctrl
         exit for
      end if
   next
End Function

Make sense?
 
I still can't see why the OP needs the name of the container. If the OP wants to change the datasource of a sub from the sub then it's me.recordsource.

The only reason to need the container is maybe to set the master/child properties (?). Maybe a wrapper class might come in handy.
 
The OP has given some very good reasons.

This is how I like to do things when I got the time.


1. I'm sure it's possible, and I want to find out regardless, I'm inquisitive like that.

2. If I can refer to the container from within the subform, if I use it anywhere else I don't need to change anything, whereas using the current method, if I place it within another form, I'll be re-entering the OnLoad code in that form too...

3. I don't like being beaten :D



Sent from my SM-G925F using Tapatalk
 
What about
Code:
Me.Parent.ActiveControl.Name
?

I've tested it by dropping a button on a subform, put the subform in a parent form, gave the subform control a wacky name, and had the button run this code:
Code:
MsgBox Me.Parent.ActiveControl.Name
The message box that pops up gives the name of the active subform control.

Of course, it's also quite possible I'm misunderstanding what you're going for...it's been one of those days.

Unfortunately it doesn't work in the subform's on load event. So far I don't believe any post has fulfilled the OP's desires.
 
I still can't see why the OP needs the name of the container.
I think, in part, the OP just wants to see if it can be done. But one reason it might be useful for real is to change out a subform based on user interaction with that subform. And in the case that there are multiple instances of that SAME subform on the main form, you would have to solve this.

And actually, I could put this to use in a calendar form. Say you make a form for a week with seven subforms, one for each day, so you have 7 instances of the same form on the main form, each showing data for that corresponding day. Currently, I would also put seven hidden textboxes and link my subforms via LinkMaster and LinkChildFields properties to date values I would control--in the textboxes--programmatically. Does that still make sense? But if the subform could determine it's host control by name, and the names contain a numeric value (0-6) indicating their offset from some fixed Sunday, then each subform could actually figure out it's own recordsource completely programmatically, which is to say, without using the LinkMasterFields and LinkChildFields properties. This would make that form simpler and potentially faster.
 
There, how long did that take?
 

Attachments

I did it a slightly different, and not so elegant way as Mark!

In the example database attached the Name of the subform window, the subform/subreport controls name is stored in the Private Property Statement "prpThisSubFrmWinNameIs"
 

Attachments

Last edited:
The OP has given some very good reasons.

This is how I like to do things when I got the time.






Sent from my SM-G925F using Tapatalk


I was referring to the OPs comment:
I need to display 2 sets of data in these 2 subforms, I want to reference the container name so I know which set of data to display in which subform.

My concern is the OP may not be aware that an object does not need to know its name or reference to refer to itself.

Granted we are all interested in solving problems regardless of their direct value.
 
What about
Code:
Me.Parent.ActiveControl.Name
?

I've tested it by dropping a button on a subform, put the subform in a parent form, gave the subform control a wacky name, and had the button run this code:
Code:
MsgBox Me.Parent.ActiveControl.Name
The message box that pops up gives the name of the active subform control.

Of course, it's also quite possible I'm misunderstanding what you're going for...it's been one of those days.

Hi Frothingslosh,

that works when I do exactly what you've done, so excellent! Nice one!! I knew there had to be a way of retrieving the parent control name somehow. Genuinely appreciate this, I spent a couple of hours on this yesterday searching and testing.
:)
 

Users who are viewing this thread

Back
Top Bottom