Check Boxes

When used in an option group, check boxes behave much like option buttons. If you have experience in other graphical languages, you might be surprised to learn that a user can select only one check box at a time when it is located in an option group. This is different behavior from VBA's parent language Visual Basic! Remember that an option group provides a single selection for any option-based control such as check boxes, option buttons, and toggle buttons.

To use check boxes in a multiselection facility, you need to add them manually, outside of an option group. In addition, you need to set each check box's DefaultValue property to a unique number during design time.

Implemented in Figure 3.10, the following code demonstrates how one might use check boxes in a multiselection capacity.

Option Compare Database Option Explicit

Dim dRunningTotal As Double

Private Sub cmdTotal_Click()

dRunningTotal = 0

If Me.chkTShirt.Value = True Then dRunningTotal = dRunningTotal + 9.99 End If

If Me.chkBaseballCap.Value = True Then dRunningTotal = dRunningTotal + 12# End If

If Me.chkSwimmingTrunks.Value = True Then dRunningTotal = dRunningTotal + 24.19 End If

If Me.chkSunBlock.Value = True Then dRunningTotal = dRunningTotal + 3# End If

If Me.chkSunGlasses.Value = True Then dRunningTotal = dRunningTotal + 6.99 End If

Me.lblTotal.Caption = "Your total is $" & dRunningTotal

End Sub

Selecting more than one check box at a time.

Selecting more than one check box at a time.

I can use the Value property of each check box to determine whether the user has selected it. If the check box has been checked, the Value property is set to True; if not, it is set to False.

0 0

Post a comment