How to: Show detail views below a record
Let the user expand a customer in the grid to see that customer's orders and notes right below it, each on its own tab, without a subform.
Requires WKL Forms Object Library 1.23 or later.
Solution overview
A detail view names a record source, its key, and the link between the detail and master records
(LinkChildFields and LinkMasterFields, just as with a subform). AddDetailView adds a view,
and the definitions are saved with the form in DetailViews. The user expands a record by
clicking its arrow or pressing Ctrl and +; code uses ExpandDetail, CollapseDetail, and
DetailGrid.
Implementation
With Me!Grid1.Object
.AddDetailView "Orders", "tblOrders", "OrderID", "CustomerID", "CustomerID", "Orders", "Item;Quantity"
.AddDetailView "Notes", "tblNotes", "NoteID", "CustomerID", "CustomerID", "Notes"
.DetailHeight = 2400 ' twips, the same for every expanded record
End With
' Open the orders of customer 3 and read them:
Me!Grid1.Object.ExpandDetail 3, "Orders"
Debug.Print Me!Grid1.Object.DetailGrid(3, "Orders").RecordCount
The designer's Detail views page edits the same list (adding, removing, and ordering views, the fields of each view, and the height).
Remarks
- Each tab is a separate grid that reads through the same Access database, in the same
DataMode, with the master grid's font, colors, andAllowEditing. Its columns are theColumnFields, or every field in the source except the link fields, and their widths fit the records shown. A column header is the field name or the alias in an SQL source; a table field'sCaptionisn't read. Use aliases when the headers should be in a different language than the field names. - A view doesn't store any per-column settings (width,
ReadOnly, format, lookup, or status). Changes made to columns throughDetailGridlast only while the record stays expanded, so make such changes inDetailExpandto apply them every time. - Detail views have a single level: a detail view can't have detail views of its own.
- When the master grid has
AllowAdditions, a detail view has its own row for new records (NewItemRow); a record added there gets the master record's values in its link fields, just as in a subform. Editing and deleting follow the master grid'sAllowEditingandAllowDeletions. - A detail view's events don't reach the form module;
DetailGridreturns its grid so code can work with it. When the data source rejects a change, the detail view marks the row and shows the message itself. The object stays valid as long as the record remains expanded. Requery, a changed binding, or a new master record in a linked grid closes every expanded record, because its detail records might no longer exist.DetailExpandandDetailCollapsereport changes made by the user and by code, but not these.- A tab whose records can't be read shows the reason instead of a grid; the master grid keeps working.
- The rows below an expanded record move down; scrolling, clicks, and UI Automation take the gap into account. The records of a master grid with a million records stay virtual.
Related topics
en/wiki/msaccess/comctl/how-to/show-detail-views.txt · Last modified: by Wolfgang van der Stille