Good Day, im using this code to filter my searchbox, what other codes can i use to set more than one keyword in the searchbox? thanks guys :) :) :) :)sorry I'm lost are asking how to create a a parser and a small interpreter from string expression to boolean checks or you are looking to learn about the dataset.filter and filtered properties? Are you going to have a single editor for your filter or are you ok with having multiple editors one for each word? How about splitting the string typed in an array of words using the space as a delimeter and checking all the words one by one until you either find one or all return false?
Accept := Pos(UpperCase(edtSearch.Text), UpperCase(DataSet.FieldByName('CommittedBy').AsString)) > 0;
If you wish to filter by multiple keywords, just add more searchboxes:
procedure TForm1.SQLQuery1FilterRecord(DataSet: TDataSet; var Accept: Boolean); begin Accept := (Pos(UpperCase(edCommitedBy.Text), UpperCase(DataSet.FieldByName('COMMITTEDBY').AsString)) > 0) and (Pos(UpperCase(edCategory.Text), UpperCase(DataSet.FieldByName('CATEGORY').AsString)) > 0); end;
Of course in this case you must force the user to enter the text both for edCommitBy and edCategory. Adding more filtering options is also possible:I would put a checkbox in front of the searchboxes. When the checkbox is checked the searchbox becomes enabled otherwise is disabled. By knowing which checkbox is checked you can set up the filter(s).
procedure TForm1.SQLQuery1FilterRecord(DataSet: TDataSet; var Accept: Boolean); begin if UserFilterOption1 then //filter only by category for example Accept := Filter1 else if UserFileterOption2 then //filter both for category and commitby Accept := Filter2 //... end;
For more then 3-4 filters though, you better use the sql where clause and pass the values as params, like this:
Then from Lazarus:
SQLQuery1.Params.BeginUpdate; SQLQuery1.ParamByName('pFIELD1').AsInteger = -1; SQLQuery1.ParamByName('pFIELD2').AsString = 'something'; SQLQuery1.Params.ParamValues['pField3'] := null; //passing null if needed SQLQuery1.Params.EndUpdate;
what do you think is the problem.??The problem is that you don't listen. As I said in my previous post you have to force the user to enter the text both for edCommitBy and edCategory, otherwise it won't work. Another solution is to ingore empty searchboxes, like this: