Home arrow Delphi-Kylix arrow Page 2 - Building Contact Detail Maintenance into a Mailing List Program with Borland Delphi
DELPHI-KYLIX

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


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.

Author Info:
By: Leidago
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
October 04, 2006
TABLE OF CONTENTS:
  1. · Building Contact Detail Maintenance into a Mailing List Program with Borland Delphi
  2. · Breaking Down the Code
  3. · Creating a New Group
  4. · The Less Complicated Code Bits

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Building Contact Detail Maintenance into a Mailing List Program with Borland Delphi - Breaking Down the Code
(Page 2 of 4 )

Let's break this code down. First the program checks to see if the user has entered the name and email address of the contact. If not, an appropriate error message is shown and the program stops executing:

if (edname.text = '') or (edemail.Text = '') then begin
showmessage('Please enter a name and email address');
exit;
end;

Next the program checks to see if the user selected the "add contact to group" option. If the user did so, the program runs a query to find the groupID and assign that value to a variable called "new_id:"

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;

You might wonder why we need the groupID. Well, the whole purpose of this code is to insert a new contact, and as you know, the contacts table in the database will need a groupID to identify what group, if any, the contact belongs to. That's why we need the groupID of the selected groupname. With the groupID found and assigned to the "new_id" variable, we now add the entire contact details to the contacts table:

//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

Next we need to make sure that the program knows what to do if the query does not find a groupID, or if the returned groupID value is less than one, or if the user does not select a groupname. The code below handles all these scenarios:

  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;


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials