Solved

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

Posted on 2011-09-15
14
315 Views
Last Modified: 2012-06-27
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
Comment
Question by:ramrom
  • 5
  • 2
  • 2
  • +4
14 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 125 total points
ID: 36543833
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
 
LVL 10

Expert Comment

by:plummet
ID: 36543981
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
 
LVL 26

Expert Comment

by:Nick67
ID: 36544606
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36545015
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
 
LVL 75
ID: 36545265

"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
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 36545294
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
 
LVL 26

Expert Comment

by:Nick67
ID: 36545305
@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
 
LVL 26

Expert Comment

by:Nick67
ID: 36545401
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
 
LVL 44

Expert Comment

by:Leigh Purvis
ID: 36545477
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 36545490
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
 
LVL 26

Expert Comment

by:Nick67
ID: 36545739
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
 
LVL 17

Author Comment

by:ramrom
ID: 36554461
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
 
LVL 26

Assisted Solution

by:Nick67
Nick67 earned 125 total points
ID: 36554543
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
 
LVL 17

Author Closing Comment

by:ramrom
ID: 37021334
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

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question