unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, db, dbf, FileUtil, Forms, Controls, Graphics, Dialogs, DbCtrls, DBGrids, StdCtrls, ExtCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; DataSource1: TDataSource; DBEdit1: TDBEdit; DBEdit2: TDBEdit; DBEdit3: TDBEdit; DBEdit4: TDBEdit; DBEdit5: TDBEdit; DBEdit6: TDBEdit; DBEdit7: TDBEdit; DBEdit8: TDBEdit; DBGrid1: TDBGrid; DBImage1: TDBImage; DBMemo1: TDBMemo; DBNavigator1: TDBNavigator; Image1: TImage; Panel1: TPanel; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure DBImage1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private ADbf: TDbf; procedure set_controls(); public end; var Form1: TForm1; const ///Database filename. DataFile = 'AddressBook.dbf'; implementation {$R *.lfm} uses clipbrd; ///This is so we can use the clipboard is are program. { TForm1 } //////////////// USER FUNCTIONS /////////////////////////// ///Set DBEdit1,DBEdit2, DBMemo1,and /// DBImage1 DataSource to DataSource1. /// This is the order thay apear on the DBGrid. procedure TForm1.set_controls(); begin DBEdit1.DataSource:=DataSource1; DBEdit1.DataField:='First Name'; DBEdit2.DataSource:=DataSource1; DBEdit2.DataField:='Inc'; DBEdit3.DataSource:=DataSource1; DBEdit3.DataField:='Last Name'; DBEdit4.DataSource:=DataSource1; DBEdit4.DataField:='Phone Number'; DBEdit5.DataSource:=DataSource1; DBEdit5.DataField:='Address'; DBEdit6.DataSource:=DataSource1; DBEdit6.DataField:='City'; DBEdit7.DataSource:=DataSource1; DBEdit7.DataField:='State'; DBEdit8.DataSource:=DataSource1; DBEdit8.DataField:='Zipcode'; DBMemo1.DataSource:=DataSource1; DBMemo1.DataField:='Memo'; DBImage1.DataSource:=DataSource1; DBImage1.DataField:='Image'; end; /////////////////// FORM FUNCTIONS //////////////////////// ///Exit button. procedure TForm1.Button4Click(Sender: TObject); begin Form1.Close; end; ///Add record button. procedure TForm1.Button1Click(Sender: TObject); var empty: Boolean; num: Integer; begin ///If you try to post a record and the record is not /// empty. Program will crash. ///Is the ID field empty? empty := ADbf.FieldByName('ID').IsNull; ///Is the blank filled? if(Length(DBEdit1.Text) < 2) or (Length(DBEdit2.Text) < 1) or (DBMemo1.Lines.Count <> 0) then else begin ///If not say so and exit. ShowMessage('Some fields empty!'); // Exit; end; ///Post the record, and add a new record. DBNavigator1.BtnClick(nbPost); ///Post to database. DBNavigator1.BtnClick(nbInsert); ///New roll. end; ///New Record button. procedure TForm1.Button2Click(Sender: TObject); begin DBNavigator1.BtnClick(nbInsert); ///New roll. end; ///Delete button. procedure TForm1.Button3Click(Sender: TObject); begin DBNavigator1.BtnClick(nbDelete); ///Delete. end; ///DubleClick DBImage. procedure TForm1.DBImage1Click(Sender: TObject); begin ///Clipboard to TImage. Image1.Picture.Assign(Clipboard); ///DbImage does store headers. dbImage1.WriteHeader:=True; ///TImage to DbImage. dbimage1.Picture.Assign(Image1.Picture); end; ///OnCreate(). procedure TForm1.FormCreate(Sender: TObject); begin ///Define the Database file, /// and creates an empty object. ADbf:=TDbf.Create(Self); ///If the database file(s) does not exist, Create it. if not FileExists(DataFile) then with ADbf do begin ///Defines the database filename. ADbf.TableName:=DataFile; TableLevel:=7; ///DBase 7. ///Are program has exclusive access to the table. Exclusive:=True; ///Adds the columns. FieldDefs.Add('ID', ftAutoInc, 4, True); FieldDefs.Add('First Name', ftString, 35, True); FieldDefs.Add('Inc', ftString, 3, True); FieldDefs.Add('Last Name', ftString, 35, True); FieldDefs.Add('Phone Number', ftString, 14, True); FieldDefs.Add('Address', ftString, 38, True); FieldDefs.Add('City', ftString, 30, True); FieldDefs.Add('State', ftString, 5, True); FieldDefs.Add('Zipcode', ftString, 14, True); FieldDefs.Add('Memo', ftMemo, 6, True); FieldDefs.Add('Image', ftBlob, 6, True); ///Create the Database file. CreateTable; ///Set the data component. set_controls(); end; ///End of if not FileExistsUTF8(DataFile) then(). ///Defines the database filename. ADbf.TableName:=DataFile; ///Has exclusive access to the table. ADbf.Exclusive:=True; ///Set DataSource1 DataSet to ADbf. DataSource1.DataSet:=ADbf; ///Set DBGrid1 and DBNavigator1 DataSource to DataSource1. DBGrid1.DataSource:=DataSource1; DBNavigator1.DataSource:=DataSource1; ///Opens the table. ADbf.Open; ///Set the data component. set_controls(); end; ///OnDestory(). procedure TForm1.FormDestroy(Sender: TObject); begin ADbf.Close; ADbf.Free; end; end.