WklToggleSwitch
WklToggleSwitch class
Signature
Dim myWklToggleSwitch As WklToggleSwitch Set myWklToggleSwitch = Me!ControlName.Object
Summary
A Yes/No switch with immediate effect, as the Windows settings show one: an optional caption, the switch, and a state text such as “Ein”/“Aus”.
Parameters
None
Remarks
Use the switch where a check box would do and the change takes effect at once - a setting, an “active” flag, a lock. It binds like an Access check box: set its Control Source (ControlSource of the Access control) to a Yes/No field, or to a bit column of a linked SQL Server table. Access writes the field of the current record into Value on every record change.
When the user switches - a click, Space, or a screen reader's Toggle - the switch first asks Access whether the field may be edited. Access refuses for a control with Locked set to Yes and on a form that does not allow edits; the switch then stays as it is. Otherwise the switch changes and reports the new value; Access marks the record as changed and writes the value into the field when the record is saved at the latest, as for any edited control: Me!tglAktiv returns the new value of the switch at once, while Me!Aktiv can return the old value of the field until the save, so read the switch, not the field; Undo or Esc puts the switch back. Then the event Change occurs. For the immediate effect, save the record in it:
Private Sub tglAktiv_Change()
Me.Dirty = False
End Sub
Value set by code only shows a state: it is the path Access uses to show the record, so it writes nothing into the field. To change the field by code, change the field (Me!Aktiv = True).
Null - a new record, or a field without value - shows a gray switch with the knob in the middle and no state text; switching from Null turns the switch on.
Enabled, Locked, Visible, Tab Stop, and ControlTip Text are the properties Access gives every ActiveX control; the switch has none of its own. A disabled switch is drawn in gray.
Screen readers hear the switch as a button with the Toggle pattern, as Windows states its own switches: the name is Caption (or the control name when Caption is empty), the state is on, off or indeterminate, and the state text is read as its status.
A list column can show the same switch: wedToggle as Editor of a column of WklGrid or WklTreeList.
Do not put a bound switch into the detail section of a continuous form: Access creates one instance of an ActiveX control there that shows only the current record, and with a Control Source in the detail section Access does not finish opening the form. For a switch per record use a wedToggle column of WklGrid.
The switch also works on Excel and Word UserForms, unbound: set Value by code and read it in Change.
An ActiveX control: inserted into a form in design view (Form Design › Controls › ActiveX Controls); reports are not supported. VBA reaches it through the Object property of the control, where ControlName is the name of the control on the form.
| Member | Kind | Summary |
|---|---|---|
| `Value` | Property | Gets or sets the state of the switch. |
| `OnText` | Property | Gets or sets the text after a switch that is on. |
| `OffText` | Property | Gets or sets the text after a switch that is off. |
| `Caption` | Property | Gets or sets the caption before the switch. |
| `BackColor` | Property | Gets or sets the background color. |
| `ForeColor` | Property | Gets or sets the color of the caption and the state text. |
| `FontName` | Property | Gets or sets the font of the caption and the state text. |
| `FontSize` | Property | Gets or sets the font size in points. |
| `ColorScheme` | Property | Gets or sets the color scheme the control draws in. |
| Event | Summary |
|---|---|
| `Change` | Occurs after the user has switched and the new value has been reported to Access. |
Code example
None
Inheritance
None
Value property
Signature
Property Get Value() As Variant Property Let Value(RHS As Variant)
Summary
Gets or sets the state of the switch.
Parameters
None
Property value: Variant – True (on), False (off) or Null. When set, also -1, 0, a number (any value other than 0 is on) and the texts “Ja”/“Nein”, “Wahr”/“Falsch”, “True”/“False”, “Yes”/“No”, “Ein”/“Aus” (case does not matter); an empty text is off.
Remarks
Value is the bound property: Access writes the field of the Control Source into it on every record change, and reads it back into the field when the record is saved after the user has switched. Setting it by code shows the state without writing the field and without the event Change. A value that is no Yes/No value raises wkeInvalidValue. The value is not stored with the form; a new switch starts off.
Read/write.
Code example
None
Inheritance
Declared by WklToggleSwitch.
OnText property
Signature
Property Get OnText() As String Property Let OnText(RHS As String)
Summary
Gets or sets the text after a switch that is on.
Parameters
None
Property value: String – Any text; “Ein” (default); empty for no text.
Remarks
Name the effect rather than the switch, for example “Aktiv” and “Gesperrt”; keep both texts about the same length, so the control does not need more room in one state.
Read/write.
Code example
None
Inheritance
Declared by WklToggleSwitch.
OffText property
Signature
Property Get OffText() As String Property Let OffText(RHS As String)
Summary
Gets or sets the text after a switch that is off.
Parameters
None
Property value: String – Any text; “Aus” (default); empty for no text.
Remarks
Read/write.
Code example
None
Inheritance
Declared by WklToggleSwitch.
Caption property
Signature
Property Get Caption() As String Property Let Caption(RHS As String)
Summary
Gets or sets the caption before the switch.
Parameters
None
Property value: String – Any text; empty (default) for none.
Remarks
The caption is also the name screen readers announce. Without a caption, give the switch a label of its own, as for a check box, or a meaningful control name.
Read/write.
Code example
None
Inheritance
Declared by WklToggleSwitch.
BackColor property
Signature
Property Get BackColor() As OLE_COLOR Property Let BackColor(RHS As OLE_COLOR)
Summary
Gets or sets the background color.
Parameters
None
Property value: OLE_COLOR – An OLE color; the window background of Windows (vbWindowBackground, default).
Remarks
Read/write.
Code example
None
Inheritance
Declared by WklToggleSwitch.
ForeColor property
Signature
Property Get ForeColor() As OLE_COLOR Property Let ForeColor(RHS As OLE_COLOR)
Summary
Gets or sets the color of the caption and the state text.
Parameters
None
Property value: OLE_COLOR – An OLE color; the window text of Windows (vbWindowText, default).
Remarks
The switch itself always uses the accent blue of Windows 11 for on and gray outlines for off, so its state reads the same in every form.
Read/write.
Code example
None
Inheritance
Declared by WklToggleSwitch.
FontName property
Signature
Property Get FontName() As String Property Let FontName(RHS As String)
Summary
Gets or sets the font of the caption and the state text.
Parameters
None
Property value: String – The name of an installed font; “Segoe UI” (default). An empty name raises wkeInvalidValue.
Remarks
Read/write.
Code example
None
Inheritance
Declared by WklToggleSwitch.
FontSize property
Signature
Property Get FontSize() As Long Property Let FontSize(RHS As Long)
Summary
Gets or sets the font size in points.
Parameters
None
Property value: Long – 6 to 72; 9 (default). Other values raise wkeInvalidValue.
Remarks
Read/write.
Code example
None
Inheritance
Declared by WklToggleSwitch.
ColorScheme property
Signature
Property Get ColorScheme() As WklColorScheme Property Let ColorScheme(RHS As WklColorScheme)
Summary
Gets or sets the color scheme the control draws in.
Parameters
None
Property value: `WklColorScheme` – wcoAutomatic (default), wcoLight, wcoDark or wcoHighContrast.
Remarks
wcoAutomatic follows, in this order, Windows high contrast mode, then the color set of the database design (Access: Design › Themes or Colors), and draws light when the database has no design or the control is on an Excel or Word UserForm. Office's own design setting and the Windows app mode are not followed: a database looks the same on every PC. In high contrast mode the control always uses the system colors. The scheme colors the background and caption while BackColor and ForeColor keep their system defaults, and the switch (light: unchanged; dark: lighter lines in the off state; a database color set: “on” in its accent 1); the toggle column of WklGrid and WklTreeList follows its own control's scheme; a BackColor you set wins over the scheme. Every text keeps a contrast of at least 4.5:1. The control reads the database design after it is first drawn, never while Access draws the form, and again when the form opens anew. The value is saved with the form.
Read/write.
Code example
None
Inheritance
Declared by WklToggleSwitch.
Change event
Signature
Event Change()
Summary
Occurs after the user has switched and the new value has been reported to Access.
Parameters
None
Remarks
The record is changed but not saved yet: read the new state from the control (Me!tglAktiv), not from the field, or save first (Me.Dirty = False; the field then holds the new value). It does not occur when Value is set by code or when Access shows another record. For a switch from UI Automation the event runs just after the screen reader's call has returned, so a message box in it does not block the screen reader. Like all events of an ActiveX control it needs a procedure in the form's module.
In a form module: Private Sub <ControlName>_Change(...). Parameters passed by reference can be changed by the procedure.
Code example
None
Inheritance
Declared by WklToggleSwitch.