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.

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:

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);vargname:string;new_id:integer;grpid,newgrp:boolean;beginnew_id:=0;if (edname.text = '') or (edemail.Text = '') then beginshowmessage('Please enter a name and email address');exit;end;//purpose: to get the groupID of the selected groupnameif 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 WHEREgroupname=: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 thegroupID 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 contactto.'+#13#10+ 'If you do not see the group name on the list thencreate 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 WHEREgroupname=: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+ ' andcontact: '+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;