{*********************************************************}
{                                                         }
{                     MightyQuery                         }
{            SQL Database Management Software             }
{                                                         }
{   Original developer: Zlatko Matić, 2009-2011           }
{             e-mail: matalab@gmail.com                   }
{  Milkovićeva 6, Mala Ostrna, 10370 Dugo Selo,  Croatia. }
{                                                         }
{*********************************************************}

{-----------------------------------------------------------------------------

The Initial Developer of the Original Code is Zlatko Matić
(matalab@gmail.com or matic.zlatko@gmail.com).

Contributor(s):

Last Modified: 04.06.2011

Known Issues:

History (Change log):
  version 1.0.4, date 04.06.2011 :
		MightyQuery released as free software
-----------------------------------------------------------------------------}

unit Unit3;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, StdCtrls, db, XMLPropStorage,
  Unit1, ZMConnection, ZMQueryDataSet;

type

  { TFormFind }

  TFormFind = class(TForm)
    ButtonFind: TButton;
    ButtonFindNext: TButton;
    ComboBoxFindFields: TComboBox;
    EditFindValue: TEdit;
    Panel1: TPanel;
    Panel2: TPanel;
    Panel3: TPanel;
    Panel4: TPanel;
    Panel5: TPanel;
    Panel6: TPanel;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    procedure ButtonFindClick(Sender: TObject);
    procedure ButtonFindNextClick(Sender: TObject);
  private
    { private declarations }
    FDataSet:TZMQueryDataSet;
  public
    { public declarations }
    constructor Create(var pDataset: TZMQueryDataset);
  end; 

var
  FormFind: TFormFind;

implementation

procedure TFormFind.ButtonFindClick(Sender: TObject);
var
  vSelectedItem:Integer;
  vFound:Boolean;
begin
  vSelectedItem:=ComboBoxFindFields.ItemIndex;
  with FDataSet do begin
    try
      vFound:=False;
      DisableControls;
      try
        vFound:=Locate(ComboBoxFindFields.Items[vSelectedItem],
            EditFindValue.Text,
            [loPartialKey, loCaseInsensitive]);
        if vFound=False then ShowMessage ('Not found.');
      except
        on E:Exception do begin
          ShowMessage(E.Message);
        end;
      end;
    finally
      EnableControls;
    end;
  end;
end;

procedure TFormFind.ButtonFindNextClick(Sender: TObject);
var
  vSelectedItem:Integer;
  vFound:Boolean;
  vBookmark:TBookmark;
begin
  vSelectedItem:=ComboBoxFindFields.ItemIndex;
  vFound:=False;
  try
    vBookmark:=FDataSet.GetBookmark;
    FDataSet.DisableControls;
    while ((not vFound) and (not FDataSet.EOF)) do begin
      FDataSet.Next;
      try
        if FDataSet.FieldByName(ComboBoxFindFields.Items[vSelectedItem]).Value=EditFindValue.Text
          then vFound:=True;
      except
      end;
    end;
  finally
    if vFound=False then begin
      FDataSet.GotoBookmark(vBookmark);
      ShowMessage('Not found.');
    end;
    FDataSet.EnableControls;
    FDataSet.FreeBookmark(vBookmark);
  end;
end;

constructor TFormFind.Create(var pDataset: TZMQueryDataset);
var I:Integer;
begin
  FDataset:=pDataset;
  //Populate ComboBoxFindFields.
  with ComboBoxFindFields do begin
    Clear;
    //Iterate through fields and assign field names to items in the combobox.
    if FDataset.Active=True then
      begin
        for I := 0 to FDataset.Fields.Count - 1 do begin
          ComboBoxFindFields.Items.Add(FDataset.Fields[I].FieldName);
        end;
        ComboBoxFindFields.ItemIndex:=0;
      end;
  end;
  //Open the form FormFilters.
  try
    FormFind.Visible:=True;
  except
    on E:Exception do begin
      ShowMessage('Error while trying to open the form "FormFind". '+ E.Message);
    end;
  end;
end;

initialization
  {$I unit3.ctrs}

end.

