Building Contact Detail Maintenance into a Mailing List Program with Borland Delphi - Creating a New Group
(Page 3 of 4 )
Section two of the code checks to see if you selected the "create new group" option. If you did, an edit text box will appear waiting to take your input. Before it does anything else, it will check to see if you entered a group name and then add the group name to the database:
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;
It then runs another query using the newly created groupname to find its groupID:
//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;
Once it has the groupID, the next step is to insert the contact details into the contacts table and inform the user that the contact details have been added.
//insert the new contact details...
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
If the user has not entered a groupname, then it will display the appropriate error message and stop the code from executing:
else
begin
showmessage('Please enter a groupname.');
exit;
end;
end;
Next, we need to tell the program what to do if the user did not select any of the aforementioned options. If the user does not want to add the new contact to a group or if the user does not want to create a new groupname, then the program should simply add the contact details to the database and set the groupID column to zero. After doing this it should inform the user that the operation was successful:
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;
Next: The Less Complicated Code Bits >>
More Delphi-Kylix Articles
More By Leidago