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.
Building Contact Detail Maintenance into a Mailing List Program with Borland Delphi (Page 1 of 4 )
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;