I need to filter a DBGrid on a column of emails. I want to filter on the first letter. For example if the email is sam@Yahoo,com, I want to display all emails tha start with the letter ‘s’. All of the emails have been converted to lower case.
Here is my code:
Form_Main.AdoTable2.First;
FilterVal := copy(email,1,1);
Filter := 'email = ' + QuotedStr('FilterVal*');
Filtered := true;
FilterVal returns the first letter of the email address.
The program runs but there are no emails listed in the DBGrid. If I set the Filtered to false all emails are listed.
What is the problem with my Filter statement?
|
Re: Fi;ter using wildcard |
|
Mervin wrote:
> What is the problem with my Filter statement?
You want to do a substring search, but you are using the '=' SQL operator,
which does an exact match comparison. You need to use the 'LIKE' operator
instead (with the '%' wildcard, not '*'), and you are not passing your FilterVal
to QuoteStr() correctly, either.
Try this instead:
{code}
Form_Main.AdoTable2.First;
FilterVal := Copy(email,1,1);
Filter := 'email LIKE ' + QuotedStr(FilterVal+'%');
Filtered := true;
{code}
--
Remy Lebeau (TeamB)
|