Locate not working ?

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Locate not working ?

Post by marsheng » Tue 29 May 2012 08:02

I'm sure it is obvious but I can't see it.

Code: Select all

procedure TfMain.Button3Click(Sender: TObject);
var
   sMemId:String;
begin
   with dm.tblSubs do begin
     first;
     while not eof do begin
        sMemId:=FieldByName('MemId').asString;
        if dm.tblMembers.Locate('MemId','sMemId',[])then begin
           Edit;
           FieldByName('MemName').asString:=dm.tblMembersMEMNAME.AsString;
           Post;
        end;
        next;
     end;
   end;
end;
I have tried both integer and string and neither work.

MemId is MediumInt of length 9

Any Suggestions ?

AndreyZ

Re: Locate not working ?

Post by AndreyZ » Tue 29 May 2012 09:09

Hello,

The Locate method takes three parameters. The first is names of the key fields, the second is key fields values, and the third is searching options. In your code you are looking for the constant string 'sMemId' instead of the sMemId variable value. To solve the problem, you should use the following code:

Code: Select all

sMemId := FieldByName('MemId').asString;
if dm.tblMembers.Locate('MemId', sMemId, []) then begin
You can find the detailed description and the example of using of the Locate method in the MyDAC documentation.

marsheng
Posts: 62
Joined: Thu 10 May 2012 10:51

Re: Locate not working ?

Post by marsheng » Tue 29 May 2012 10:18

Damn, I looked at that code 20 + times and missed I had inserted the second set of quotes.

I've have just changed it to

Code: Select all

        if dm.tblMembers.Locate('MemId',FieldByName('MemId').asString,[])then begin
which obviously works.

Thanks Wallace

AndreyZ

Re: Locate not working ?

Post by AndreyZ » Tue 29 May 2012 12:17

If any other questions come up, please contact us.

Post Reply