=?Utf-8?B?U2VjcmV0IFNxdWlycmVs?=
I should also note the results I'm trying to achieve. I'm looking
to list all employees who have the selected CapacityID. I only
want to return the results if an employee has all of the selected
from the listbox. I don't want to list employees who have just
some of them, they have to have all to show in my results.
Secret Squirrel said:
I'm trying to write a string query to pull records but I'm not
having much luck using the "AND" statement. I'm using a listbox
to select multiple records but why doesn't the AND statement pull
them? I keep getting a blank result. Here is the code I'm using:
Set ctl = Me.SkillList
strSQL = "Select EmpName from qryEmployeeID"
intI = 0
For Each varItem In ctl.ItemsSelected
intI = intI + 1
If intI = 1 Then
strSQL = strSQL & " WHERE [CapacityID] = " &
ctl.ItemData(varItem)
Else
strSQL = strSQL & " AND [CapacityID]= " &
ctl.ItemData(varItem)
End If
Next varItem
The trick to get this to work is to use an Integer or Long for
CapacityId, but limited to powers of 2. calculate 2 to the power of
CapacityID and SUM the value.
You then need to AND this value with the desired quantity to return a
true or false false.
so given 5 capacityID possibilities, 1,2,4,8,16 you want to find
anyone who has 1 and 4, you sum the EmployeeID_CapacityID table.
John 1
john 2
John 4
Mark 1
Mark 4
Ann 1
Ann 8
Pete 2
Pete 4
which returns
Ann 9
John 7
Mark 5
Pete 6
Ann (9 and 5) = 5 is false
John (7 AND 5) = 5 is true
Mark (5 and 5) = 5 is true
Pete (6 and 5) = 5 is false
for some reason I could not get the AND to work in the query, so I
created a little function:
Public Function blAnd(param1, param2) As Boolean
If IsNull(param1) Or IsNull(param2) Then
blAnd = False
Else
blAnd = ((param1 And param2) = param2)
End If
End Function
This SQL demonstrates
PARAMETERS capacities Long;
SELECT EmployeeCapacity.EmployeeID, Sum(EmployeeCapacity.CapacityID)
AS SumOfCapacityID, blAnd(Sum([capacityID]),[capacities]) AS Expr1
FROM EmployeeCapacity
GROUP BY EmployeeCapacity.EmployeeID;
and can be reduced to
PARAMETERS capacities Long;
SELECT EmployeeCapacity.EmployeeID
FROM EmployeeCapacity
GROUP BY EmployeeCapacity.EmployeeID
HAVING (((bland(Sum([capacityID]),[capacities]))=True));