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;
Next: Creating a New Group >>
More Delphi-Kylix Articles
More By Leidago