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
Caption | EMPLOYEE TABLE |
Table
Active | True |
DatabaseName | ALIASMYDATA |
TableName | EMPLOYEE |
DataSource
DataSet | Table1 |
Button1
Caption | &First |
Name | First |
Button2
Caption | &Last |
Name | Last |
Button3
Caption | &Previous |
Name | Previous |
Button4
Caption | &Next |
Name | Next |
Button5
Caption | &Add |
Name | Add |
Button6
Caption | &Edit |
Name | Edit |
Button7
Caption | &Save |
Name | Save |
Button8
Caption | &Cancel |
Name | Cancel |
Button9
Caption | &Delete |
Name | Delete |
Button10
Caption | E&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.
0 comments:
Post a Comment