Master/Child linking in code

D

DanJ

I have a Main form with a subform which I want to set the Master/Child
property for in code. In the Main forms Open event I have the following:

Me.Jail_Record_Act_Log.LinkMasterFields = "Jail_ID"
Me.Jail_Record_Act_Log.LinkChildFields = "Jail_ID"

Jail_ID is a long integer field and the control is the same name as the
field. Jail_ID is Primary Key for the recordsource of the main form and a
foreign key for the recordsource of the subform.

Jail_Record_Act_Log is the name of the subform control which holds the
subform of the same name.

When I open the main form I get the following error:

Run-time error '2101':
The setting you entered isn't valid for this property.

I experimented by manually adding the Jail_ID link in Design view and it
works fine if I save the form with the link already entered. But I want to
be able to change the link in code later.

I have also tried changing all the names so that the field name is different
than the control name and the subform name is different than the subform
control name.

Any ideas what is wrong?
 
K

Keith Wilby

DanJ said:
I have a Main form with a subform which I want to set the Master/Child
property for in code. In the Main forms Open event I have the following:

Me.Jail_Record_Act_Log.LinkMasterFields = "Jail_ID"
Me.Jail_Record_Act_Log.LinkChildFields = "Jail_ID"

Jail_ID is a long integer field and the control is the same name as the
field. Jail_ID is Primary Key for the recordsource of the main form and a
foreign key for the recordsource of the subform.

Jail_Record_Act_Log is the name of the subform control which holds the
subform of the same name.

When I open the main form I get the following error:

Run-time error '2101':
The setting you entered isn't valid for this property.

I experimented by manually adding the Jail_ID link in Design view and it
works fine if I save the form with the link already entered. But I want
to
be able to change the link in code later.

I have also tried changing all the names so that the field name is
different
than the control name and the subform name is different than the subform
control name.

Any ideas what is wrong?

I'm not 100% sure but I seem to remember once having trouble trying to set
the property in the form's Open event. I've used that syntax in the past
but in the After Update event of an option group. I had the "default"
setting set up using the properties palette in design view.

Also, make sure you're using the subform's object name rather than the name
you gave it when you saved it. It should show up in intellisense.

Another way to try is to set the properties of the Parent object from the
subform.

Keith.
www.keithwilby.co.uk
 
A

Albert D. Kallal

The forms open event Has the cancel option in it, the general use the forms
open event is to test the settings that particular controls, and exit
cancel=true to prevent the form from loading.

In other words the forms open event should not be used for setting up the
values in the actual form itself. So, if all the forms verification is
successful in your application in the form's open event, then any setup code
belongs in the next event that occurs, which is the forms load event.


try placing your code in the form's load event, it should work.

" the above tip in mind as to how you use one event appropriately, or the
other event appropriately. these two events in access forms are quite
distinct, and are something that V. B. developers never had, and it's a
wonderful separation all forms verification and testing to prevent a form
from loading, as opposed to wear your setup code for the form should go.


It also means as an experienced developer I will be looking your forms on
load event for setup and settings of variables etc and various forms
settings. And as a developer all also be looking in your forms open event
for code that will cancel or prevent the form from loading in the first
place....

In fact the more I think about this, this is an excellent question I would
ask when hiring a perspective access developer - to explain to me when and
how the difference between the two form events are to be used....

So you can modify or set of values of contorls in the forms open event, it's
too early of an event...you have to use the forms load event.
 
D

DanJ

I actually had changed it at one time to the Load Event but then changed it
back because it did not work there either and because the Access Help
indicated that it must be used in the Open event of the form.
 
D

DanJ

I actually thought that it might not belong in the Open event and changed it
to the Load event. That didn't work either so I opened the help for the key
word 'LinkChildFields' and it states the following:

"The properties can only be set in Design view or during the Open event of a
form or report."

Therefore, I moved in back to the Open event. Any other ideas?
 
A

Albert D. Kallal

DanJ said:
I actually thought that it might not belong in the Open event and changed
it
to the Load event. That didn't work either so I opened the help for the
key
word 'LinkChildFields' and it states the following:

"The properties can only be set in Design view or during the Open event of
a
form or report."

Therefore, I moved in back to the Open event. Any other ideas?

That has to be a documentation error. I have all kinds of code in production
and use by many clients, and I been doing this for years....

And, I just tested this stuff...the following code works on both on-open,
and on-load....


Me.child1_test.SourceObject = "contactChild Subform1"
Me.child1_test.LinkChildFields = "contact_id"
Me.child1_test.LinkMasterFields = "contactID"


So, I am 100% wrong in my suggestiong that you must use on-load. The above
code works just fine for Both on-open and on-load event.


About the only reason this would likely fail is if the form perhaps does not
have a record source. If the form does not have a record source, then you
can't set the sub-form relationships at that point in time.
 
D

DanJ

Thanks for your help. I never could get this to work from the Load or Open
events even though I verified that there is a valid recordsource in both the
Master and Child forms and that both recordsources contain the Jail_ID field.

However, when I moved the code to the After_Update event of a combobox on
the form, it worked correctly. Go figure...

Microsoft really led me wrong with their documentation. Thanks for setting
the record straight that it is OK to use this elsewhere.
 
A

Albert D. Kallal

DanJ said:
Thanks for your help. I never could get this to work from the Load or
Open
events even though I verified that there is a valid recordsource in both
the
Master and Child forms and that both recordsources contain the Jail_ID
field.

However, when I moved the code to the After_Update event of a combobox on
the form, it worked correctly. Go figure...

Microsoft really led me wrong with their documentation. Thanks for
setting
the record straight that it is OK to use this elsewhere.
I am not sure what you mean by "elsewhere". As I said, I have production
code running all over the place by even clients I never meet in person.

You are free and able to use my code example in both the on-open and on-load
event.
 
D

DanJ

What I meant by elsewhere is that the code can be used in other events
besides the on load and on open events of the form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top