Hi all,
I have two tables - Quote and Job (each Job has an associated Quote, but not all Quotes have a Job).
I also have a search form which enables the user to search the quotes/jobs in the system. The search fields include QuoteNumber, JobNumber, Date, Client, etc.
I am having trouble with the query used to produce the search results. The trouble comes in with those quotes that have jobs. This is the code I have in place to query the database...
The problem is for each quote that has an associated job I get two entries in the results....for example
Job: 4 --- Quote: 2 --- Date: 12/12/07 --- Status: 'NEW'
Job: - --- Quote: 2 --- Date: 12/12/07 --- Status: 'ACCEPTED'
I only want the first row to show; that is - if a quote has a job then both the job and quote number should be shown. If a quote doesn't have a job then just show the quote number.
Any idea how I can achieve this? Sorry if this is hard to understand.
Thanks in advance
I have two tables - Quote and Job (each Job has an associated Quote, but not all Quotes have a Job).
I also have a search form which enables the user to search the quotes/jobs in the system. The search fields include QuoteNumber, JobNumber, Date, Client, etc.
I am having trouble with the query used to produce the search results. The trouble comes in with those quotes that have jobs. This is the code I have in place to query the database...
Code:
Dim strSelect1 As String
Dim strSelect2 As String
strSelect1 = "SELECT * FROM (SELECT J.ID as JobID, Q.ID as QuoteID, "
strSelect1 = strSelect1 & "Q.Date, Q.Client, J.Status as Status "
strSelect1 = strSelect1 & "FROM Job AS J, Quote AS Q WHERE "
strSelect2 = "SELECT '-' as JobId, Q.ID as QuoteID, Q.Date, "
strSelect2 = strSelect2 & "Q.Client, Q.chrState as Status "
strSelect2 = strSelect2 & "FROM Quote AS Q, Job AS J WHERE "
'Make up the filter string
strFilter = ""
If Not IsNull(Me!QuoteNumber) Then strFilter = strFilter & "Q.ID=" & Me!QuoteNumber & " AND "
If Not IsNull(Me!JobNumber) Then strFilter = strFilter & "J.ID=" & Me!JobNumber & " AND "
If Not IsNull(Me!Date) Then strFilter = strFilter & "Q.Date=" & Chr(34) & Me!Date & Chr(34) & " AND "
If Not IsNull(Me!Client) Then strFilter = strFilter & "Q.Client=" & Me!Client & " AND "
If strFilter <> "" Then
Dim queryString As String
If IsNull(Me!JobNumber) Then
queryString = strSelect1 & strFilter & " J.Quote=Q.ID UNION " & strSelect2 & strFilter
queryString = queryString & " NOT J.Quote=Q.ID"
queryString = queryString & ") ORDER BY QuoteID"
Else
queryString = strSelect1 & strFilter & " J.Quote=Q.ID "
queryString = queryString & ") ORDER BY QuoteID"
End If
Me.QuoteSearchResults.Form.RecordSource = queryString
End If
The problem is for each quote that has an associated job I get two entries in the results....for example
Job: 4 --- Quote: 2 --- Date: 12/12/07 --- Status: 'NEW'
Job: - --- Quote: 2 --- Date: 12/12/07 --- Status: 'ACCEPTED'
I only want the first row to show; that is - if a quote has a job then both the job and quote number should be shown. If a quote doesn't have a job then just show the quote number.
Any idea how I can achieve this? Sorry if this is hard to understand.
Thanks in advance
