ddrew said:
Still having problems with this, I put in a new If statement as you suggested and it produces an error. This is my code as it stands now: In Red is where the error comes. It says "Characters found after teh end of SQL statement"
quot = """"
If Not IsNull(Me.txtExerciseName) Then
strSearch = strSearch & "SELECT tblTechnicalIncidentReport.Exercise_Name FROM tblTechnicalIncidentReport " & _
"WHERE Exercise_Name LIKE " & quot & Me.txtExerciseName.Value & "*" & quot & ";" 'WHERE clause here captures the user input in the textbox TxtExerciseName
End If
'add on more if & end if loops for each controlname(criteria) that you have
If Not IsNull(Me.txtBoxNo) Then
strSearch = strSearch & "SELECT tblTechnicalIncidentReport.BoxNo FROM tblTechnicalIncidentReport " & _
"WHERE BoxNo LIKE " & quot & Me.txtBoxNo.Value & "*" & quot & ";" 'WHERE clause here captures the user input in the textbox TxtBoxNo
End If
Me.Results.Form.RecordSource = strSearch ' Update the record source, this line is to be placed at the end of all your criteria's if end if codesRequery the subform
Me.Results.Requery
End If
End Sub
ok lets focus on ur search filter..which is actually the key element to any search form.
1st: why are 2 instances of WHERE? do u know how does SQL statements function? in the WHERE clause, the 1st criteria is the only one that has WHERE, subsequent criteria uses AND
e.g.
SELECT .....
FROM ....
WHERE tableA.column1 = "X" AND tableA.column2 = "X" AND tableB.column3 = "Z"
in the 2nd piece of code i posted, i again omitted something(my bad), i assumed the excercise name is a mandatory field so i used a "WHERE" statement in it while the rest should be using "AND" since the user should be able to key in multiple criteria
which secondly brings me to yet another question:
2nd: how would u expect your user to enter/select criteria?
would they be selecting/entering ONLY ONE criteria for every search?
or would they be able to enter ONE OR MORE criteria and then search, i.e. multiple criteria search?
if its the former than i think it would be better using the method of case & select. if its the latter then the method i stated would work.
what the method i listed out does is:
For every user input, the "If not isnull" condition is met, thus VBA executes the statement and adds the corresponding strSearch to the original strSearch. it continues to add on as long the condition is met, if it isn't, it skips and moves on to the next "if end if" statement until the last.
so in your case(assuming user can enter 1 or more criteria for each search), you have 8 criteria for the user to enter with:
'this is assuming that columnA is the mandatory column to show regardless of what user input, it could be "Analyst_Name"
strSearch = "SELECT columnX FROM tblTechnicalIncidentReport WHERE tblTechnicalIncidentReport.Analyst_Name Is Not Null"
If IsNull(Me.txtExerciseName) Then 'this is to ensure user keys/select at least 1 criteria; add on rest of the controlnames as desired
MsgBox "Please enter at least one criteria"
Else
quot = """"
If Not IsNull(Me.txtExerciseName) Then
strSearch = strSearch & "SELECT tblTechnicalIncidentReport.Exercise_Name FROM tblTechnicalIncidentReport " & _
"AND Exercise_Name LIKE " & quot & Me.txtExerciseName.Value & "*" & quot & ";" 'AND clause here captures the user input in the textbox TxtExerciseName
End If
'add on more if & end if loops for each controlname(criteria) that you have, with AND in front of them
If Not IsNull(Me.txtBoxNo) Then
strSearch = strSearch & "SELECT tblTechnicalIncidentReport.BoxNo FROM tblTechnicalIncidentReport " & _
"AND BoxNo LIKE " & quot & Me.txtBoxNo.Value & "*" & quot & ";" 'AND clause here captures the user input in the textbox TxtBoxNo
End If
'rest of ur controls, each with its own If end if
'rest of the code..
ok so how does it work? if controlname1 is not null, the AND clause is added to the 1st strSearch(strSearch = "SELECT columnX FROM tblTechnicalIncidentReport WHERE tblTechnicalIncidentReport.Analyst_Name Is Not Null")
if controlname2 is not null, it continues to add to the previous entry of strSearch. so let say ur user enters 3 criteria, controlname1, 2 and 4
so strSearch = "SELECT columnX FROM tblTechnicalIncidentReport WHERE tblTechnicalIncidentReport.Analyst_Name Is Not Null AND columnA = controlname1's value AND columnB = controlname2's value AND columnC = controlname4's value"
i hope this is clear enough...try not to copy code blindly..try to understand how it works 1st.
Cheers