Openform with search criteria
Friends,
Having trouble making my search form work correctly. I have a search form which returns records in a query (Dynamic_Query). Instead of the query, I want the the retrieved records to be displayed in the form that I use for data entry. I am not sure how to write the where clause for the filter argument of the openform method. I have a number of different things and most recently tried to set the QueryDef SQL string to a variable and use that for the filter argument, but no joy. Any help on this would be greatly appreciated. Here is a the code I am using (shortened) to help illustrate my problem.
'''''''
Option Compare Database
Option Explicit
Private Sub cmdRunQuery_Click()
Dim db As DAO.Database
Dim QD As 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]= '" + 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]= '" + 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]= '" + Me![txtStatus] + "'"
Else
where = where & " AND [Status]= '" + Me![txtStatus] + "'"
End If
Set QD = db.CreateQueryDef("Dynamic_Query", _
"Select * from (tblLogos RIGHT JOIN tblHumintCases ON tblLogos.LogoID = tblHumintCases.LogoID) LEFT JOIN tblIntelAgencies ON tblHumintCases.Source = tblIntelAgencies.AgencyName " & (" where " + Mid(where, 6) & ";"))
DoCmd.OpenQuery "Dynamic_Query"
'DoCmd.OpenForm "frmHumintCasesDE", acNormal, , "frmHumintCases.RecordSource = '" & "Dynamic_Query"
'Clear fields on search form
Me.txtHumintReportNumber = Null
Me.txtReportType = Null
Me.txtStatus = Null
End Sub
Friends,
Having trouble making my search form work correctly. I have a search form which returns records in a query (Dynamic_Query). Instead of the query, I want the the retrieved records to be displayed in the form that I use for data entry. I am not sure how to write the where clause for the filter argument of the openform method. I have a number of different things and most recently tried to set the QueryDef SQL string to a variable and use that for the filter argument, but no joy. Any help on this would be greatly appreciated. Here is a the code I am using (shortened) to help illustrate my problem.
'''''''
Option Compare Database
Option Explicit
Private Sub cmdRunQuery_Click()
Dim db As DAO.Database
Dim QD As 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]= '" + 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]= '" + 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]= '" + Me![txtStatus] + "'"
Else
where = where & " AND [Status]= '" + Me![txtStatus] + "'"
End If
Set QD = db.CreateQueryDef("Dynamic_Query", _
"Select * from (tblLogos RIGHT JOIN tblHumintCases ON tblLogos.LogoID = tblHumintCases.LogoID) LEFT JOIN tblIntelAgencies ON tblHumintCases.Source = tblIntelAgencies.AgencyName " & (" where " + Mid(where, 6) & ";"))
DoCmd.OpenQuery "Dynamic_Query"
'DoCmd.OpenForm "frmHumintCasesDE", acNormal, , "frmHumintCases.RecordSource = '" & "Dynamic_Query"
'Clear fields on search form
Me.txtHumintReportNumber = Null
Me.txtReportType = Null
Me.txtStatus = Null
End Sub