How to: Show a status from a field of the record
Show the state of the current record (“running”, “stopped”, “failed”) as a colored, one-line status
on a single form or in the form header, with a WklStatusLabel bound to a field.
Requires WKL Forms Object Library 1.23 or later.
Solution overview
- Bind it like a text box: Control Source (
ControlSource) names the field or an expression. Access writes the value of the current record to the label on every record change; the label never writes back. - Values become text and kind through entries (
AddStatusItem): one entry per value, with the text shown and the kind (wskOk,wskWarn,wskBad,wskInfo,wskNeutral) that sets the colors. Values without an entry show the value formatted withFormat, in the designedKind. - Or take the kind from another field (
KindField) when the record stores it: a number 0-5 or a name such asokorbad. - The look is chosen with
Display: a pill (wsdPill), a dot (wsdDot) or a symbol (wsdSymbol) before the text;Captionputs plain text in front (“Service”).
Implementation
A form frmServices bound to tblServices, whose field State holds 0, 1, or 2, has a status
label stState. The entries are added once in Design view and stored with the form:
' Once, in design view (for example from the Immediate window); saved with the form.
DoCmd.OpenForm "frmServices", acDesign
With Forms!frmServices!stState
.ControlSource = "State"
With .Object
.Caption = "Service"
.Display = wsdPill
.ClearStatusItems
.AddStatusItem 0, "stopped", wskNeutral
.AddStatusItem 1, "running", wskOk
.AddStatusItem 2, "failed", wskBad
.NullText = "unknown"
End With
End With
DoCmd.Close acForm, "frmServices", acSaveYes
When the record stores the kind in a field of its own (Kind: ok, warn, bad, and so on) and
the text in another (Message), bind the text and name the kind field:
With Forms!frmServices!stMessage
.ControlSource = "Message"
.Object.KindField = "Kind"
End With
While a long-running action runs, show it with the busy pulse:
Me!stState.Object.Busy = True ' ... the action ... Me!stState.Object.Busy = False
Remarks
- Where you can use it: single forms, form headers and footers, and Excel and Word UserForms, but not the detail section of a continuous form: Access creates only one instance there, and a bound one keeps Access from opening the form. For a status per record in a list, use a status column of
WklGrid(wedStatus), which takes the same entries. - Code changes on the same record: the label follows record changes on its own; when code changes the kind field without moving to another record, call
Refresh. - Never color alone: the text is always shown, and high contrast is respected; screen readers announce the caption, the text, and the kind.
Related topics
en/wiki/msaccess/comctl/how-to/show-a-status-from-a-field.txt · Last modified: by Wolfgang van der Stille