What you have here is a set of flags stored as a 7-bit integer variable.
Each bit represents one day of the week. In binary notation 127 is 1111111,
so this represents all days of the week as every bit is turned on. 62 in
binary notation is 0111110, i.e. only the bits for Monday to Friday are
turned on.
To determine whether a bit is turned on a Boolean AND operation is used.
This will return the value where both bits are turned on so:
0111110
AND
0000100
will return 4 (in decimal notation), because only the bit representing 4 is
turned on in both.
So you can wrap the logic up in a function by inserting the following module
into your database. Just paste the code into a new standard module and save
it as something like mdlDayStuff:
''''module begins''''
Option Compare Database
Option Explicit
Public Const conSAT As Integer = &H1
Public Const conFRI As Integer = &H2
Public Const conTHU As Integer = &H4
Public Const conWED As Integer = &H8
Public Const ConTUE As Integer = &H10
Public Const ConMON As Integer = &H20
Public Const ConSUN As Integer = &H40
Public Function GetDays(intEmpDayFlags As Integer) As String
Dim i As Integer
Dim intDay As Integer
Dim strDays As String
For i = 7 To 1 Step -1
intDay = (intEmpDayFlags And (2 ^ i) / 2)
Select Case intDay
Case ConSUN
strDays = strDays & "S"
Case ConMON
strDays = strDays & "M"
Case ConTUE
strDays = strDays & "T"
Case conWED
strDays = strDays & "W"
Case conTHU
strDays = strDays & "T"
Case conFRI
strDays = strDays & "F"
Case conSAT
strDays = strDays & "S"
Case Else
strDays = strDays & "-"
End Select
Next i
GetDays = strDays
End Function
Public Function HasAccess(intEmpDayFlags As Integer, _
intAccessDayFlags As Integer)
As Boolean
HasAccess = ((intEmpDayFlags And intAccessDayFlags) = intAccessDayFlags)
End Function
'''module ends''''
Note how its common to use hexadecimal notation to declare the constants in
cases like this.
To return a string showing which days the value stored in the column in the
table represents call the GetDays function, passing the value into it as its
argument, so in your report add an unbound text box with a ControlSource
property of:
=GetDays([YourFieldNameGoesHere])
If the value is 62 this will return -MTWTF-, if its 64 it will return
S-----S for instance.
You'll see that I've also included a HasAccess function in the module, to
demonstrate how you are not restricted to determining whether one particular
bit is turned on in both values; you can also determine if a set of bits are
turned on in both. So to see if an employee has access on Saturdays and
Sundays for example you can compare their value with 65 (64+1). If an
employee has 7-day access for instance:
=HasAccess([YourFieldNameGoesHere], 65) returns True because (127 AND 65) =
65, the first and seventh bits being turned on in both cases, but if they
have access from Sunday to Friday only for instance then it returns False
because (126 AND 65) returns 64, not 65, the Saturday bit (1) being turned on
only in 65 (64+1), not in 64.
Finally you'll notice that the constants are declared as Public, making then
available throughout the database (but not in queries) so you can use these
rather than their numeric values, e.g. to see if an employee has weekend
access:
=HasAccess([YourFieldNameGoesHere], conSAT + conSUN)
Ken Sheridan
Stafford, England