Link to home
Start Free TrialLog in
Avatar of ramrom
ramromFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of peter57r
peter57r
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

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.
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

"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
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
@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.
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.
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.
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
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
Avatar of ramrom

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ramrom

ASKER

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.