Thursday 4 March 2010

Delphi - Interbase Tutorial # 3 : Create a user interface for manipulating table without DBNavigator

In the previous tutorial we have created a tiny user interface for manipulating table. Now we’re going to create a user interface without using a DBNavigator. If there is no DBNavigator, how do we manipulate our table ?. We’re going to use some buttons and codes to manipulate our table. Let’s see our blank form, add some components into it : a DBGrid, a Table, a DataSource, ten Buttons (Button1……Button10)

Set the properties of our components :

Form

CaptionEMPLOYEE TABLE

Table

ActiveTrue
DatabaseNameALIASMYDATA
TableNameEMPLOYEE

DataSource

DataSetTable1

Button1

Caption&First
NameFirst

Button2

Caption&Last
NameLast

Button3

Caption&Previous
NamePrevious

Button4

Caption&Next
NameNext

Button5

Caption&Add
NameAdd

Button6

Caption&Edit
NameEdit

Button7

Caption&Save
NameSave

Button8

Caption&Cancel
NameCancel

Button9

Caption&Delete
NameDelete

Button10

CaptionE&xit
Name

Exit

Now our form appears like the picture below :

Type the code for the OnClick event of the buttons :

procedure TForm1.ExitClick(Sender: TObject);
begin
 Application.Terminate;
end;

procedure TForm1.FirstClick(Sender: TObject);
begin
 Table1.First;{go to the first record}
end;

procedure TForm1.LastClick(Sender: TObject);
begin
 Table1.Last;{go to the last record}
end;

procedure TForm1.PreviousClick(Sender: TObject);
begin
 Table1.Prior;{go to the previous record}
end;

procedure TForm1.NextClick(Sender: TObject);
begin
 Table1.Next;{go to the next record}
end;

procedure TForm1.AddClick(Sender: TObject);
begin
 Table1.Append;{insert new record }
end;

procedure TForm1.EditClick(Sender: TObject);
begin
 Table1.Edit;{edit existing record }
end;

procedure TForm1.SaveClick(Sender: TObject);
begin
 Table1.Post;{save record }
end;

procedure TForm1.CancelClick(Sender: TObject);
begin
 Table1.Cancel;{cancel record }
end;

procedure TForm1.DeleteClick(Sender: TObject);
begin
 Table1.Delete;{delete record }
end;

To insert data we can use Table1.Append or Table1.Insert procedure. Find out yourself, the different between these two procedures.

Run the program. You can navigate and manipulating employee table by clicking the button. Notice that you can not delete any record in ‘employee’ table, because there another table that refers to ‘id’ column value of table employee. If you try to click Delete button, an error message will be appear :

In the next tutorial I will show you how to handle this exception.

Go to Previous Tutorial or Next Tutorial

0 comments:

Post a Comment

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.