How to: Link two grids without code
Show the orders of the customer selected in one grid in a second grid (or in an Access subform) on the same form, without any VBA.
Requires WKL Forms Object Library 1.23 or later.
Solution overview
The detail's LinkMasterFields names the master grid instead of a form field: Grid1 stands for
its key, and Grid1.FieldName for a field of its selected record. LinkChildFields names the
field in the detail records that holds that value. An Access subform names the grid in its own
LinkMasterFields.
Implementation
Grid to grid (on the designer's Link page, in the property sheet, or in code):
With Me!Grid2.Object
.LinkChildFields = "CustomerID"
.LinkMasterFields = "Grid1" ' the key of the customer selected in Grid1
End With
' A field of the selected record other than the key:
Me!Grid3.Object.LinkChildFields = "CustomerID"
Me!Grid3.Object.LinkMasterFields = "Grid2.CustomerID" ' Grid2 shows orders
Grid to subform (property sheet of the subform control, Data tab):
| Property | Value |
|---|---|
| Link Master Fields | Grid1 |
| Link Child Fields | CustomerID |
Remarks
- The detail follows right away when code sets the master's
SelectedKey, and after a short pause (120 ms) while the user moves through the master with the arrow keys, so holding down a key doesn't query the detail for every record it passes. - If nothing is selected in the master, or a group row or the new-record row is selected, the detail shows no records, just as a subform does for a new master record.
Grid1.FieldNamecan name the key, a column, or any other field in the master's record source; a field that isn't a column is read with a single statement and kept until the master moves on.- The master can be any WKL control on the form:
WklGrid,WklTreeList,WklTreeView, orWklGantt. You can mix both kinds of entries:"Grid1;Year"takes the key ofGrid1and the form'sYearfield. - An Access subform reads the grid's selected key through its
Value. Access doesn't refresh a subform when an ActiveX control's value changes, so the grid requeries the subforms on its form whoseLinkMasterFieldsnames it. Only a grid with a single key field can drive a subform; for a different field of the selected record, link a second grid withGrid1.FieldName. - In Design view with
LivePreview, the detail shows the records for the first record in the master's record source. - In server mode, the master condition is part of every statement the detail sends.
Related topics
- How to: Link a tree to a master record (the form, a recordset or values from code as master)
en/wiki/msaccess/comctl/how-to/link-two-grids.txt · Zadnja sprememba: uporabnika Wolfgang van der Stille