Board index » delphi » TTable Searching Question...

TTable Searching Question...

I've got a question about searching TTables in Delphi 16 (So I can't use
locate or lookup methods).

I've been using delphi 1.01 to create a database front end on a Paradox
Table, and am impressed with the ease of designing browsing
functionality, but am finding implementing searches a bit daunting for
someone rather new to delphi.

I'm trying to implement a search engine for my database that searches an
entire table for a given string in an edit box, and then returns a
complete list of results.  My original thought was to create a new
identical empty table, and then copy all the fields[] properties for
each record into a "results" table.   I've been finding this a little
more difficult than I expected.  Is there anyone who has any prewritten
code or searching objects/components that would fill this gap (Again, it
needs to use GotoKey and/or FindKey, not Locate or Lookup).

Thanks in Advance,

Sean Upton
Correct Technologies

 

Re:TTable Searching Question...


On Sat, 21 Dec 1996 22:58:25 -0800, "Sean D. Upton"

Quote
<socra...@shell.aros.net> wrote:
>I've got a question about searching TTables in Delphi 16 (So I can't use
>locate or lookup methods).

>I've been using delphi 1.01 to create a database front end on a Paradox
>Table, and am impressed with the ease of designing browsing
>functionality, but am finding implementing searches a bit daunting for
>someone rather new to delphi.

>I'm trying to implement a search engine for my database that searches an
>entire table for a given string in an edit box, and then returns a
>complete list of results.  My original thought was to create a new
>identical empty table, and then copy all the fields[] properties for
>each record into a "results" table.   I've been finding this a little
>more difficult than I expected.  Is there anyone who has any prewritten
>code or searching objects/components that would fill this gap (Again, it
>needs to use GotoKey and/or FindKey, not Locate or Lookup).

>Thanks in Advance,

>Sean Upton
>Correct Technologies

Here is my code to implement this.  I am using D2 Developer version,
so you should simulate the DataModule in D1.  In my application, I
{*word*128}popup an edit window and get the string to be searched.  Then in
the second window I have a TQuery component which use regular select
with 1 PARAM.  Then the query is executed and show the result in the
second window.  Hope this help.

procedure TFrmPathSelect.BitBtnSearchIncumbentClick(Sender: TObject);

var sIncumName : string;
    iTrackingNum : integer;

begin
  Application.CreateForm(TFrmGetIncumName,FrmGetIncumName);
  FrmGetIncumName.ShowModal;
  if FrmGetIncumName.OKBtn.ModalResult = mrOK then
     begin
        Application.CreateForm(TFrmSQLBrowse,FrmSQLBrowse);
        if CostingDataModule.Query1.RecordCount <> 0 then
           begin
              FrmSQLBrowse.ShowModal;
              if FrmSQLBrowse.BitBtnConfirm.ModalResult = mrOK then
                 begin
                    iTrackingNum :=
CostingDataModule.Query1.FieldByName('TRACKINGNUM').AsInteger;
                    with CostingDataModule.TblPOPath do begin
                       Close;
                       MasterSource := Nil;
                       IndexFieldNames := 'TRACKINGNUM';
                       Open;
                       Setkey;
                       FieldByName('TRACKINGNUM').AsInteger :=
iTrackingNum;
                       GotoKey;
                    end; {with CostingDataModule.TblPOPath}
                    pReposition(iTrackingNum);
                    FrmPathSelect.Refresh;
                 end; {if FrmSQLBrowse.ShowModal = mrOK}
            end {CostingDataModule.Query1.RecordCount <> 0}
        else MessageDlg('No record found', mtInformation,[mbOk], 0)
     end {FrmGetIncumName.ShowModal = mrOK}
  else
     FrmPathSelect.Refresh;
end;

Re:TTable Searching Question...


Sean,

here are three simple methods:

1. use TQuery check out "like"
2. use TTables: see below
3. use a TTable.filter

ClearList;
table1.first;
while not table1.eof do
        begin
        for i:= 0 to table1.fields.count-1 do
                begin
                if pos(SearchText, Table1.Fields[i].asString)>0 then AddRecordToList;
                end;
        table1.next;
        end;

You can use many different methods to show the end user the results: a
TListbox is easy.  

