• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

How to distinguish between instances of a subform from the subform's open event procedure.

I plan to have several instances of a subform on a main form. I'd like the subform's open event code to distinguish one instance from another.

The only way I have thought of is to access the (e.g.) name property of the subform's container control. But I don't seem to be able to a access that control from within the subform's code.

I'd like to avoid writing any main form code.

 Any ideas?
0
ramrom
Asked:
ramrom
  • 5
  • 2
  • 2
  • +4
2 Solutions
 
peter57rCommented:
You'll find code that you can use here..
http://datagnostics.com/dtips/subform.html

for example...using the doubleclick event of a field in a subform..

Private Sub Order_ID_DblClick(Cancel As Integer)
         MsgBox ParentSubformControl(Me).Name
End Sub
0
 
plummetCommented:
I'd add some code into the main form, I know you don't want to, but all you need to do is trap the enter event for the subform control, set a variable, and then you can use that to determine which one the user is in.

0
 
Nick67Commented:
My understanding is that sub-objects (forms and report) are instantiated BEFORE their parents so what you have asked for is probably a non-starter
I guess really the questions to ask are:
<I'd like the subform's open event code to distinguish one instance from another>
Why? To what end and in what place would you use that distinguishing information?
And why only in the subform's code?
Why would you like to avoid writing the main form's code?

Because what you've asked for I think is impossible on the face of it.
And I don't say impossible in regard to Access very often.
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
Jeffrey CoachmanMIS LiasonCommented:
ramrom,

As much this feature is requested (Creating "Instances" of a form).

I have never had a real, legitimate use for this.

As fellow expert,  can you explain what your ultimate goal is here, and why no other technique will get you what you are after?

Just curious...


JeffCoachman
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:

"I have never had a real, legitimate use for this."
"Why? To what end and in what place would you use that distinguishing information?"

Somewhere I have a 12 month calendar, built with two physical objects ... a Main form and a Calendar form, used as a subform.  There are 12 instances of the same physical calendar form on the main form.  As well, there are various controls on the main form to change the Year, starting Month, and so on.  

So ... there is one minor example.

mx
0
 
Leigh PurvisDatabase DeveloperCommented:
Can I just make sure that you check out the answer provided by Peter?
That is an answer to the question asked.
(It's not the only way - I've offered a slightly alternate method here.)

As for form instances - firstly I don't think this is applicable.  It's just a turn of phrase.
Technically - subforms do exactly that, create instances of forms and are not limited to one per parent form, or simultaneously opened form.  Subforms do create instances that we take for granted - but they're not members of the forms collection (because they're subforms).

Actual instancing - well that depends on the business requirements.
If you have a search results form, say, and there is a business requirement that allows you to display a detail form for any of the returned matches - and not only one, but any result you want to open simultaneously - then form instancing is applicable.  It's just one of those "If you've never been asked for it, you've never needed it" scenarios.

Cheers
0
 
Nick67Commented:
@mx
I can think of ways to use the same subform multiple times on a main form but I don't think you can code up anything to distinguish them from each other in the subform's OnOpen() event can you?

And if that isn't doable, then knowing a little more may help solve the OP's actual need rather than answering the nominal question.
0
 
Nick67Commented:
There's nothing wrong with the solutions suggested except, of course, that they are not and cannot be in the subform's OnOpen() event.
Which is what was asked.
Which I think may be impossible.

Hence I asked for more detail on why that particular event is the one being targetted.
0
 
Leigh PurvisDatabase DeveloperCommented:
Except that they are able to be called in that event.
The load order of forms often has a bit too much stock put in it.  We're talking UI objects here, rather than data accessibility.
0
 
Jeffrey CoachmanMIS LiasonCommented:
To All,

Yeah, I did not mean to hijack the thread, or change it's direction, ...it is just perhaps the entire concept of "Instantiating" a form has never been clearly explained in detail anywhere.

So I was just asking for the OPs reasoning...

I do think that this topic would be a great "Article" though...
Perhaps the confusion stems from the terminology "Copy"(an object) vs "Instantiate"(an object)


;-)

Jeff
0
 
Nick67Commented:
Leigh,
I stand corrected.
Peter's solution can be called from the OnOpen() event.
Presumably yours can as well

@ramrom
The first post was the right answer.

Thank you both.
I learned something new
Nick67
0
 
ramromconsultant Author Commented:
Thanks for the questions & comments. I come from Visual FoxPro which has a far better implementation of OOP than Access. I kep looking for ways to do in Access what is a no-brainer in FoxPro.

Peter's comment (#1) is how I have solved this problem in the past.I'm just looking for a better way.
0
 
Nick67Commented:
There are some things we have to live without in Access.  Control arrays being one.  Some day if MS ever quits focussing on idiocy like SharePoint we may get stuff like that.
0
 
ramromconsultant Author Commented:
Thank you all for the efforts. Confirms my negative opinion of Microsoft.

I will continue to use the resource wasting strategy of examining every control to see if it is a subform.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 5
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now