Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help To Create Buttons - Beginner

delphi

  • Please log in to reply
3 replies to this topic

#1 havsys

havsys

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Programming Language:C#, Delphi/Object Pascal
  • Learning:C#, Delphi/Object Pascal

Posted 24 July 2012 - 04:27 AM

Hello, Im a beginner and i need help with my problem.
How to create insert/save/update/delete and clear Fields(DBEdit) buttons for mysql database using delphi.

This is my example.

First I create mysql database:
create database Contacts;

create table coktacts(
id int auto_increment primary key not null,
First_name varchar(25) not null,
Last_name varchar(25) not null,
Address varchar(50) not null,
Country varchar(25) not null,
Phone varchar(13));


Then I create application in delphi.

File -> New -> VLC Forms Application

In Forms I Use:

DBGrid
5 buttons (insert/save/Update/delete/clear)
AdoConnection
AdoDataset
Data Source

then set my AdoConnection:
- ConnectionString: Find my source to connection ...
- LoginPrompt: false
- Connected: True
- Name: AdoConnection1

Then set AdoDataSet:
Connection: AdoConnection1
Name: AdoDataSet1
CommandType: cmdTable
CommandText: Contacts
for dataset I use Field Editor-add fields-Drag and Drop to Form.

Then set DAtaSource:
DataSet: AdoDataSet1
Name: DataSource1

Then set DBGrid:
DataSource: DataSource1
Columns:(TDBGridColumns): id, First Name, Last Name, Address, Country, Phone.

For Show my database into DBgrid I use Code:
for procedure TForm3.FormCreate(Sender: TObject);
begin
ADODataSet1.Active := true;
end;

Question 1: how to create insert/save/update/delete/clear buttons?
I dont want to use DBnavigator.

When I click INSERT button I want to clear DBEdits fiels(Firstname,lastname,address ...) input new contact info and SAVE to my database.

form.jpg
  • 0

#2 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 25 July 2012 - 12:43 AM

Hi havsys, welcome to CodeCall!

Before proceed, we must clear about terms. First you must know about DB-Aware controls. Most controls you were using in the project are db-aware controls. They are: the TDBGrid and those TDBEdit-s. DB-Aware controls do not actually have their own value to show or to change. Instead they got their values to show or to edit from underlying dataset.

For example, in your project the TDBGrid actually does not keep the tabular data like shown in the image. The data is contained in AdoDataset1 connected to it through a TDataSource. Same goes with those TDBEdits. They just show value of the field assigned to them of the currently active record of AdoDataset1.

So to "clear" those DBEdits, you need to clear the values of underlying fields. E.g. to "clear" existing active record you might get away with the following codes. Note that in some situations the codes would fail.

procedure TForm1.ClearDBEdits;
var
  i: Integer;
  vField: TField;
begin
  for i := 0 to AdoDataset1.FieldCount-1 do
  begin
    vField := AdoDataset1.Fields[i];
    vField.Clear;  // give NULL value to the field 
  end;
end;

I will continue if you understand this term.
  • 0

#3 havsys

havsys

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Programming Language:C#, Delphi/Object Pascal
  • Learning:C#, Delphi/Object Pascal

Posted 25 July 2012 - 01:19 AM

Thank you LuthfiHakim, your post is very helpful for me :)

can I ask you one more question?

I want to disable TBDGrids for Insert, In DbGrids I just want to see my data.
For insert I want to use only DBEdits. In your first reply to me, you explained to me how to insert and save to my database.

I find on internet that I need to use ADODataSet1 --> Event --> Before Insert

procedure TForm3.ADODataSet1BeforeInsert(DataSet: TDataSet);
begin

end;

Thank you.
  • 0

#4 Luthfi

Luthfi

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1320 posts
  • Programming Language:PHP, Delphi/Object Pascal, Pascal, Transact-SQL
  • Learning:C, Java, PHP

Posted 25 July 2012 - 02:56 AM

Hi havsys,

Glad if I can be of any help.

I want to disable TBDGrids for Insert, In DbGrids I just want to see my data.


If you don't want to do any editing with the TDBGrid, just set its ReadOnly property to True.

For insert I want to use only DBEdits. In your first reply to me, you explained to me how to insert and save to my database.


My reply in other forum, right? :) Actually I am most active here.

I find on internet that I need to use ADODataSet1 --> Event --> Before Insert

procedure TForm3.ADODataSet1BeforeInsert(DataSet: TDataSet);
begin

end;


Looked to me that you have forgotten to paste the actual code.

Thank you.


You're welcome. Oh, next time please wrap any codes inside CODE tag. Just like I have done with my sample codes. To do that, just select the codes and click on button with icon looks like this: <>
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download