You are on page 1of 5

Performing Logic Operations with Boolean Values

 
With a background as an electronics technician, I found myself needing to perform fairly
complex logical operations in my VB applications. I started out building long If…Then
statements and then worked my way up to Select…Case statement. Even by combining
the two, I often had difficulty performing the logic I needed to.
 
Take this example:
 
I had eight check boxes on a form. I needed to know if any of them had been checked.
This was my first attempt:
If chkOption1 = True then

BlnItemChecked = True

End If

If chkOption2 = True then

BlnItemChecked = True

If chkOption3 = True then

BlnItemChecked = True

End If

If chkOption4 = True then

BlnItemChecked = True

If chkOption5 = True then

BlnItemChecked = True

End If

If chkOption6 = True then

BlnItemChecked = True

If chkOption7 = True then

BlnItemChecked = True

End If

If chkOption8 = True then


BlnItemChecked = True

End If

If blnItemChecked = False Then

MsgBox "You must select an option ."

End If

 
 
You can see that this got really big really fast. I worked it over and came up with this:
If chkOption1 = True then

BlnItemChecked = True

ElseIf chkOption2 = True then

BlnItemChecked = True

ElseIf chkOption3 = True then

BlnItemChecked = True

ElseIf chkOption4 = True then

BlnItemChecked = True

ElseIf chkOption5 = True then

BlnItemChecked = True

ElseIf chkOption6 = True then

BlnItemChecked = True

ElseIf chkOption7 = True then

BlnItemChecked = True

ElseIf chkOption8 = True then

BlnItemChecked = True

End if

If blnItemChecked = False Then

MsgBox "You must select an option ."

End If

 
I was quite pleased with myself for eliminating seven "End If" statements without
changing the logic. Since all I needed to know was if any one of them was checked, it
worked perfectly…but somehow I was still not happy with it. Something was bugging
me, but I couldn't put my finger on it. Then, some three days later, I was explaining how
a logic circuit works to a fellow programmer, and it suddenly hit me. Boolean values
were JUST LIKE electronic gate circuits.
 
For those of you who know what Gates are, you will catch on to my thinking pretty
quick. The rest of you hang in there, it is worth it.
 
A Gate is an electronic device that normally has multiple inputs and a single output. The
output is either a logical High (1) or Low(0). The logic levels of the inputs determine
what the output will be. The two main types of gates are AND gates and OR gates.
These are the microscopic parts that make it possible for you to read this on your
computer.
AND Gates have the following rules:
If One input is High (True) then the output is Low (False).
If ALL inputs are High, then the output is High
A practical example of this is the alarm panel on a security guard's desk. Each sensor in
the building has an output. If all of these sensors or outputting a High level (Secured),
the security guard's big green SECURED light becomes lit. But if one of them stops
outputting a High (because someone opened a door and broke the contact), is
SECURED light turns off.
This is done with an AND gate.
 
An OR gate works just the opposite. Any High will cause the output to go High.
 
This logical High or Low…On or Off…is what brought this concept to my attention.
Suddenly I realized that I could design logic circuits the and then substitute boolean
values for inputs. The output is also a boolean value.
 
The key to this concept is the Visual Basic Boolean data type. To Visual basic,
something is either False (0) or True (<>0). For the longest time I thought that True was
always equal to one because if you check the value of a variable that holds a boolean
true value, it will be "1". But what I failed to realize is that ANY NON-ZERO value
evaluates as True in VB. This is very useful. Each of these values evaluates as True in
a boolean variable: 1,10,1000, -1, -200, … The only thing that evaluates as False is the
number zero.
 
In short, if it is zero, it is false. If it is not zero, it is not false.
 
Now I routinely use statements such as:
If myRecordset.RecordCount = True Then

…Do some record stuff…

Else

MsgBox "No Records Retrieved"

End if

Since the RecordCount property will always be either 0 or a non-zero value, this will
always evaluate as true or false. I can take advantage of this concept in code and act
on the recordset only if the RecordCount is greater than 0.
 
This logic is not limited to recordsets. It can be used on any evaluation. One site note…
in VB, you can do another neat shorthand trick to make your code easier to read and do
less typing. Since boolean values always evaluate to true or false, you can omit the "=
True" from your If statement:
If myRecordset.RecordCount Then

…Do some record stuff…

Else

MsgBox "No Records Retrieved"

End if

Is logically identical to the prior example. It executes the exact same way.
Another useful boolean operator is "Not". The Not keyword "swaps" the value of a
boolean variable. For example:
MsgBox Not True
Shows a message box that reads "False" . In the preceding example, we could have
said:
If Not myRecordset.RecordCount Then

MsgBox "No Records Retrieved"


Else

…Do some record stuff…

End if

And gotten the same results.


 
Once I discovered this concept, I found uses for it at every turn. In my first example, I
applied my "boolean logic gates" to the checkbox problem and came up with this
compact piece of code:
If Not (chkOption1 + chkOption2 + chkOption3 + chkOption4 + chkOption5 + chkOption6 + chkOption7 +
chkOption8) Then

MsgBox "You must select an option."

End if

 
This is logically the same as the first example, but look at how much less real estate it
takes up!
 
This article is a basic introduction to the concept of boolean logic gates. As far as I
know, this is something that I conceived. If anyone is interested in some more advanced
uses for them, please email me.
If I get enough responses, I will post additional, more advanced articles with some
illustrations.
 
 
 
 
 

You might also like