Login Form Delphi by Connecting Ms Access Database.
In making/ building application in Delphi, login Form is an important to protect application so that cannot be open by irresponsible person.
This login Form is connected with database so that able to keep a lot of username data and password.
Before make the application, please create a database as the picture below:
Picture :Login form Delphi database Microsoft Access
Save the database in the folder which use to save the application
Next step is create the application
Enter the component and the source code as follows.
No | Component Name | Properties | Value |
1 | Label1 | Caption | Username |
2 | Label2 | Caption | Password |
3 | Edit1 | Text | - |
4 | Edit2 | Text | - |
5 | Button1 | Caption | Login |
6 | Adoconnection1 | - | - |
7 | AdoQuery1 | Connection | ADOConnection1 |
Arrange the component as the picture below:
Figure. Login Form Delphi design form
Create new form
Form properties can be check in the table below
No | Component Name | Properties | Value |
1 | Form1 | Caption | Success Login |
Name | Hasil_Login |
Type the source code Below
No | Component Name | Event | Source code |
1 | Form1 | OnCreate | var s : Char; dbaddress : WideString; begin s := '*'; edusername.Text := ''; edpassword.PasswordChar := s; edpassword.Text := ''; dbaddress := ExtractFilePath(Application.ExeName) + 'data.mdb'; with ADOConnection1 do begin Connected := False; LoginPrompt := False; Mode := cmShareDenyNone; ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + alamatdb + ';Persist Security Info=False'; end; ADOConnection1.Connected := True; end; |
2 | Button1 | OnClick | with ADOQuery1 do begin Close; SQL.Clear; // bersihkan perintah sql jika ada SQL.Add('select * from login where username='+ QuotedStr(edusername.Text)); Open; end; // end with // jika tidak ditemukan data yang dicari maka // tampilkan pesan if ADOQuery1.RecordCount = 0 then Application.MessageBox('wrong username!!!!!!', 'Information', MB_OK or MB_ICONINFORMATION) else begin if ADOQuery1.FieldByName('password').AsString <> edpassword.Text then Application.MessageBox('Be sure user name and password is correct', 'Error', MB_OK or MB_ICONERROR) else begin Hide; Hasil_Login.Show; end end; |
This is the Login Form Application:
When wrong username
Picture :Login form Delphi wrong username
When Wrong Password
Picture :Login form Delphi wrong Password
All the source code is appear below:
unit ULogin;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, ExtCtrls;
type
TFrmLogin = class(TForm)
Label1: TLabel;
edusername: TEdit;
Label2: TLabel;
edpassword: TEdit;
Button1: TButton;
ADOQuery1: TADOQuery;
ADOConnection1: TADOConnection;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmLogin: TFrmLogin;
implementation
uses
XPMan, UHasilLogin, Unit2;
{$R *.dfm}
procedure TFrmLogin.FormCreate(Sender: TObject);
var
s : Char;
dbaddress : WideString;
begin
s := '*';
edusername.Text := '';
edpassword.PasswordChar := s;
edpassword.Text := '';
dbaddress := ExtractFilePath(Application.ExeName) + 'data.mdb';
with ADOConnection1 do begin
Connected := False;
LoginPrompt := False;
Mode := cmShareDenyNone;
ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' +
alamatdb + ';Persist Security Info=False';
end;
ADOConnection1.Connected := True;
end;
procedure TFrmLogin.Button1Click(Sender: TObject);
begin
with ADOQuery1 do begin
Close;
SQL.Clear; SQL.Add('select * from login where username='+
QuotedStr(edusername.Text));
Open;
end;
if ADOQuery1.RecordCount = 0 then
Application.MessageBox('wrong username!!!', 'Information',
MB_OK or MB_ICONINFORMATION)
else begin
if ADOQuery1.FieldByName('password').AsString <> edpassword.Text
then Application.MessageBox('Be sure user name and password is correct', 'Error',
MB_OK or MB_ICONERROR)
else begin
Hide;
Hasil_Login.Show;
end
end;
end;
end.
I hope this article can give benefit to the visitors, readers, developer and me. Thanks for visiting and availability for sharing this article by pressing
Share this on Facebook |