July 24th, 2012, 07:43 AM
Help to create buttons - beginner
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,
Then I create application in delphi.
File -> New -> VLC Forms Application
In Forms I Use:
5 buttons (insert/save/Update/delete/clear)
then set my AdoConnection:
- ConnectionString: Find my source to connection ...
- LoginPrompt: false
- Connected: True
- Name: AdoConnection1
Then set AdoDataSet:
for dataset I use Field Editor-add fields-Drag and Drop to Form.
Then set DAtaSource:
Then set DBGrid:
ColumnsTDBGridColumns): id, First Name, Last Name, Address, Country, Phone.
For Show my database into DBgrid I use Code:
for procedure TForm3.FormCreate(Sender: TObject);
ADODataSet1.Active := true;
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.
July 24th, 2012, 11:54 PM
Basically just place a button (either TButton, TBitBtn, or TSpeedButton - I recommend TBitBtn) for each operation to your form then put appropriates codes in their OnClick events. Like for example I show for your next question.
Originally Posted by havsys
When a new record is appended to a dataset, its fields will be initialized to default values predefined for the dataset. If no default value defined for a field, it will be given the value of null (for now you can consider this value similar with empty). Therefore if the code in your INSERT button OnClick event adds new record to a dataset, its current record will contain fields with either default values or empty.
If there is no default values defined, then all the fields will be empty, which looks like the same with clearing the TDBEdit controls. So assuming your INSERT button is named btnInsert, you can use the following code in its OnClick event handler.
Note that Append does not automatically save the new record into database. If you do not change anything in your new record and you change your active record, the new record will be lost (deleted).
procedure TForm1.btnInsertClick(Sender: TObject);
to save the new record, you can put the following code for your SAVE button.
procedure TForm1.btnSaveClick(Sender: TObject);
July 25th, 2012, 04:20 AM
Thank you Luthfi, your post is very helpful for me
July 25th, 2012, 08:23 AM
One of the nice things about the DBNavigator control is that it will enable/disable buttons as appropriate depending on the current state of the datasource. So if you are just browsing your records, the post button is not enabled, but is when you start making any changes to a record. Adding a new record, will disable the new record button, but will enable your cancel and post buttons.
They all work in unison, so just attaching the event handler for each button is only half the story. You would also need to add code to enable/disable those buttons as the state of your datasource changes.
These actions are appropriate to be coded in the DataSource's OnStateChange event, something like:
The valid states a datasource can be in is:
procedure TForm1.DataSource1StateChange(Sender: TObject);
PostButton.Enabled := DataSource1.State in [dsInsert, dsEdit];
NextButton.Enabled := DataSource1.State in ...
dsNewValue - Internal use only
dsOldValue - Internal use only
dsCurValue - Internal use only
dsInternalCalc - Internal use only
July 27th, 2012, 06:02 AM
Choose Edit > Deselect All, or click an empty area of the Stage to ensure that nothing is selected on the Stage.
Choose Insert > New Symbol.
In the Create New Symbol dialog box, enter a name. For the symbol Type, select Button.
Flash Pro switches to symbol-editing mode. The Timeline changes to display four consecutive frames labeled Up, Over, Down, and Hit. The first frame, Up, is a blank keyframe.
To create the Up state button image, select the Up frame in the Timeline. Then use the drawing tools, import a graphic, or place an instance of another symbol on the Stage.
You can use graphic symbols or movie clip symbols inside a button, but you cannot use another button symbol.