Solved

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

Posted on 2011-09-15
14
274 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
Comment Utility
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

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility

"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
Comment Utility
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
Comment Utility
@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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 26

Expert Comment

by:Nick67
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now