This month, Doug Steele looks at how to have larger checkboxes and how to deal with code that gets unlinked from the control to which it’s supposed to be linked.
How can I change the size of the checkbox on my form?
Unfortunately, you can’t. What you can do, though, is simulate a larger checkbox. The first step is to put a checkbox on the form, as you would normally, and set its Visible property to False/No (for the discussion that follows, I’m going to assume that this hidden checkbox is named chkHidden). The second step is to put an unbounded label on the form where you want the larger checkbox. Set the properties of the label to the entries in Table 1 (I’m going to assume that the label you just added is named lblUnboundCheckbox).
dTable 1. Label property settings to simulate oversized checkboxes.
|” ” (a space)
|Whatever you want (try 14)
I’ve set the Font property to WingDings font because it actually contains a checkmark. I’m going to set the caption of the lblUnboundCheckbox to that character when I want a checkmark and to a zero-length string (“”) when I don’t. The final step is to adjust the height and width of the label so that the checkmark is fully displayed.
Now, checkboxes toggle from checked to unchecked when you click on them. Fortunately, labels have a Click event, so I use code to control the appearance of my simulated checkbox:
Private Sub lblUnboundCheckbox_Click() If IsNull(Me.chkHidden) Then Me.chkHidden = True Else Me.chkHidden = Not Me.chkHidden End If If Me.chkHidden = True Then Me.lblUnboundCheckbox.Caption = Chr(252) Else Me.lblUnboundCheckbox.Caption = "" End If End Sub
When the large checkbox is clicked on, I toggle the hidden checkbox (if it was checked, it becomes unchecked, and vice versa). I then base what I display in my simulated checkbox on the value of that checkbox.
If the hidden checkbox is bound to a field in the form’s recordset, I can simulate binding the large checkbox to the data field by using code in the form’s Current event:
Private Sub Form_Current() If Me.chkHidden = True Then Me.lblUnboundCheckbox.Caption = Chr(252) Else Me.lblUnboundCheckbox.Caption = "" End If End Sub
If you want a CheckBox field label, just add another unbound label and set its caption to the CheckBox’s field name (and leave it Visible).
Unfortunately, this approach doesn’t work for continuous forms. This is because, in a continuous form, Access has just one set of control properties for each control for all the records displayed. So, when you change the caption on a label, you change the caption for that label on every record. To get around this, you can use a text box instead and use the text box’s ControlSource property to manage whether the check appears. The ControlSource for a text box is the one property that won’t be replicated across all the records in the continuous form.
Begin by adding a text box to your continuous form and set the properties as shown in Table 2.
Table 2. Text box property settings to simulate oversized checkboxes.
|Whatever you want (try 14)
I bind my hidden checkbox to a field in the form’s recordset. For the text box, I can set its ControlSource property to a formula that uses the IIF function. If, for example, the field in the recordset that I want to bind to is named Discontinued, I set the text box’s control source to:
=IIf([Discontinued], Chr$(252), Chr$(32))
You still need code to ensure that the underlying recordset field is updated to reflect changes in the checkbox’s status. Again, I use the text box’s Click event for this:
Private Sub txtBoundCheckBox_Click() Me.Discontinued = Not Me.Discontinued End Sub
Since the control is bound to the data, there’s no need for code in the form’s Current event to update the field in the record.