All,
I got the following code working from a command button on a search form. The search form has 5 fields to search on. My only problem now is that I can only enter one search criteria at a time to get the correct results. It doesn't work when I put more than one search criteria in. For instance if I only search one field (fieldA1) for a name and I put that name in, I will get the records with that name.
If I want to search on fieldA1 and field 2 the search will come back with no results. What is wrong with the code below?
Private Sub cmdSearch_Click()
'Set the Dimensions of the Module
Dim strSQL As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()
'Constant Select statement for the Query definition
strSQL = "SELECT tbl_test.MainID, tbl_test.field1, tbl_test.field2, tbl_test.field3, tbl_sub1.MainID, tbl_sub1.fieldA1, tbl_sub1.fieldA2, tbl_sub2.MainID, tbl_sub2.fieldB1, tbl_sub2.fieldB2"
strSQL = strSQL & "FROM (tbl_test INNER JOIN tbl_sub1 ON tbl_test.MainID = tbl_sub1.MainID) INNER JOIN tbl_sub2 ON tbl_test.MainID = tbl_sub2.MainID "
strSQL = "SELECT tbl_test.*" ',tbl_sub1.*,tbl_sub2.*"
strSQL = strSQL & "FROM tbl_test "
'strSQL = strSQL & "INNER JOIN tbl_sub1 ON tbl_test.MainID = tbl_sub1.MainID)"
'strSQL = strSQL & "INNER JOIN tbl_sub2 ON tbl_test.MainID = tbl_sub2.MainID)"
strWhere = "WHERE"
strOrder = "ORDER BY tbl_test.MainID;"
'Set the WHERE clause for the QueryDef if information has been entered into a field on the form
If Not IsNull(Me.field1) Then '<--If the textbox field1 contains no data THEN do nothing
strWhere = strWhere & " (tbl_test.field1) Like '*" & Me.field1 & "*' AND" '<--otherwise, apply the LIKE statment to the QueryDef
End If
If Not IsNull(Me.field2) Then
strWhere = strWhere & " (tbl_test.field2) Like '*" & Me.field2 & "*' AND"
End If
If Not IsNull(Me.field3) Then
strWhere = strWhere & " (tbl_test.field3) Like '*" & Me.field3 & "*' AND"
End If
If Not IsNull(Me.fieldA1) Then
strWhere = strWhere & " (tbl_sub1.fieldA1) Like '*" & Me.fieldA1 & "*' AND"
strSQL = strSQL & "INNER JOIN tbl_sub1 ON tbl_test.MainID = tbl_sub1.MainID "
End If
If Not IsNull(Me.fieldA2) Then
strWhere = strWhere & " (tbl_sub1.fieldA2) Like '*" & Me.fieldA2 & "*' AND"
strSQL = strSQL & "INNER JOIN tbl_sub1 ON tbl_test.MainID = tbl_sub1.MainID "
End If
'Remove the last AND from the SQL statment
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the QueryDef to the query
Set qryDef = dbNm.QueryDefs("qry_test"
qryDef.SQL = strSQL & " " & strWhere & "" & strOrder
'Open the Query
DoCmd.OpenReport "rpt_test", acViewPreview, "", "", acNormal
End Sub
Please help,
Thanks,
Jerome Benton
JERPAT Web Designs
GOD Is Good All The Time!!!
I got the following code working from a command button on a search form. The search form has 5 fields to search on. My only problem now is that I can only enter one search criteria at a time to get the correct results. It doesn't work when I put more than one search criteria in. For instance if I only search one field (fieldA1) for a name and I put that name in, I will get the records with that name.
If I want to search on fieldA1 and field 2 the search will come back with no results. What is wrong with the code below?
Private Sub cmdSearch_Click()
'Set the Dimensions of the Module
Dim strSQL As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()
'Constant Select statement for the Query definition
strSQL = "SELECT tbl_test.MainID, tbl_test.field1, tbl_test.field2, tbl_test.field3, tbl_sub1.MainID, tbl_sub1.fieldA1, tbl_sub1.fieldA2, tbl_sub2.MainID, tbl_sub2.fieldB1, tbl_sub2.fieldB2"
strSQL = strSQL & "FROM (tbl_test INNER JOIN tbl_sub1 ON tbl_test.MainID = tbl_sub1.MainID) INNER JOIN tbl_sub2 ON tbl_test.MainID = tbl_sub2.MainID "
strSQL = "SELECT tbl_test.*" ',tbl_sub1.*,tbl_sub2.*"
strSQL = strSQL & "FROM tbl_test "
'strSQL = strSQL & "INNER JOIN tbl_sub1 ON tbl_test.MainID = tbl_sub1.MainID)"
'strSQL = strSQL & "INNER JOIN tbl_sub2 ON tbl_test.MainID = tbl_sub2.MainID)"
strWhere = "WHERE"
strOrder = "ORDER BY tbl_test.MainID;"
'Set the WHERE clause for the QueryDef if information has been entered into a field on the form
If Not IsNull(Me.field1) Then '<--If the textbox field1 contains no data THEN do nothing
strWhere = strWhere & " (tbl_test.field1) Like '*" & Me.field1 & "*' AND" '<--otherwise, apply the LIKE statment to the QueryDef
End If
If Not IsNull(Me.field2) Then
strWhere = strWhere & " (tbl_test.field2) Like '*" & Me.field2 & "*' AND"
End If
If Not IsNull(Me.field3) Then
strWhere = strWhere & " (tbl_test.field3) Like '*" & Me.field3 & "*' AND"
End If
If Not IsNull(Me.fieldA1) Then
strWhere = strWhere & " (tbl_sub1.fieldA1) Like '*" & Me.fieldA1 & "*' AND"
strSQL = strSQL & "INNER JOIN tbl_sub1 ON tbl_test.MainID = tbl_sub1.MainID "
End If
If Not IsNull(Me.fieldA2) Then
strWhere = strWhere & " (tbl_sub1.fieldA2) Like '*" & Me.fieldA2 & "*' AND"
strSQL = strSQL & "INNER JOIN tbl_sub1 ON tbl_test.MainID = tbl_sub1.MainID "
End If
'Remove the last AND from the SQL statment
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the QueryDef to the query
Set qryDef = dbNm.QueryDefs("qry_test"
qryDef.SQL = strSQL & " " & strWhere & "" & strOrder
'Open the Query
DoCmd.OpenReport "rpt_test", acViewPreview, "", "", acNormal
End Sub
Please help,
Thanks,
Jerome Benton
JERPAT Web Designs
GOD Is Good All The Time!!!