Can someone take a look at my filter coding. When I give the form a value to search by, the subform returns all related values instead of exactly those that match. For instance, my form has a combo box to search for the appropriate name and populates the box with the name id. If I pick 3 John Smith from the pull down, and hit the search button, I get all matching records for 3 (John Smith), but also any records for name id's that begin with 3, such as 31 Leo Smith, to 39 Zach Smith and 300 (John Doe)! I think the problem is in the Select statement, but I'm not a programmer, so I can't figure out how to fix it. The query2 works perfect by itself, so I know it's not a problem with the query.
Private Sub Show_matching_Click()
' Create a WHERE clause using search criteria entered by user and
' set RecordSource property of Query2 subform.
Dim MySQL As String, MyCriteria As String, MyRecordSource As String
Dim ArgCount As Integer
Dim Tmp As Variant
' Initialize argument count.
ArgCount = 0
' Initialize SELECT statement.
MySQL = "SELECT * FROM query2 WHERE "
MyCriteria = ""
' Use values entered in text boxes in form header to create criteria for WHERE clause.
AddToWhere [Look For 1st Name], "[query cross names1].[name_id]", MyCriteria, ArgCount
AddToWhere [Look For 2nd Name], "[query cross names2].[name_id]", MyCriteria, ArgCount
AddToWhere [Look For 3rd Name], "[query cross names3].[name_id]", MyCriteria, ArgCount
'If no criterion specified, return all records.
If MyCriteria = "" Then
MyCriteria = "True"
End If
'Create SELECT statement
MyRecordSource = MySQL & MyCriteria
'Set RecordSource property of Query2 subform.
Me![Query2 subform].Form.RecordSource = MyRecordSource
'If no records match criteria, then display message.
'Otherwise, move focus to the Clear button.
If Me![Query2 subform].Form.RecordsetClone.RecordCount = 0 Then
MsgBox "No records found to match the criteria you entered.", 48, "No records Found"
Me!Clear.SetFocus
Else
'Enable control in the detail section.
Tmp = EnableControls("Detail", True)
'Move insertion point to Query2 subform.
Me![Query2 subform].SetFocus
End If
End Sub
Thanks
Private Sub Show_matching_Click()
' Create a WHERE clause using search criteria entered by user and
' set RecordSource property of Query2 subform.
Dim MySQL As String, MyCriteria As String, MyRecordSource As String
Dim ArgCount As Integer
Dim Tmp As Variant
' Initialize argument count.
ArgCount = 0
' Initialize SELECT statement.
MySQL = "SELECT * FROM query2 WHERE "
MyCriteria = ""
' Use values entered in text boxes in form header to create criteria for WHERE clause.
AddToWhere [Look For 1st Name], "[query cross names1].[name_id]", MyCriteria, ArgCount
AddToWhere [Look For 2nd Name], "[query cross names2].[name_id]", MyCriteria, ArgCount
AddToWhere [Look For 3rd Name], "[query cross names3].[name_id]", MyCriteria, ArgCount
'If no criterion specified, return all records.
If MyCriteria = "" Then
MyCriteria = "True"
End If
'Create SELECT statement
MyRecordSource = MySQL & MyCriteria
'Set RecordSource property of Query2 subform.
Me![Query2 subform].Form.RecordSource = MyRecordSource
'If no records match criteria, then display message.
'Otherwise, move focus to the Clear button.
If Me![Query2 subform].Form.RecordsetClone.RecordCount = 0 Then
MsgBox "No records found to match the criteria you entered.", 48, "No records Found"
Me!Clear.SetFocus
Else
'Enable control in the detail section.
Tmp = EnableControls("Detail", True)
'Move insertion point to Query2 subform.
Me![Query2 subform].SetFocus
End If
End Sub
Thanks