I have a form which displays companies and a subform which displays contacts. There are multiple contacts per company.
I have a filter which works off of a combo box, which selects companies only where they have contacts with the selected responsibility;
Sub SetFilter()
Dim ASQL As String
If IsNull(Me.cboshowcat) Then
' If the combo and all check boxes are Null, use the whole table as the
' RecordSource.
Me.RecordSource = "SELECT company.* FROM company"
Else
ASQL = "SELECT DISTINCTROW company.* " & _
"FROM company INNER JOIN Contacts " & _
"ON company.company_id = Contacts.company_id " & _
"WHERE Contacts.responsibility= '" & cboshowcat & "' " & _
"ORDER BY Company.company_id"
Me.RecordSource = ASQL
End If
End Sub
I also have 3 checkboxes which further refine the records which can only be used once a job responsibility has been selected. The filter and checkboxes are ran after clicking a button;
Private Sub Command201_Click()
If Nz(Me.cboshowcat) = "" _
And Me.Check194 = True _
Or Nz(Me.cboshowcat) = "" _
And Me.Check199 = True _
Or Nz(Me.cboshowcat) = "" _
And Me.Check205 = True _
Then
MsgBox "Please Select a Job Responsibility"
Cancel = True
Else
SetFilter
If Me.Check194 = True _
And Me.Check199 = True _
And Me.Check205 = True _
Then
Me.Filter = "[contacts].[edit] <=Date()-90 " & _
"and [contact].[opt out]='No' " & _
"and [company].[exclude site] is null"
Me.FilterOn = True
Else
If Me.Check194 = True _
And Me.Check199 = True _
And Me.Check205 = False _
Then
Me.Filter = "[contacts].[edit] <=Date()-90 " & _
"and [contact].[opt out]='No'"
Me.FilterOn = True
Else
'................(repeated for each combination)
Me.Filter = ""
Me.FilterOn = False
End If
End If
End If
End If
End If
End If
End If
End If
Me.Repaint
End Sub
The above query does not work as it does not find the field name that I am referencing in the contact table. If I include the field in the filter query select statement it does work, however it shows me multiple instances of each company depending on how many contacts are returned for each company.
I need to filter the companies based on information in contact table without duplicating the company information.
If someone knows how to get around this problem I would be very grateful.