logo
vbRad Home
Source Code
Book Reviews
Forum
Links
About Us
Contribute

Compare Databases with SQL Effects Clarity
 
 The Best Way to Handle Populating a Checkbox from a Boolean Variable

Posted on
2/18/2001
Author:
Matthew Ferry
Email:
Not Shown
Applies To OS:
All
Product:
5, 6



There are a number of ways to set a check box value from a boolean variable. Some people just put their head down and plow through it like a bull in a china shop and write code like the following:

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.

I have found a different method that I much prefer. You can set or retrieve a boolean value into or out of a check box control with a single line of code that calls only a simple function. Use the Absolute Value function to set the combo box and the CBool function to read from it:

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.



Add Your Comment  

Name: Email Address: all fields optional
Notify me via email when someone responds to this message (valid email required).

Enter the word: