| The Best Way to Handle Populating a Checkbox from a Boolean Variable |
Applies To |
|
| OS: VB: |
NT, 9x, 2000 5, 6 |
|
If bActive = True then
chkActive.Value = 1
else
chkActive.Value = 0
End If
Some people prefer to make a single line If..Then..Else statement. Still others do a conditional if (IIF):
chkActive.Value = IIF(bActive,1,0)
None of the above approaches are actually bad, and they do all work, but they are somewhat cumbersome - not to mention slow. Keep in mind
that hardly ever will you have to just set a control's value. You almost always have to read from the control too
which means that you will have to write similar code to get the value out of the checkbox. The above methods
are therefore even more cumbersome.
chkActive = Abs(bActive) 'sets the check box (Abs(True) = 1, and Abs(False) = 0)
bActive = CBool(chkActive) 'reads boolean value out of check box (CBool(1) = True, CBool(0) = False)
Not only is the above approach quicker and easier to code - but it will execute faster as well.