unit Finder; interface uses DB, DBTables, SysUtils; function GrabMemoFieldAsPChar(TheField : TMemoField): PChar; function DoFindIn(TheField : TField; SFor : String): Boolean; function FindIt(TheTable : TDataSet; TheFields : array of integer; SearchBackward : Boolean; FromBeginning : Boolean; SFor : String): Boolean;{применение функции FindIt - if FindIt(NotesSearchT,[NotesSearchT.FieldByName('Leadman').Index],False, True, SearchText.Text) then DoSomething; } implementation function GrabMemoFieldAsPChar(TheField : TMemoField): PChar; begin with TBlobStream.Create(TheField, bmRead) do beginGetMem(Result, Size + 1);FillChar(Result^, Size + 1, #0);Read(Result^, Size);Free;end;end; function DoFindIn(TheField : TField; SFor : String): Boolean; var PChForMemo : PChar;begin Result := False; case TheField.DataType of ftString :beginif (Pos(SFor, UpperCase(TheField.AsString)) > 0) thenResult := True;end;ftInteger :beginif (Pos(SFor, TheField.AsString) > 0) then Result := True;end;ftBoolean :beginif SFor = UpperCase(TheField.AsString) thenResult := True;end;ftFloat :beginif (Pos(SFor, TheField.AsString) > 0) then Result := True;end;ftCurrency :beginif (Pos(SFor, TheField.AsString) > 0) then Result := True;end;ftDate .. ftDateTime :beginif (Pos(SFor, TheField.AsString) > 0) then Result := True;end;ftMemo :beginSFor[Ord(SFor[0]) + 1] := #0;PChForMemo := GrabMemoFieldAsPChar(TMemoField(TheField));StrUpper(PChForMemo);if not (StrPos( PChForMemo, @SFor[1] ) = nil) then Result :=True; FreeMem(PChForMemo, StrLen(PChForMemo + 1));end;end;end; function FindIt(TheTable : TDataSet; TheFields : array of integer; SearchBackward : Boolean; FromBeginning : Boolean; SFor : String): Boolean;var i, HighTheFields, LowTheFields : integer;BM : TBookmark;begin TheTable.DisableControls; BM := TheTable.GetBookmark; try LowTheFields := Low(TheFields); HighTheFields := High(TheFields); SFor := UpperCase(SFor); Result := False; if FromBeginning then TheTable.First; if SearchBackward then beginTheTable.Prior;while not TheTable.BOF dobeginfor i := LowTheFields to HighTheFields dobeginif DoFindIn(TheTable.Fields[TheFields[i]], SFor) thenbeginResult := True;Break;end;end;if Result then Break else TheTable.Prior;end;end elsebeginTheTable.Next;while not TheTable.EOF dobeginfor i := LowTheFields to HighTheFields dobeginif DoFindIn(TheTable.Fields[TheFields[i]], SFor) thenbeginResult := True;Break;end;end;if Result then Break else TheTable.Next;end;end;finally TheTable.EnableControls; if not Result then TheTable.GotoBookmark(BM);TheTable.FreeBookmark(BM); end; end; end. |