Code Samples for Businesses, Schools & Developers

Click any image to view a larger version

Version 1.1           Last Updated 23 May 2022                 Approx 0.37 MB


The form/report controls available in Access have been around for over 20 years with only minimal change.
There is a real need for new controls to be added that don’t depend on the use of ActiveX

This is the first in an occasional series of additional controls that can be added to Access forms without requiring ActiveX functionality

It is based on a thread I posted at Access World Forums in Dec 2017, Toggle On/Off Button where I asked:
Does anyone know of a form control which can act in a similar way to those below?
ToggleOnOff This type of control is common in Windows settings and on smartphones but is not available in Access.

Fellow forum member, Chris Arnold, suggested a simple solution:
You can use a button - use format to select a quick style but you would need code to set border, back and fore colors - plus code to change the alignment.
Use Wingdings font (lowercase L) or Webdings (lowercase N)

The prompt acted as a reminder about using Quick Styles and other items on the Control Formatting section of the Format ribbon:


ControlFormat

Quick Styles:

QuickStyles

Change Shape:

ChangeShape

Shape Effects:

ShapeEffects

It took no more than 2 minutes to achieve exactly what I wanted!

In this case I just needed to use Change Shape to create the rounded corners effect and code to toggle the appearance of each button

image1 image2


First create the rounded button(s), add the button caption as a lower case 'l' in Wingdings then add code similar to this to your form module.



CODE:

Option Compare Database
Option Explicit

Dim IsOn As Boolean

‘===============================

Private Sub cmd1_Click()       'first button

      If IsOn = False Then
            FormatButton True, cmd1
            FormatButton False, cmd2
            IsOn = True
            Me.lbl1.Caption = "ON"
            Me.lbl2.Caption = "OFF"
      Else
            FormatButton False, cmd1
            FormatButton True, cmd2
            IsOn = False
            Me.lbl2.Caption = "ON"
            Me.lbl1.Caption = "OFF"
      End If
End Sub

‘===============================

Private Sub cmd2_Click()       'second button

      If IsOn = False Then
            FormatButton True, cmd2
            FormatButton False, cmd1
            IsOn = True
            Me.lbl2.Caption = "ON"
            Me.lbl1.Caption = "OFF"
      Else
            FormatButton False, cmd2
            FormatButton True, cmd1
            IsOn = False
            Me.lbl1.Caption = "ON"
            Me.lbl2.Caption = "OFF"
      End If
End Sub

‘===============================

Function FormatButton(IsOn As Boolean, ctrl As control)

'sets the toggle button appearance on/off

      If IsOn Then
            ctrl.BorderColor = vbBlue
            ctrl.BackColor = vbBlue
            ctrl.ForeColor = vbWhite
            ctrl.Alignment = 3 'right
      Else
            ctrl.BorderColor = vbBlack
            ctrl.BackColor = vbWhite
            ctrl.ForeColor = vbBlack
            ctrl.Alignment = 0
      End If

End Function

‘===============================

Private Sub Form_Load()
      IsOn = False
End Sub





Click to download:
      OnOffToggle.zip              Approx 0.37 MB (zipped)



Colin Riddington           Mendip Data Systems                 Last Updated 23 May 2022



Return to Code Samples Page




Return to Top