Access VBA Macro Programming

(Joao Candeias) #1
The following returns False because there is no True value:

MsgBox False Or False


It works using binary arithmetic, on the basis that 1 or 1 make 1, 1 or 0 make 1, 0 or 1 make 1,
and 0 or 0 make 0.
The top row of the following table represents the value of each binary bit going from bit 7
to bit 0. The two rows below it represent the binary equivalents of the numeric numbers on
the right of the table (columnn). The final row shows both the binary and numeric equivalents
when the two numbers areOred together. Each bit pair uses anOroperator to achieve the
final result on the bottom line.


128 64 32 16 8 4 2 1 n
0101010084
1 0 0 1 0 0 0 1 145
1 1 0 1 0 1 0 1 213

Each column of the preceding table shows a binary bit based on an eight-bit number. The
bit values are shown in bold across the top. The right-hand column contains the actual decimal
numbers.
Bits are counted from right to left, starting at bit 0 and ending at bit 7 for a single-byte
number. Notice that the values of each bit increase in powers of 2. The first number is 84, so
bit 6, bit 4, and bit 2 are all set. If you add 64 + 16 + 4, this comes to 84. The second number
is 145, so bit 7, bit 4, and bit 0 are set. If you add 128 + 16 + 1, this comes to 145.
When a logicalOris done on the two numbers, the result is 213 (128 + 64 + 16 + 4 +1).
This can be shown using the following VBA example:


MsgBox 8 4 Or 145


This will give the result of 213.
TheOroperator is often used for masking purposes in graphics and also for combining
two parameters. In Chapter 5, I discussed the message box you had to combine with
vbExclamationandvbYesNoin order to get the correct icon and the correct buttons on the
message box. Using a simple+operator to addvbExclamationandvbYesNotogether will
result in the wrong value being set for the required flag. It is only by usingOrthat the correct
result is achieved.
You also see use ofOrin If statements, as in Chapter 4:


If x = 1 Or y = 1 Then


Chapter 6: Operators 77

Free download pdf