I got a problem with Delphi.
I have to compare a string of 4 char, with data in database which is 6 char long (postal code(netherlands)).
What I have now is:
procedure Tfmpostcode.Button1Click(Sender: TObject);
var
postcode : string;
target: string;
begin
postcode := ePostcode.text;
target := leftStr(postcode,4);
dm.atinlog.filter := 'postcode =' + (target);
dm.atinlog.filtered := true;
dbgrid1.visible := true;
end开发者_C百科;
I have been searching the net, and I found you could use ansistring. but it gives an error.
missing operator or semicolon
What should I do?
I updated the code
Additional info:
I need to compare a part of the string with data in database If i enter 5504BX in edit field the dbgrid should show up with 5504LA 5504KJ all that starts with the first 4 numbers. the letters doesnt matter.
I think you need to quote the string. Use QuotedStr.
dm.atinlog.filter := 'postcode = ' + QuotedStr(target);
If you need a wild-card match you can do
dm.atinlog.filter := 'postcode like ' + QuotedStr(target+'%');
You didn't give a line associated with the error, but if I had to guess, I'd say it's complaining about the line that says
target := AnsileftStr(postcode,4);
You declared AnsileftStr as a variable, then you're trying to use it with syntax that looks like a function call with two arguments. Are you trying to call the AnsiLeftStr
function from the StrUtils
unit? If so, your code would probably work fine if you removed the variable declaration and made sure that StrUtils
is in your uses clause.
You can use the function LeftStr from StrUtils.pas
function LeftStr(const AText: AnsiString; const ACount: Integer): AnsiString; overload;
function LeftStr(const AText: WideString; const ACount: Integer): WideString; overload;
or
function AnsiLeftStr(const AText: string; const ACount: Integer): string; overload;
But don't declare it as a variable.
Try:
procedure Tfmpostcode.Button1Click(Sender: TObject);
var
postcode : string;
target : string;
begin
postcode := ePostcode.text;
target := LeftStr(postcode, 4);
//...
end;
But, because the postalcode is entered from within a edit box. You probably need some safeguards (only accept alphanumeric chars) or use Trim(ePostcode.text) to delete extra spaces. And if you want to get the last part, don't forget that some people add a space between the number and the letters (1234 AB).
精彩评论