unit unitStuLogged;

interface

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

type
  TfrmStudentLogged = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    lvStudent: TListView;
    GroupBox1: TGroupBox;
    txtSearch: TEdit;
    btnSearch: TBitBtn;
    btnDel: TBitBtn;
    procedure btnSearchClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
        pKey : Integer;
        procedure studentLogged;
        procedure search;
  end;

var
  frmStudentLogged: TfrmStudentLogged;

implementation

uses DM;

{$R *.dfm}

procedure TfrmStudentLogged.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;

procedure TfrmStudentLogged.studentLogged;
var newState : TListItem;
begin
      if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT a.OCC_ID, b.APPID, c.STUDENT_ID, c.APP_LNAME, c.APP_FNAME, a.DATELOG, a.TIMELOG, d.STATUSDESC, a.NAME, a.ADDRESS, a.CONTACTNO, a.REASON_OVERNYT');
          DataModule1.query.SQL.Add('FROM LOGGED A, OCCUPANTS B,  APPLICANTS C, STATUS D');
          DataModule1.query.SQL.Add('WHERE d.STATUSID = a.STATUSID AND c.APPID = b.APPID and b.OCC_ID = a.OCC_ID');
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          lvStudent.Items.BeginUpdate;
          lvStudent.Items.Clear;

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

         newState := lvStudent.Items.Add;
         newState.Caption := DataModule1.query.Fields.AsString[0];
         newState.SubItems.Add(DataModule1.query.Fields.AsString[1]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[2]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[3]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[4]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[5]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[6]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[7]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[8]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[9]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[10]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[11]);
         DataModule1.query.Next;
end;
            if DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              lvStudent.Items.EndUpdate;
end;

procedure TfrmStudentLogged.FormShow(Sender: TObject);
begin
        studentLogged;
end;

procedure TfrmStudentLogged.search;
var newState : TListItem;
begin
      if DataModule1.tr.InTransaction then
          DataModule1.tr.Commit;

          DataModule1.query.SQL.Clear;
          DataModule1.query.SQL.Add('SELECT a.OCC_ID, b.APPID, c.STUDENT_ID, c.APP_LNAME, c.APP_FNAME, a.DATELOG, a.TIMELOG, d.STATUSDESC, a.NAME, a.ADDRESS, a.CONTACTNO, a.REASON_OVERNYT');
          DataModule1.query.SQL.Add('FROM LOGGED A, OCCUPANTS B,  APPLICANTS C, STATUS D');
          DataModule1.query.SQL.Add('WHERE d.STATUSID = a.STATUSID AND c.APPID = b.APPID and b.OCC_ID = a.OCC_ID AND c.STUDENT_ID = :A OR c.APP_LNAME LIKE :B OR c.APP_FNAME LIKE :C');
          DataModule1.query.Params.AsString[0] := Trim(txtSearch.Text);
          DataModule1.query.Params.AsString[1] := Trim(txtSearch.Text);
          DataModule1.query.Params.AsString[2] := Trim(txtSearch.Text);
          DataModule1.query.Prepare;
          DataModule1.query.Open();
          lvStudent.Items.BeginUpdate;
          lvStudent.Items.Clear;

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

         newState := lvStudent.Items.Add;
         newState.Caption := DataModule1.query.Fields.AsString[0];
         newState.SubItems.Add(DataModule1.query.Fields.AsString[1]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[2]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[3]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[4]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[5]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[6]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[7]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[8]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[9]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[10]);
         newState.SubItems.Add(DataModule1.query.Fields.AsString[11]);
         DataModule1.query.Next;
end;
            if DataModule1.tr.InTransaction then
              DataModule1.tr.Commit;
              lvStudent.Items.EndUpdate;
end;


end.
