ibgreat
09-09-2008, 11:34 AM
I have text search field on a form that is set up to provide the results from two related tables. The search looks for values in the tblAgency.AgencyName and tsubAgencySubName for matches.
tblAgency
AgencyID
AgencyName
AgencyActive
tsubAgency
AgencySubID
AgencyID
AgencySubName
....
AgencySubActive
The search is no problem. But the results are. I want to only list unique values of tblAgency.AgencyName in the resulting listbox. Using DISTINCT returns unique values across all the columns. Unfortunately, AgencySubID and AgencySubName have to be included in the query to get the results. Do I need to apply a filter or query the query after the initial results are returned? Not sure how to go about this within the VBA. This is how the SQL statement is currently being built.
Dim SQL As String
SQL = "SELECT DISTINCT tblAgency_S.AgencyID, tblAgency_S.AgencyName, tsubAgency_S.AgencySubID, "
SQL = SQL & "tsubAgency_S.AgencySubName, tsubAgency_S.CityAgency, tsubAgency_S.StateAgency "
SQL = SQL & "FROM tblAgency_S LEFT JOIN tsubAgency_S ON tblAgency_S.AgencyID = tsubAgency_S.AgencyID "
'add the txtSearchFor criteria if a value is entered
If Not IsNull(Me.txtSearchFor) Then
SQL = SQL & "WHERE (AgencySubName like '*" & Me.txtSearchFor & "*' OR AgencyName like '*" & Me.txtSearchFor & "*') "
End If
SQL = SQL & "ORDER BY AgencyName"
Me.lstAgencyResultsSimple.RowSource = SQL
tblAgency
AgencyID
AgencyName
AgencyActive
tsubAgency
AgencySubID
AgencyID
AgencySubName
....
AgencySubActive
The search is no problem. But the results are. I want to only list unique values of tblAgency.AgencyName in the resulting listbox. Using DISTINCT returns unique values across all the columns. Unfortunately, AgencySubID and AgencySubName have to be included in the query to get the results. Do I need to apply a filter or query the query after the initial results are returned? Not sure how to go about this within the VBA. This is how the SQL statement is currently being built.
Dim SQL As String
SQL = "SELECT DISTINCT tblAgency_S.AgencyID, tblAgency_S.AgencyName, tsubAgency_S.AgencySubID, "
SQL = SQL & "tsubAgency_S.AgencySubName, tsubAgency_S.CityAgency, tsubAgency_S.StateAgency "
SQL = SQL & "FROM tblAgency_S LEFT JOIN tsubAgency_S ON tblAgency_S.AgencyID = tsubAgency_S.AgencyID "
'add the txtSearchFor criteria if a value is entered
If Not IsNull(Me.txtSearchFor) Then
SQL = SQL & "WHERE (AgencySubName like '*" & Me.txtSearchFor & "*' OR AgencyName like '*" & Me.txtSearchFor & "*') "
End If
SQL = SQL & "ORDER BY AgencyName"
Me.lstAgencyResultsSimple.RowSource = SQL