Or you could create a new table and use a DBGrid if you want - and it's
easy:

var T:TTable;

procedure ClearList;
begin
{if you're using a listbox}
ListBox1.clear;

{if you're using a table}
{create T}
        if assigned(T) then T.destroy;
        T:= TTable.create(nil);
{just copy the properties of Table1 to T}
        T.databasename:= Table1.databasename;
        T.tablename:= 'temp.dbf';
        Table1.FieldDefs.refresh;
        T.IndexDefs.assign(Table1.FieldDefs);
        Table1.IndexDefs.refresh;
        T.IndexDefs.assign(Table1.IndexDefs);
{create the table on the disk ...}
        T.createtable;
        T.open;
end;

procedure AddRecordToList;
var i:integer;
begin
{if you're using a listbox}
        ListBox1.items.add(Table1.fields[0].asString+ ....);

{if you're using a table}
        T.insert;
        for i:= 0 to table1.fields.count-1 do
                begin
                T.fields[i].value:=Table1.Fields[i].value;
                end;
        try T.post; except T.cancel; raise; end;
end;

At some stage you need to destroy T (eg. in the FormClose event):
T.destroy;

HTH,

--
Paul Motyer
pa...@linuxserver.pccity.com.au
Croydon, Australia, 3136

Sean D. Upton <socra...@shell.aros.net> wrote in article
<32BCDC11.7...@shell.aros.net>...

Quote
> I've got a question about searching TTables in Delphi 16 (So I can't use
> locate or lookup methods).

> I've been using delphi 1.01 to create a database front end on a Paradox
> Table, and am impressed with the ease of designing browsing
> functionality, but am finding implementing searches a bit daunting for
> someone rather new to delphi.

> I'm trying to implement a search engine for my database that searches an
> entire table for a given string in an edit box, and then returns a
> complete list of results.  My original thought was to create a new
> identical empty table, and then copy all the fields[] properties for
> each record into a "results" table.   I've been finding this a little
> more difficult than I expected.  Is there anyone who has any prewritten
> code or searching objects/components that would fill this gap (Again, it
> needs to use GotoKey and/or FindKey, not Locate or Lookup).

> Thanks in Advance,

> Sean Upton
> Correct Technologies

Re:TTable Searching Question...


"Sean D. Upton" <socra...@shell.aros.net> wrote:

Quote
>I'm trying to implement a search engine for my database that searches an
>entire table for a given string in an edit box, and then returns a
>complete list of results.  My original thought was to create a new
>identical empty table, and then copy all the fields[] properties for
>each record into a "results" table.   I've been finding this a little
>more difficult than I expected.  Is there anyone who has any prewritten
>code or searching objects/components that would fill this gap (Again, it
>needs to use GotoKey and/or FindKey, not Locate or Lookup).

I concur with  the previous posting (Paul Moyer?). I had a tough time
with trying to implement a 'FIND NEXT' like Paradox offers and this,
with the BDE is not obvious. To this date I still do not have a FIND
NEXT routine SO, I implement the filter command for the table, based
on a TEdit box or similar component. It works great and is easy to
remove at a click of a button. The great thing about implementing a
filter is the user's ability to edit a record 'LIVE'.

As for Delphi being a good front end for Paradox, it's TERRIFIC IMHO.

Hope this helps...

Bill Artemik
-----------------------------------
arte...@ix.netcom.com

Re:TTable Searching Question...


Quote
> I'm trying to implement a search engine for my database that searches an
> entire table for a given string in an edit box, and then returns a
> complete list of results.  My original thought was to create a new

Two ways:

1) If you wish the search results to be editable, take a look at filters,
rather than a search and extract.

2) Add the results to a list box or grid as you find them. The following
handles this:

{ Clear the search results }
lstResults.Clear;

if tblCust.FindNearest(['Prit']) then
        while (not tblCust.Eof) and (Copy(tblCust.FieldByName('Name'),1,4) =
'Prit') do
                lstResults.Items.Add(tblCust.FieldByName('Name'));

This is pretty {*word*99} code, but will give you a push in the right direction.

--

Mark P
-----------
http://www.ozemail.com.au/~pritchma

Other Threads