Delphi tutorial MS Access database with KADao

Delphi tutorial MS Access database with KADao

This is Delphi Tutorial about creating and manipulating MS Access database
with KADAo component ( http://www.kadao.dir.bg/ ) ,
and as usual you can find here source code for this app,
and also exe files at address : https://drive.google.com/open?id=0B_njK7HczCjDWkdtbTVseExyYW8




Here is the video :



Source code for this app example :

(code style formatted by http://hilite.me/ )



unit MainForm

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, KDaoTable, KDaoDataBase, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
lbTblName: TLabel;
Label2: TLabel;
ComboBox1: TComboBox;
Edit1: TEdit;
Label3: TLabel;
Label4: TLabel;
Edit2: TEdit;
Label5: TLabel;
Edit3: TEdit;
Button5: TButton;
Button6: TButton;
Database1: TKADaoDatabase;
Table1: TKADaoTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button7: TButton;
lbDbName: TLabel;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
SaveDialog1.InitialDir:=ExtractFilePath(Application.ExeName) ;
if SaveDialog1.Execute() then
begin

Database1.Close;
Database1.CreateAccessDatabase(SaveDialog1.FileName+'.mdb');
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
TM:TKADaoTableManager;
begin
if Database1.Connected=False then Exit;
try
Database1.Connected:=true;
TM:=TKADaoTableManager.Create(Database1);
TM.TableName:=InputBox('Insert table name','Table name','Table 1');
TM.FieldDefs.Add('Field 1',ftInteger,0,False);
TM.FieldDefs.Add('Field 2',ftString,100,False);
TM.FieldDefs.Add('Field 3',ftDate,0,False);
TM.IndexDefs.Add('Field 1','Field 1',[ixPrimary,ixUnique]);
TM.IndexDefs.Add('Field 2','Field 2',[]);
TM.CreateTable;
lbTblName.Caption:=TM.TableName;
TM.Free;

Database1.Connected:=False;
Database1.Open;
ComboBox1.Items.Clear;
ComboBox1.Items.AddStrings(Database1.TableNames);
ComboBox1.ItemIndex:=0;


except
ShowMessage('Error creating table');
end;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
if OpenDialog1.Execute() then
begin
OpenDialog1.InitialDir:=ExtractFilePath(Application.ExeName);
Database1.Close;
Database1.Database:=OpenDialog1.FileName;
Database1.Open;
lbDbName.Caption:=Database1.Database;
ComboBox1.Items.Clear;
ComboBox1.Items.AddStrings(Database1.TableNames);
ComboBox1.ItemIndex:=0;
end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
if (Database1.Connected=True) and ( ComboBox1.Text<>'') then
begin
Table1.Close;
Table1.TableName:=ComboBox1.Text;
Table1.Open;

DBGrid1.Columns[0].Width:=100;
DBGrid1.Columns[1].Width:=200;
DBGrid1.Columns[2].Width:=100;

end;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
if (Edit1.Text<>'') then begin

Table1.Insert;
Table1.Append;
Table1.FieldByName('Field 1').AsInteger:=StrToInt(Edit1.Text);
Table1.FieldByName('Field 2').AsString:=Edit2.Text;
Table1.FieldByName('Field 3').AsDateTime:=StrToDate(Edit3.Text);
Table1.Post;

end;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
Table1.Edit;
Table1.UpdateRecord;
Table1.Post;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
Table1.Delete;
end;

end.
Share on Google Plus

About Kn Kn

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.