Private Sub cmdRunQuery_Click()
Dim db As DAO.Database
Dim QD As DAO.QueryDef
Dim where As Variant
Dim strSQL As String
Set db = CurrentDb()
' Delete the existing dynamic query; trap the error if the query
' does not exist.
On Error Resume Next
db.QueryDefs.Delete ("Dynamic_Query")
On Error GoTo 0
where = Null
'Report Type
If left(Me![txtReportType], 1) = "*" Or right([txtReportType], 1) = "*" Then
where = where & " AND [ReportType] like '" + Me![txtReportType] + "'"
Else
where = where & " AND [ReportType]= '" + Me![txtReportType] + "'"
End If
'ReportNum field
If left(Me![txtHumintReportNumber], 1) = "*" Or right([txtHumintReportNumber], 1) = "*" Then
where = where & " AND [HumintReportNum] like '" + Me![txtHumintReportNumber] + "'"
Else
where = where & " AND [HumintReportNum]= '" + Me![txtHumintReportNumber] + "'"
End If
'Status field
If left(Me![txtStatus], 1) = "*" Or right([txtStatus], 1) = "*" Then
where = where & " AND [Status] like '" + Me![txtStatus] + "'"
Else
where = where & " AND [Status]= '" + Me![txtStatus] + "'"
End If
'Event ID
If left(Me![txtEventID], 1) = "*" Or right([txtEventID], 1) = "*" Then
where = where & " AND [EventID] like '" + Me![txtEventID] + "'"
Else
where = where & " AND [EventID]= '" + Me![txtEventID] + "'"
End If
'Source field
If left(Me![txtSource], 1) = "*" Or right([txtSource], 1) = "*" Then
where = where & " AND [Source] like '" + Me![txtSource] + "'"
Else
where = where & " AND [Source]= '" + Me![txtSource] + "'"
End If
'Classification field
If left(Me![txtClassification], 1) = "*" Or right([txtClassification], 1) = "*" Then
where = where & " AND [Classification] like '" + Me![txtClassification] + "'"
Else
where = where & " AND [Classification]= '" + Me![txtClassification] + "'"
End If
'AOR field
If left(Me![txtAOR], 1) = "*" Or right([txtAOR], 1) = "*" Then
where = where & " AND [AOR] like '" + Me![txtAOR] + "'"
Else
where = where & " AND [AOR]= '" + Me![txtAOR] + "'"
End If
'OpType
If left(Me![txtOpType], 1) = "*" Or right([txtOpType], 1) = "*" Then
where = where & " AND [OpType] like '" + Me![txtOpType] + "'"
Else
where = where & " AND [OpType]= '" + Me![txtOpType] + "'"
End If
'TrackStatus
If left(Me![txtTrackStatus], 1) = "*" Or right([txtTrackStatus], 1) = "*" Then
where = where & " AND [TrackStatus] like '" + Me![txtTrackStatus] + "'"
Else
where = where & " AND [TrackStatus]= '" + Me![txtTrackStatus] + "'"
End If
'HumintDepartCountry
If left(Me![txtHumintDepartCountry], 1) = "*" Or right([txtHumintDepartCountry], 1) = "*" Then
where = where & " AND [HumintDepartCountry] like '" + Me![txtHumintDepartCountry] + "'"
Else
where = where & " AND [HumintDepartCountry]= '" + Me![txtHumintDepartCountry] + "'"
End If
'HumintArriveCountry
If left(Me![txtHumintArriveCountry], 1) = "*" Or right([txtHumintArriveCountry], 1) = "*" Then
where = where & " AND [HumintArriveCountry] like '" + Me![txtHumintArriveCountry] + "'"
Else
where = where & " AND [HumintArriveCountry]= '" + Me![txtHumintArriveCountry] + "'"
End If
'ReportText
If left(Me![txtReportText], 1) = "*" Or right([txtReportText], 1) = "*" Then
where = where & " AND [ReportText] like '" + Me![txtReportText] + "'"
Else
where = where & " AND [ReportText]= '" + Me![txtReportText] + "'"
End If
'Dates
If Not IsNull(Me![To Date]) Then
where = where & " AND [BaseReportDate] between '" + Me![From Date] + " AND " & Me![To Date] + "'"
'where = where & " AND [BaseReportDate] between #" + Me![From Date] + "# AND #" & Me![To Date] & "#"
Else
where = where & " AND [BaseReportDate] >= '" + Me![From Date] + "'"
'where = where & " AND [BaseReportDate] >= #" + Me![From Date] + " #"
End If
strSQL = "SELECT tblHumintCases.*, tblIntelAgencies.*, tblLogos.*, tblReportText.* FROM ((tblLogos RIGHT JOIN tblHumintCases ON tblLogos.LogoID = tblHumintCases.LogoID) LEFT JOIN tblIntelAgencies ON tblHumintCases.Source = tblIntelAgencies.AgencyName) LEFT JOIN tblReportText ON tblHumintCases.HumintID = tblReportText.HumintID " & (" where " + Mid(where, 6) & ";")
'MsgBox strSQL
Set QD = db.CreateQueryDef("Dynamic_Query", strSQL)
'Make the Search Results subform visible again.
'Show search results in subform.
Me.fsubSearchResults.Visible = True
Me.fsubSearchResults.Form.RecordSource = strSQL
End Sub