0

I got this code working for my app's search box, unfortunately when it search or filters, it checks every row on the database and then when the searched word is found, it will display it on the tlabels,

procedure Tspcb.dccolbtnClick(Sender: TObject);
begin
  zdctable.First;
  while not zdctable.EOF do
  begin
     if (zdctable.FieldByName('Collector').AsString = dcedit.Text)
     then begin
        cn.Caption := zdctable.FieldByName('Client_Name').AsString;
        col.Caption := zdctable.FieldByName('Collector').AsString;
        pay.Caption := zdctable.FieldByName('Daily_Payment').AsString;
        date.Caption := zdctable.FieldByName('Date').AsString;
        ddate.Caption := zdctable.FieldByName('Due_Date').AsString;
        id.Caption := zdctable.FieldByName('ID').AsString;
        la.Caption := zdctable.FieldByName('Loan').AsString;
        tc.Caption := zdctable.FieldByName('Total_Collectibles').AsString;
     end;

     ShowMessage('click ok for next profile');
     zdctable.Next;
  end;
end;

what i want to do is for it to check rows that has the searched word on it, and not searching every row in the database.

i need help on analyzing this code, im just a beginner who is learning how to use delphi 7 and to program.

thank you

Chunk Chunk
  • 143
  • 6
  • 17
  • 1
    You're not asking a specific question but asking someone to do what you're supposed to do, which is to analyze how this code work and change it to work as you want. This kind of question is _too localized_ for the overly broad audience of StackOverflow. – jachguate Apr 11 '13 at 19:03
  • Start by taking the `zdctable.First` out. You're returning to the start of the dataset every time the button is clicked, which naturally will prevent you from seeing any other rows except the first match. Click button->Go to First Row->Find first match->Click button->Go to First Row->Find first match->Click button... Then see the help about `TDataSet.FindFirst` and `TDataSet.FindNext` to remove the loop entirely. – Ken White Apr 11 '13 at 19:23
  • sorry if my question seemed localized and i ask to be spoonfed. im just a beginner and not familiar with the terms to use on asking help but i do not want to be spoonfed, just guidance is enough because this code is the best that i can do atm with a help from a friend. thanks @KenWhite thank you kind sir for the response i will look into the matter. sorry if my questions always seemed that i ask spoonfeeding here – Chunk Chunk Apr 11 '13 at 19:27
  • 1
    You're using Delphi 7. It comes with a demo app (unfortunately still based on the BDE, but works) called `MastApp`. You should find it in your Demos folder (don't remember where that was on D7, but you should find it in your Start menu). Looking at what it does and how it does it should give you some good starting points. Also, you might look for a Delphi tutorial through Google. :-) – Ken White Apr 11 '13 at 19:32
  • have read the delphi tutorial in about.com, and someone there helped me to construct this code (thanks to his guidance) but the problem is that i don't know where i would edit the code to make it search for the next closest entry to the text in the tedit. sadly i cannot find the tutorial even in the start menu. but anyways thanks a lot sir i will read a lot on the tips you gave me. – Chunk Chunk Apr 11 '13 at 19:38
  • Have you looked at http://docwiki.embarcadero.com/Libraries/XE3/en/Data.DB.TDataSet.Filter property? – Gerry Coll Apr 13 '13 at 04:28

1 Answers1

1

The problem basically is with the line

if (zdctable.FieldByName('Collector').AsString = dcedit.Text)

Here you're looking for equality, so you're never going to be able to match partial strings. I suggest that you change your query (ie zdctable) so that it looks something like this

select * from zdctable
where collector like :p1

Your program would then be

zdctable.params[0].asstring:= '%' + dcedit.text + '%';
zdctable.open;

If your table is linked to a grid via a datasource, then all the matching records will be displayed; there will be no need to extract each field and show it in a different label. If you want to show one record at a time with a navigator, then turn your labels into dbtext components which are linked to the dataset; if these components are set up correctly then each field in your dataset will be displayed in a different dbText component.

Another possibility is using the 'locate' method. If I convert your statement to use 'locate', then you have written the equivalent of

zdctable.locate ('collector', dcedit.text, [])

In order to locate partial matches, you would need to write

zdctable.locate ('collector', dcedit.text, [loPartialKey])
No'am Newman
  • 6,395
  • 5
  • 38
  • 50