1

I'm trying to make a class that contain the whole query (CRUD) for other units/forms to call and use it. But then I'm facing some errors. I'm using Firebird as database.

I have no idea how to add query to execute inside a function. I get error when I try to input query code in the function.

unit AllClass;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
FireDAC.Stan.Intf, FireDAC.Stan.Option,FireDAC.Stan.Error,FireDAC.UI.Intf, 
FireDAC.Phys.Intf, FireDAC.Stan.Def,FireDAC.Stan.Pool, FireDAC.Stan.Async, 
FireDAC.Phys, FireDAC.Phys.FB,FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait, 
FireDAC.Stan.Param, FireDAC.DatS,FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, 
FireDAC.Comp.DataSet,FireDAC.Comp.Client, Vcl.StdCtrls;

type
 TForm6 = class(TForm)
 FDConnection1: TFDConnection;
 EveryQuery: TFDQuery;
 procedure login(username,password:String);
 private
  { Private declarations }
 public
  { Public declarations }
end;

var
 Form6: TForm6;

implementation

procedure login(username,password:String);
begin
 EveryQuery.SQL.Clear;  //here error
end;
end.

But it will have no error if I input query code inside a button trigger procedure.

procedure TForm1.LogInClick(Sender: TObject);
begin
if (Username.Text <> '') And (Password.Text <> '') then
begin
loginQuery.SQL.Clear;

//use parameter method
loginQuery.SQL.Text := 'Select Password from MYGUESTS where FIRSTNAME = :theID';
loginQuery.ParamByName('theID').AsString := Username.Text;
loginQuery.Open();
passwords := loginQuery.FieldByName('Password').AsString;
if(passwords = Password.Text) then
begin
  with form3 do
  begin
    Show;
    Username.Clear;
    Password.Clear;

    Form1.Hide;
  end;
end
else
begin
  ShowMessage('Wrong username or password. please re-enter');
end;
end
else
begin
  ShowMessage('Please enter something');
end;
end;

Please help me if you have any solutions or idea on this problem.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kai
  • 155
  • 3
  • 9

2 Answers2

2

Your function login should be function TForm6.Login, because it's declared as a method of the form itself. You can avoid this issue by writing the declaration in the form class (in either the private or public section, not where you have it presently) and hitting Ctrl+Shift+C, and the IDE will generate the proper code in the implementation section.

Your current code:

procedure login(username,password:String);
begin
 EveryQuery.SQL.Clear;  //here error
end;

The corrected code:

procedure TForm6.login(username,password:String);
begin
 EveryQuery.SQL.Clear;  //here error
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444
2

Your login() procedure is declared as a member of your TForm6 class, but you are not implementing it as a class member. You need to add TForm6. in front of the login procedure name in the implementation section, eg:

unit AllClass;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, 
  FireDAC.Stan.Intf, FireDAC.Stan.Option,FireDAC.Stan.Error,FireDAC.UI.Intf, 
  FireDAC.Phys.Intf, FireDAC.Stan.Def,FireDAC.Stan.Pool, FireDAC.Stan.Async, 
  FireDAC.Phys, FireDAC.Phys.FB,FireDAC.Phys.FBDef, FireDAC.VCLUI.Wait, 
  FireDAC.Stan.Param, FireDAC.DatS,FireDAC.DApt.Intf, FireDAC.DApt, Data.DB, 
  FireDAC.Comp.DataSet,FireDAC.Comp.Client, Vcl.StdCtrls;

type
  TForm6 = class(TForm)
    FDConnection1: TFDConnection;
    EveryQuery: TFDQuery;
  private
    { Private declarations }
  public
    { Public declarations }
    function login(username, password: String): boolean;
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

function TForm6.login(username, password: String): boolean;
begin
  Result := False;
  if (username <> '') and (password <> '') then
  begin
    //use parameter method
    EveryQuery.Close;
    EveryQuery.SQL.Text := 'Select 1 from MYGUESTS where FIRSTNAME = :theID and Password = :thePsw';
    EveryQuery.ParamByName('theID').AsString := username;
    EveryQuery.ParamByName('thePsw').AsString := password;
    EveryQuery.Open;
    Result := not EveryQuery.Eof;
    EveryQuery.Close;
    if not Result then
      ShowMessage('Wrong username or password. Please re-enter');
  end else
  begin
    ShowMessage('Please enter something');
  end;
end;

end.

Then you can call it like this:

procedure TForm1.LogInClick(Sender: TObject);
begin
  if Form6.login(Username.Text, Password.Text) then
  begin
    Username.Clear;
    Password.Clear;
    Form3.Show;
    Hide;
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770