Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help to create buttons - beginner

Status
Not open for further replies.

havsys

Programmer
Jul 23, 2012
5
RS
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.
 
I don't have Delphi open so this might not be exactly perfect:

If you drop a TActions item on to your form and then double-click on it you can then right-click in the actions editor and automatically insert a bunch of database related actions. Connect the newly created actions to your datasource and then link your buttons to the actions and Delphi will take care of the rest.

 
- For example your table has foloowing fields (Tabel name is Music) :
Title_OF_CD , Artist , Genre & Price

For your 'Insert Button'
you want to insert a new record
you would do the following :

Insert Into Music 'Believe','Justin Bieber','Pop','$10';

it follows this principle
- Insert Into <Table> <Field1(Titel_OF_CD)>,
<Field2(Artists)>,<Field3(Genre)>,<Field4(Price)>;
your data should be inserted according to your fields, so whatever field is first's data should be specified first.

For your 'Update Button'
you want to change the artist name of a certain CD
you would do the following :

Update Music Set Artist = 'Justin Bieber' Where Title_OF_CD = 'Believe';

it follows this principle
-Updat <Table> Set <Field> = <Value> Where <Criteria>
If you want to update more than one field
-Updat <Table> Set <Field> = <Value>, <Field> = <Value>
Where <Criteria>

For you 'Delete Button'
You want to delete all records where the Atrist is Eminem
you would do the following :

Delete From Music Where Artist = 'Eminem';

it would follow this principle
- Delete From <Table> Where <Field> = <Criteris>;
If you want to be more precise
- Delete From <Table> Where <Field> = <Criteria> and <Field> = <Criteria> and...

As for your Save and clear I'm not sure myself as far as I know what ever you insert or delete is automaticly saved, and clear to me is same as delete.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top