J
Jon Lewis
Hi All
I've a tables of Accounts, Categories and a table (to manage the
many-to-many relationship) AccountsCategories.
If I need a query which returns all Accounts of Category i AND j AND k (as
opposed to OR) the following doesn't work (as it does with OR):
SELECT DISTINCT tblAccountsCategories.AccountID
FROM tblAccountsCategories
WHERE tblAccountsCategories.CategoryID=i AND
tblAccountsCategories.CategoryID=j AND AND
tblAccountsCategories.CategoryID=k
presumably because for each record the CategoryID can only have one value.
The only way I've got this sort of query to work is something like the
following which is looping through a multiselectable ListBox of Categories :
Dim lCount As Long
Dim rs As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rs = db.OpenRecordset("Select Distinct [AccountID] FROM
tblAccountsCategories")
With rs
If Not .EOF Then
For lCount = 0 To Me.lstSelectedCategory.ListCount - 1
If .EOF Then
Exit For
End If
.Filter = "[AccountID] IN (SELECT [AccountID] FROM
tblAccountsCategories WHERE [CategoryID]= '" &
Me.lstSelectedCategory.ItemData(lCount) & "')"
Set rs = .OpenRecordset
Next
End If
End With
Is there an easier/better way?
TIA
I've a tables of Accounts, Categories and a table (to manage the
many-to-many relationship) AccountsCategories.
If I need a query which returns all Accounts of Category i AND j AND k (as
opposed to OR) the following doesn't work (as it does with OR):
SELECT DISTINCT tblAccountsCategories.AccountID
FROM tblAccountsCategories
WHERE tblAccountsCategories.CategoryID=i AND
tblAccountsCategories.CategoryID=j AND AND
tblAccountsCategories.CategoryID=k
presumably because for each record the CategoryID can only have one value.
The only way I've got this sort of query to work is something like the
following which is looping through a multiselectable ListBox of Categories :
Dim lCount As Long
Dim rs As DAO.Recordset
Dim db As DAO.Database
Set db = CurrentDb
Set rs = db.OpenRecordset("Select Distinct [AccountID] FROM
tblAccountsCategories")
With rs
If Not .EOF Then
For lCount = 0 To Me.lstSelectedCategory.ListCount - 1
If .EOF Then
Exit For
End If
.Filter = "[AccountID] IN (SELECT [AccountID] FROM
tblAccountsCategories WHERE [CategoryID]= '" &
Me.lstSelectedCategory.ItemData(lCount) & "')"
Set rs = .OpenRecordset
Next
End If
End With
Is there an easier/better way?
TIA