#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0

    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,
    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
    ColumnsTDBGridColumns): 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    156
    Rep Power
    3
    Originally Posted by havsys
    Question 1: how to create insert/save/update/delete/clear buttons?
    I dont want to use DBnavigator.
    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.

    When I click INSERT button I want to clear DBEdits fiels(Firstname,lastname,address ...) input new contact info and SAVE to my database.
    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.

    Code:
    procedure TForm1.btnInsertClick(Sender: TObject);
    begin
      AdoDataSet1.Append;
    end;
    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).

    to save the new record, you can put the following code for your SAVE button.

    Code:
    procedure TForm1.btnSaveClick(Sender: TObject);
    begin
      AdoDataSet1.Post;
    end;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    12
    Rep Power
    0
    Thank you Luthfi, your post is very helpful for me
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    355
    Rep Power
    7
    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:
    Code:
    procedure TForm1.DataSource1StateChange(Sender: TObject);
    begin
       PostButton.Enabled := DataSource1.State in [dsInsert, dsEdit];
       NextButton.Enabled := DataSource1.State in ...
    end;
    The valid states a datasource can be in is:

    dsInactive
    dsBrowse
    dsEdit
    dsInsert
    dsSetKey
    dsCalcFields
    dsFilter
    dsNewValue - Internal use only
    dsOldValue - Internal use only
    dsCurValue - Internal use only
    dsBlockRead
    dsInternalCalc - Internal use only
    dsOpening
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Creating buttons


    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.


    Regards,
    Selva.

IMN logo majestic logo threadwatch logo seochat tools logo