Delphi-Kylix
  Home arrow Delphi-Kylix arrow Building Contact Detail Maintenance into a...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Building Contact Detail Maintenance into a Mailing List Program with Borland Delphi
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2006-10-04

    Table of Contents:
  • Building Contact Detail Maintenance into a Mailing List Program with Borland Delphi
  • Breaking Down the Code
  • Creating a New Group
  • The Less Complicated Code Bits

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Building Contact Detail Maintenance into a Mailing List Program with Borland Delphi


    (Page 1 of 4 )

    A mailing list would not be of much use if you cannot add and remove contact addresses. Therefore, in this second part of a two-part tutorial on building a mailing list application using Borland Delphi, we are going to add the capability of maintaining contact details.
    A downloadable file for this article is available here.

    Add two more forms to the project and save the units as new_contact and addresses. On the new contacts form, drop three edits (rename them edname, edemail and ednewgroup), two radio buttons (rename them rb1 and rb2), two statictext (name and email), two buttons (rename to btnAddContact and btnCancel) and a combobox (rename to cb). This is how the form should look:

    On the form's onshow event, add the following code:

    procedure TForm2.FormShow(Sender: TObject);
    begin
     cb.Visible:=false;
     ednewgroup.Visible:=false;
    end;

    Because we only take the contact's name and email address, the form provides us with text fields that will take that information. The next step is to decide whether you want to add this contact to a group. To make the choices clearer, I've added two radio buttons, one asking if you want to add the new contact to a group and the other if you want to create a new group. You can choose one or the other but not both.

    If you choose to add the contact to a group, a drop down box with the various group names in it will  appear. All you have to do is click on that name and then click on the "Add Contact" button. Below is the code for the Add Contact button:

    procedure TForm2.btnAddContactClick(Sender: TObject);
    var
    gname:string;
    new_id:integer;
    grpid,newgrp:boolean;
    begin
    new_id:=0;
    if (edname.text = '') or (edemail.Text = '') then begin
    showmessage('Please enter a name and email address');
    exit;
    end;
    //purpose: to get the groupID of the selected groupname
    if rb1.Checked then begin
     if cb.Text <> '' then begin
     gname:=cb.Text;
    //search for the groupID of the selected groupname 
     form1.query1.Close;
     form1.query1.SQL.Text:='SELECT groupID from groups WHERE
    groupname=:thename';
     form1.query1.Parameters.ParamByName('thename').Value:=gname;
     form1.query1.Open;
    //Check if the group id is greater than 0:
      if form1.query1.FieldByName('groupID').AsInteger > 0  then
      begin
      new_id:= form1.query1.FieldByName('groupID').AsInteger;
      grpid:=true;
      //Now insert the data into the database
      form1.query1.Close;
      form1.query1.SQL.Text:='INSERT INTO contacts (name,email,gid)
    values (:aname,:aemail,:agid)';
      form1.query1.Parameters.ParamByName('aname').Value:=edname.Text;
      form1.query1.Parameters.ParamByName('aemail').Value:=edemail.text;
      form1.query1.Parameters.ParamByName('agid').Value:=new_id;
      form1.query1.ExecSQL;
      showmessage('New contact has been added');
        end
      else
      begin
      //the record with the selected name has not been found, set the
    groupID to zero
      new_id:=0;
      //Now insert the data into the database
      form1.query1.Close;
      form1.query1.SQL.Text:='INSERT INTO contacts (name,email,gid)
    values (:aname,:aemail,:agid)';
      form1.query1.Parameters.ParamByName
    ('aname').Value:=edname.Text;
      form1.query1.Parameters.ParamByName
    ('aemail').Value:=edemail.text;
      form1.query1.Parameters.ParamByName('agid').Value:=new_id;
      form1.query1.ExecSQL;
      showmessage('New contact has been added');
      grpid:=false;
      end;
      end
      else
      begin
    //User did not select a name...
      showmessage('Please select a group name to add the new contact
    to.'+#13#10+ 'If you do not see the group name on the list then
    create a new group name by selecting the ''Create a New Group ''
    option below.');
      exit;
      end;
      end;//end main IF
      if rb2.Checked then
      begin
    //check that the new name is entered
      if ednewgroup.Text <> '' then begin
    //add the new groupname to the groups table and get its ID
      form1.query1.Close;
      form1.query1.SQL.Text:='INSERT INTO groups (groupname) values
    (:edval)';
      form1.query1.Parameters.ParamByName('edval').Value:=ednewgroup.Text;
      form1.query1.ExecSQL;
      //now get the group id
      form1.query1.close;
      form1.query1.SQL.Text:='SELECT groupID from groups WHERE
    groupname=:thename';
      form1.query1.Parameters.ParamByName
    ('thename').Value:=form2.ednewgroup.Text;
      form1.query1.Open;
      new_id:=form1.query1.fieldbyname('groupID').AsInteger;
      //insert the new groupname..
      form1.query1.Close;
      form1.query1.SQL.Text:='INSERT INTO contacts (name,email,gid)
    values (:aname,:aemail,:agid)';
      form1.query1.Parameters.ParamByName
    ('aname').Value:=edname.Text;
      form1.query1.Parameters.ParamByName
    ('aemail').Value:=edemail.text;
      form1.query1.Parameters.ParamByName('agid').Value:=new_id;
      form1.query1.ExecSQL;
      showmessage('Both the new group: '+ednewgroup.Text+ ' and
    contact: '+inttostr(new_id)+' has been added.');
      end
      else
      begin
      showmessage('Please enter a groupname.');
      exit;
      end;
      end;
      if not (rb1.Checked) and not (rb2.Checked) then
      begin
      form1.query1.Close;
      form1.query1.SQL.Text:='INSERT INTO contacts (name,email,gid)
    values (:aname,:aemail,:agid)';
      form1.query1.Parameters.ParamByName
    ('aname').Value:=edname.Text;
      form1.query1.Parameters.ParamByName
    ('aemail').Value:=edemail.text;
      form1.query1.Parameters.ParamByName('agid').Value:=0;
      form1.query1.ExecSQL;
      showmessage('New contact has been added.');
      end;
      end;

    More Delphi-Kylix Articles
    More By Leidago


       · Please make sure that you use the latest version of Indy when compiling the sample...
     

    DELPHI-KYLIX ARTICLES

    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...
    - Using the Client Dataset in File-Based Archi...
    - Demystifying the Client Dataset
    - Working with INI Files in Delphi
    - Creating Data Link (UDL) Files in Delphi
    - Looking at the Details for an Invoicing Appl...
    - Invoicing in Delphi: Show Me the Money
    - Saving Images and Binary Files to a Database...
    - Saving Files to a Database using Delphi: Sav...
    - Creating CF Applications and Integrating a S...
    - Using Try and Finally to Help Prevent Memory...
    - The Implementation of an FTP Server
    - FTP Server: The Theory







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway