Reputation: 1284
I am new to Access and queries, however trying to learn fast :)
The project has 2 tables: tblClientData
contains only data of clients, tblClientComments
contains all the commentaries made to all clients.
Database
tblClientData
tblClientComments
I wanted to make an option button - if ticked it would only show certain Clients, when unticked it would show all again. Problem raises here.
Criteria to this query comes from another table (tblClientComments
) within this database. For example: if Client was contacted by an employee - he is making commentary.
In order to finish this task I need to get all unique IDs from tblClientComments
and then add each of it as criteria to query tblClientData
. I tried to make an array with values from tblClientComments
and then create a query but so far still fighting with syntax.
Or maybe there is another way?
Upvotes: 0
Views: 80
Reputation: 977
I don't see any reason that you need VBA to build your query. A simple SQL IN()
statement should certainly suffice:
SELECT Client.ID, Client.OtherInfo
FROM tblClientsData As Client WHERE Client.ID IN(SELECT tblClientComments.CustID From tblClientComments)
Now, you have a few options. (I'm assuming you're using a checkbox for simplicity.)
After Update
event:
Now put in some code:
If(Forms!yourForm!yourCheckbox = True) Then
CurrentDB.QueryDefs("yourQuery").SQL = "SELECT Client.ID, Client.OtherInfo FROM tblClientsData As Client WHERE Client.ID IN(SELECT Comments.CustID From Comments)"
Else
CurrentDB.QueryDefs("yourQuery").SQL = "SELECT Client.ID, Client.OtherInfo FROM tblClientsData As Client"
End If
You can then just use Forms!yourForm.Refresh
to reflect your changes with the checkbox.
DoCmd.ApplyFilter
should be a very straightforward way to change the form's contents. (Note that the example at the last hyperlink is very similar to your situation.)Upvotes: 2