unit unitUserList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls, Buttons, ComCtrls;

type
  TfrmUser = class(TForm)
    lvAccnts: TListView;
    GroupBox1: TGroupBox;
    txtSearch: TEdit;
    btnSearch: TBitBtn;
    PopupMenu1: TPopupMenu;
    ChangePassword1: TMenuItem;
    procedure ChangePassword1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnSearchClick(Sender: TObject);
  private
    { Private declarations }
  public
        procedure accounts;
        procedure search;
  end;

var
  frmUser: TfrmUser;

implementation

uses DM, unitChangePass;

{$R *.dfm}

procedure TfrmUser.ChangePassword1Click(Sender: TObject);
begin
       frmChangePass.ShowModal;
end;

procedure TfrmUser.FormShow(Sender: TObject);
begin
       accounts;
end;

procedure TfrmUser.accounts;
 var accnts : TListItem;
begin
        if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

        DataModule1.tr.StartTransaction;
        DataModule1.query.SQL.Clear;

     DataModule1.query.SQL.Add('SELECT USERID,LASTNAME, FIRSTNAME, USERNAME, USERACCESS');
     DataModule1.query.SQL.Add(' FROM USER_ACCOUNTS');
     DataModule1.query.Prepare;
     DataModule1.query.Open();
     lvAccnts.Items.BeginUpdate;
     lvAccnts.Items.Clear;

     if DataModule1.query.Fields.IsNull[0] then Exit;
     while not DataModule1.query.Eof do begin

         accnts:= lvAccnts.Items.Add;
         accnts.Caption := DataModule1.query.Fields.AsString[0];
         accnts.SubItems.Add(DataModule1.query.Fields.AsString[1]);
         accnts.SubItems.Add(DataModule1.query.Fields.AsString[2]);
         accnts.SubItems.Add(DataModule1.query.Fields.AsString[3]);
          accnts.SubItems.Add(DataModule1.query.Fields.AsString[4]);

         DataModule1.query.Next;
end;
        if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;
        lvAccnts.Items.EndUpdate;
end;

procedure TfrmUser.search;
var accnts : TListItem;
begin
      if DataModule1.tr.InTransaction then
        DataModule1.tr.Commit;

        DataModule1.query.SQL.Clear;
        DataModule1.query.SQL.Add('SELECT USERID,LASTNAME, FIRSTNAME, USERNAME, USERACCESS FROM USER_ACCOUNTS where LASTNAME LIKE :A OR FIRSTNAME LIKE :B');
        DataModule1.query.Params.AsString[0] := txtSearch.Text;
        DataModule1.query.Params.AsString[1] := txtSearch.Text;
        DataModule1.query.Prepare;
        DataModule1.query.Open();
        lvAccnts.Items.BeginUpdate;
        lvAccnts.Items.Clear;

        if DataModule1.query.Fields.IsNull[0] then Exit;
            while not DataModule1.query.Eof do begin

            accnts:= lvAccnts.Items.Add;
            accnts.Caption := DataModule1.query.Fields.AsString[0];
            accnts.SubItems.Add(DataModule1.query.Fields.AsString[1]);
            accnts.SubItems.Add(DataModule1.query.Fields.AsString[2]);
            accnts.SubItems.Add(DataModule1.query.Fields.AsString[3]);
            accnts.SubItems.Add(DataModule1.query.Fields.AsString[4]);
            DataModule1.query.Next;
            end;

            if DataModule1.tr.InTransaction then
                DataModule1.tr.Commit;
                lvAccnts.Items.EndUpdate;
end;

procedure TfrmUser.btnSearchClick(Sender: TObject);
begin
      if Length(txtSearch.Text) > 0 then
              begin
                    search;
              end
                else  begin
                    MessageDlg('Please input first the textbox when searching.',mtInformation,[mbOK],0);
end;
end;
end.
