How LinkChildFields and LinkMasterFields with code?

Luthius

Active Member
Joined
Apr 5, 2011
Messages
324
How use LinkChildFields and LinkMasterFields with code?
I have in the main form the primary key IDRecord, and would link it Key with the child field by code.

I tried this in my main form:
Private Sub Form_Current()
Me.MySubForm.LinkChildFields = Me.IDRecord
Me.MySubForm.LinkMasterFields = Me.IDRecord
End Sub

Ps.:
My form is not linked in the table. I use code to get the records.
In MySubForm I don't have any reference to my main form.

Luthius
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Is there an ID field in the subform that matches the primary key field of the subform?
eg...
Customers form has CustomerID.
The subform (Orders) should have OrderID, and CustomerID.
You then link CustomerID in both forms using Master and Child.

If you don't have a setup like that, you can't use the Master and Child fields.

Denis
 
Upvote 0
Let me give an example:
I have an order table (tblOrder) and table tblOrderDetails.

tblOrder
idOrder = Primary Key

tblOrderDetails
idOrderDetail = Primary Key
idOrder = Foreign Key

I have an unbound form (frmOrders) with records from the table tblOrder that I get through a SQL statement

Example:
SELECT * FROM tblOrder;

Form name = frmOrders (Main Form where you put the subform control)
Form name = subFrmOrderDetails (subform that 'inherits' the information from idOrder through the relationship between the tables)
The SQL statement for the subform like this:

SELECT * FROM tblOrderDetails;

That it,if necessary add an order detail, to insert the data into the subform it will inherit the idOrder from frmOrders.

I want to use the Master and Child fields through VBA code using LinkChildFields and LinkMasterFields properties.

Luthius
 
Upvote 0
It sounds to me like the main form *is* bound, unless I misunderstand the setup.
To clarify:
Do you need to use code to move between records on the main form?
Are you using a disconnected recordset?

Denis
 
Upvote 0
Luthius

If you did use bound forms, and I agree with Denis - it sounds like you are, then you should be able to set the LinkChild and LinkMaster fields using the wizard that
pops up when you add the subform to the main form.

You shouldn't need code - the only other thing you would perhaps need to do is set the default value for the ID field to the ID field from the main form.
 
Upvote 0
I have an unbound form (frmOrders) with records from the table tblOrder that I get through a SQL statement.

To clarify We can use as example the nothwind database where we have:

Form:
"Orders"
SubForm
"Orders Subform"

But in my case both forms are unbound.

I'm using OOP (Object-oriented programming) tha'ts the reason that all my forms are unbound.
I have one sample file. If you can send me by PM your email, I'll send it to you.
 
Upvote 0
How are you populating the form with data?

If you do have unbound forms I don't think you can use the LinkMaster and LinkChield properties.

Unless I'm missing something you'll need to populate the subform in much the same way as you've populated the main form.
 
Upvote 0
My form is not linked in the table. I use code to get the records.
In MySubForm I don't have any reference to my main form.

You probably *could* set a reference to the main form - link the child field to the master field. Bind the subform to the order details table.

I find it convenient not to use the same form for viewing data as entering or editing data. Your data entry form need not be linked - it just needs to grab the Order ID when a records is added. A data viewing form is better bound to the data source and letting Access handle the work of keeping the main and subforms synchronized. Just my two cents - don't know if it would work for you or not.
 
Upvote 0
I know that is easier work with bound forms, however I'm using OOP, and it is a new way to see the things.And my database will be used in corporate network.
Does anyone?

Thanks
 
Upvote 0
I forgot to ask, why are you using OOP for this?
 
Upvote 0

Forum statistics

Threads
1,224,506
Messages
6,179,159
Members
452,892
Latest member
yadavagiri

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top