DBA > Articles

Multiple Selection Through Bitmasks

By: Danny Lesandrini
To read more DBA articles, visit http://dba.fyicenter.com/article/

Let me begin by revealing that I'm not an expert with bitmasks. However, this fact should bring a little comfort to the uninitiated because it means that anyone can implement multiple selections using a bitmask field and operators. Odds are, you've been using them for years, but simply didn't notice it. If you've ever set the properties of a message box using Intellisense, then you've used bitmasks.

Notice the screen shot of the Microsoft VBA MsgBox function in action, adorned with all its Intellisense regalia. When you type the first comma, a drop-down list of options appears. If you check the help file, you'll find the table of options shown below, along with their constant values. If we select vbCritical, the value used is 16, and the Critical Stop icon is added to the message box.

As you may be aware, there are more options than the ones I show below. In addition the help file for the MsgBox() function provides these helpful instructions:

The first group of values (05) describes the number and type of buttons displayed in the dialog box; the second group (16, 32, 48, 64) describes the icon style; the third group (0, 256, 512) determines which button is the default; and the fourth group (0, 4096) determines the modality of the message box. When adding numbers to create a final value for the buttons argument, use only one number from each group.

That's basically how a bitmask works ... it's singular value that is made up of the addition of other distinct values. This example, however familiar, is not what I consider to be a pure example because it comes with the instruction disclaimer above. In a perfect world, the sum of two bitmask values can NEVER equal another value in the list. Thus, pure binary values are usually employed. See the example in the next section.






 Display OK button only.



 Display OK and Cancel buttons.



 Display Abort, Retry, and Ignore buttons.



 Display Yes, No, and Cancel buttons.



 Display Yes and No buttons.



 Display Retry and Cancel buttons.



 Display Critical Message icon.



 Display Warning Query icon.



 Display Warning Message icon.



 Display Information Message icon.

Bitmask Demo Example

While the idea behind bitmasks can seem a little convoluted, the code is very simple. Download the sample application and you'll see what I mean. It solves the following problem:
How do I save a number of Boolean attributes for an employee?
More specifically, as shown in the form below, how do I save user selections for the FIVE attributes shown here: Full Time, Hourly, Union, Management, Amiable? These options are not mutually exclusive. An employee could possess one or many, all or none of these attributes. An employee could be Hourly and be in Management. They could belong to the Union and they might even be Amiable. Or they could possess none of those attributes.

Traditionally, we are inclined to create a field of the table for each checkbox. But what happens when you come up with another attribute, like [Terminated]? Back to the table, right? Not necessarily. If you're using a bitmask, you simply add another attribute and code for it.

Full article...

Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/