How to: Keep the user's arrangement
Let each user find a form arranged the way they left it, store the arrangement in a place of your own, such as a settings table on the server, or offer a button that restores the original design.
Requires WKL Forms Object Library 1.28 or later.
Solution overview
Without any code, both layout controls remember the arrangement for each Windows user
(AutoSaveLayout, on by default) under a key (LayoutKey, by default „form name.control name“).
Layout returns and accepts the arrangement as text for code that stores it elsewhere; Reset
restores the design and removes the remembered arrangement.
WklGrid and WklTreeList do the same for their columns (widths, order, hidden columns, bands,
sorting, grouping, a grid's column filters, and the height of its detail views), with the same
properties, the same table, and the same rules.
Implementation
The default needs no code: every change by the user or by code is saved right away and restored the next time the form opens. A button that restores the design:
Private Sub cmdDefaultLayout_Click()
Me!Layout1.Object.Reset ' also removes the user's row in USysWklLayout
End Sub
The same for the columns of a grid:
Private Sub cmdDefaultColumns_Click()
Me!Grid1.Object.Reset
End Sub
Two forms that share one arrangement, or the same form name in two documents with UserForms, need a key of their own (designer's Behavior page, Key field, or code that runs before the form is shown):
Private Sub Form_Open(Cancel As Integer)
Me!Layout1.Object.LayoutKey = "Admin.MainLayout"
End Sub
To use your own storage instead of the built-in one, turn AutoSaveLayout off and save the text:
Private Sub Form_Open(Cancel As Integer)
With Me!Layout1.Object
.AutoSaveLayout = False
.Layout = Nz(DLookup("LayoutText", "tblUserSettings", "UserName = '" & Environ("USERNAME") & "' AND FormName = 'frmAdmin'"), "")
End With
End Sub
Private Sub Layout1_AfterLayoutChange(ByVal Pane As String)
SaveUserSetting "frmAdmin", Me!Layout1.Object.Layout ' your own procedure
End Sub
Remarks
- In Access, the arrangement is saved in the local table
USysWklLayoutof the database that contains the form, one row per Windows user (DOMAIN\name) and key; theUSysprefix makes it a hidden system table. A front end copied to each workstation keeps arrangements locally, and one shared on a terminal server keeps each user's arrangement separate. On Excel and Word UserForms, it's saved underHKEY_CURRENT_USER\Software\The White Knight Labs\Access Framework\Layouts. - An arrangement that matches the design isn't saved, so
ResetandRefreshleave nothing behind. Layoutset in code before the form is shown takes precedence over the remembered arrangement; code that stores arrangements elsewhere is never overridden.- The text is JSON (
{"format":"WklDockLayout",...}or{"format":"WklSplitter",...}); text from the other control or unreadable text raiseswkeInvalidValue. Areas missing from older text keep their design, so saved text survives changes to the form. - A read-only front end or a rejected write doesn't break the form; the arrangement simply isn't remembered.
- Grids and tree lists save only differences. A column the user never changed follows later changes to the design; columns removed from the design are ignored, and new ones appear as designed. The design is what the control shows when it's first displayed, including any changes that
Form_Loadmakes in code. Selection, search text, and expanded records aren't remembered. - A grid's detail views have no arrangement of their own; their height is part of the grid's.
Related topics
- API reference: WklDockLayout.Layout, WklSplitter.Layout, WklGrid, WklTreeList:
AutoSaveLayout,LayoutKey,Reset