UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Closed TopicStart new topic
> Checking/Setting the Attributes of an Object    
 
   
datAdrenaline
post May 1 2007, 03:08 AM
Post#1


UtterAccess Editor
Posts: 17,938
Joined: 4-December 03
From: Northern Virginia, USA


Testing for Attributes can be a confusing matter. Attribute values, like properties, represent characterists of an object. However, attributes, represent those characteristics as a number, or more specifically a Long Integer, and even more specifically, a series of "bit fields" in which each bit of the long integer can represent a different characteristic being set or not set. A long integer is 4 bytes, which is the same as 32 bits, so an Attribute value may have up to 32 different characteristics that need to be represented.
With all that, one might ask "How do I check to see if a bit field {aka: characteristic} is set or not set in the total attribute value".....
A result of TRUE for the expressions would indicate the characteristic {bit} is set:
<value of the characteristic> = (<attribute value> And <value of the characteristic>)
... Alternatively ...
((<attribute value> (2 ^ <bit position you are testing for>)) Mod 2) = 1
CAn example usage ... Check to see if the table has an AutoIncrement field:
CODE
Public Function AutoField(strTableName As String) As String
    Dim fld As DAO.Field
    Dim db As DAO.Database
    Set db = CurrentDb()
    For Each fld In db.TableDefs(strTableName).Fields
        If dbAutoIncrField = (fld.Attributes And dbAutoIncrField) Then
            AutoField = fld.Name
            Exit For
        End If
    Next
End Function

The explanation behind the usage of: dbAutoIncrField = (fld.Attributes And dbAutoIncrField)
The value dbAutoIncrField = 16, which is 00000000 00000000 00000000 00010000 in a 4 byte (long) binary. The value of fld.Attributes is a LONG data type, but we only care about the 5th bit from the right (actual its bit 4, bits are zero based), so we must first "MASK" fld.Attributes with that bit pattern that has the bit in question set to 1
For example ... a MASKing of bits is sorta like a huge series of And's where each respective bit from the value is compared the mask. BOTH the bit from the Value and the Mask must equal 1 to set that bit in the Result ...
CODE
Value : 00010001
And
Mask  : 00010000
--------------------
Result: 00010000

The above is the bit pattern for 17 masked with 16, to return a result of 16. So when testing to see if the fld.Attributes (17) has the dbAutoIncrField (16) bit set, you must mask the fld.Attributes with a mask of 16, then compare the result of the masking to the value you wish to test for being set ...
Heres a progression ...
If dbAutoIncrField = (fld.Attributes And dbAutoIncrField) Then ...
..
If 16 = (17 And 16) Then ...
...
If 00010000 = (00010001 And 00010000) Then ...
...
If 00010000 = (00010000) Then ...
...
If 16 = 16 Then ...
Now that you can TEST for a particular bit being set, how do you SET the bit, if the characteristic is able to be written to.
And expression which sets a bit uses the VBA's Or operator. The Or, like the And, is a 'double-duty' operator, you can use it to perform value comparisons or BitWise operations.
The core expression used to set a Bit is ...
<value> = <value> Or <characteristic value>
In practical application you may see something like this:
fld.Attributes = fld.Attributes Or dbAutoIncrField
The way this works is like this: When the Or is used as a bitwise operator, each respective bit between the value (Attribute) and the mask (dbAutoIncrField) is compare to each other, if EITHER one of them is a 1, a 1 is set in the repsective bit of the result.
CODE
Value : 00000001
Or
Mask  : 00010000
--------------------
Result: 00010001

The above is the bit pattern for 1 is Or'd with the mask of 16, to return a result of 17. So when setting a particular bit, you simply Or the current value of the Attribute with the value of the characteristic you with to set.
Heres a progression ...
fld.Attributes = fld.Attributes Or dbAutoIncrField
...
1 = 1 Or 16
...
00000001 = 00000001 Or 00010000
...
Attributes = 00010001 = 17
Other bitwise operators in VBA that are worth looking at are:
CODE
XOr: Exclusive Or, which means either value MUST have a 1 in the bit position, BUT, both can not be 1.
     0001 XOr 0001 = 0000
     ...
     1100 XOr 0011 = 1111
Not: Reverses the Bit pattern.  Not 1100 => 0011

There are others which can be researched in the VBA help.
...
Now that you can Set and Check bits, you can successfully understand the Attribute value and where it comes from.
...
Click Here for an some bitwise manipulation code
I hope this helps! ...If you have any questions, please PM me and reference this FAQ.
5/3/2007 Edit: Added link to Bitwise Manipulation and Binary/Hex Conversions FAQA -- Doug
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    14th December 2017 - 09:34 PM