Assign a subform instance to the parent form (1 Viewer)

bayouc

Registered User.
Local time
Today, 07:50
Joined
Jan 19, 2010
Messages
13
Hello Access gurus...

I have a subform that I initialize in different ways... I use a function to initialize the subform (a function because I on occassion I end up nesting subforms), but cannot figure out how to hand the result of the function back to the form.

Code:
Dim frm As New Form_frmMainForm
Dim sfrm As Form_frmSubForm
Set sfrm = BuildSubform(frm.SubForm.Form, variousparameters)
Set frm.SubForm.Form = sfrm

.form is a read only string... so this does not work.

How can I hand my initialized subform back to the parent form? Or am I breaking good programming rules by trying to hand back and forth instances...

Thanks kindly!
 

ChrisO

Registered User.
Local time
Tomorrow, 01:50
Joined
Apr 30, 2003
Messages
3,202
What is you code supposed to do?

Can you please post a small Access 2003 demo?
 

bayouc

Registered User.
Local time
Today, 07:50
Joined
Jan 19, 2010
Messages
13
Chris,

I was afraid you would ask that...

This probably opens a whole new set of problems... but what I am trying to do is dynamically manage my forms and subforms through settings in a table. The database is designed to manage and track a large workflow (tell the user to do this step first, this second, etc). The problem was that the workflow itself is subject to frequent change. To avoid having 1500 forms I wanted to use one form (with consistent view and behaviors) that I initialized based on the settings in my "process" tables.

So - when you open the database it should work, and show you data from two tables (A and B) based on the link I defined in a "Process" table. However, try to add another level of embedding and it fails because the subform does not load - so its properties cannot be modified.

In the process table add a "3" to the second row, then throw a stop in the Main module to see the problem. I should add that, during the stop, if you manually set focus to the form and its subforms (which loads the embedded subform) the code works again.

Note there is no error handling because I just threw this together specifically as a demo.

I welcome the inevitable criticism of the rest of the code, too. I'm sure it needs a lot of corrections. Actually - the entire approach may be poor for all I know...

Cheers,
James
 

Attachments

  • NestedSubforms.mdb
    368 KB · Views: 111

ChrisO

Registered User.
Local time
Tomorrow, 01:50
Joined
Apr 30, 2003
Messages
3,202
I don’t know.

Subform1 control exists because it was created in design mode. If I understand correctly, you would need to create Subform2, Subform3… controls at runtime. That should mean going into design mode at runtime and that can’t be done in an MDE file.

I think you might be better off thinking of another way to do it.

I also don’t understand that if you have so many forms, which would be identical in design and only vary in data, why you can’t swap the data in/out of a single fixed form.

If you wanted to you could, I guess, store the different SQL of the Record Source property in a table and step through the table. You could also store the different label descriptions in the same table.

But I does seem to me to be a bit overly complicated the way you are trying to do it.
 

bayouc

Registered User.
Local time
Today, 07:50
Joined
Jan 19, 2010
Messages
13
Thanks for taking a look.

I am going to basically use just that form and swap data in and out as you suggested (most of the data does not require the nested subforms). But how to do that with the data that required nested subforms...

But what you said makes a lot of sense. I think it is probably easiest to just make several copies of frmSubform - so that each is already initialized, and then use each of those copies for each nesting level. I should only ever need to nest 4 deep, so that is not too bad.

Thanks for the help!
 

bayouc

Registered User.
Local time
Today, 07:50
Joined
Jan 19, 2010
Messages
13
Did anything else look out of the ordinary in the file I attached?
 

wazz

Super Moderator
Local time
Today, 22:50
Joined
Jun 29, 2004
Messages
1,711
haven't looked at this much but you said: if you manually set focus to the form and its subforms (which loads the embedded subform) the code works again.

what if you set focus in code as needed?
 

bayouc

Registered User.
Local time
Today, 07:50
Joined
Jan 19, 2010
Messages
13
I tried setting focus to the subform control, but get a 2110 error which I guess means I cannot setfocus to a subform control. I also tried setting focus to the form itself... but still it only works to load the first subform, not the nested subform.

I would give up were it not for the fact that it works when manually setting focus when in debug. I'll try a couple more approaches to setfocus to see if I can get it to load.
 

vbaInet

AWF VIP
Local time
Today, 14:50
Joined
Jan 22, 2010
Messages
26,374
Set focus to a control in the subform like this:

Me.NameOfSubform.Form!NameOfControl.SetFocus

or you can do it without the "Me." binding
 

wazz

Super Moderator
Local time
Today, 22:50
Joined
Jun 29, 2004
Messages
1,711
i seem to recall someone trying to do this a long time ago. do a search for someone trying to nest subdatasheets. i don't think it was possible programmatically. (as i recall he seemed to know what he was doing, too). i think he was trying to access the table-level property 'subdatasheet' but also couldn't work out the nesting.

edit: the thread might have been lost when the site was hacked a while back.
 

bayouc

Registered User.
Local time
Today, 07:50
Joined
Jan 19, 2010
Messages
13
Well - I got it to work, though not exactly in an elegant manner. By making three duplicate copies of the subform, and then nesting them one inside the other, they will all load and I can access all their controls.

It would be much nicer to simply use one form three times (nesting within itself) - but for whatever reason it will not load the form and assign it to the control.

If you are interested, the attached mdb has sample code which at least gets the job done.

Thanks very much everybody for your help - got me pointed in the right direction!
 

Attachments

  • NestedSubforms.mdb
    432 KB · Views: 127

Users who are viewing this thread

Top Bottom