Building Contact Detail Maintenance into a Mailing List Program with Borland Delphi - The Less Complicated Code Bits
(Page 4 of 4 )
Now that we have the main code of form2 out of the way, let's deal with the less complicated ones. When the user chooses the "Add Contact to a group" option, the drop down box that appears needs to be filled with the group names from the groups table. So go to radiobutton1 which we renamed "rb1"and on its onclick event add the following code:
procedure TForm2.rb1Click(Sender: TObject);
begin
if rb1.Checked then begin
cb.Visible:=true;
ednewgroup.Visible:=false;
form1.query1.Close;
form1.query1.SQL.Text:='SELECT *from groups';
form1.query1.Open;
if form1.query1.fieldbyname('groupname').Text <> '' then begin
while not form1.query1.Eof do
begin
cb.Items.Add(form1.query1.fieldbyname('groupname').AsString);
form1.query1.Next;
end;
end
else begin
cb.Visible:=false;
end;
end;
end;
All that this code does is run a query to retrieve all the group names and then add them to the drop down box.
Now go to radiobutton2's onclick event (we renamed it to rb2) and double click on it. Add the following code:
procedure TForm2.rb2Click(Sender: TObject);
begin
if rb2.Checked then
begin
ednewgroup.Visible:=true;
cb.Visible:=false;
end
else begin
ednewgroup.Visible:=false;
end;
end;
The code above ensures that no components are visible that should not be visible when this radio button is selected.
Now we need to create a form that displays all the addresses on our mailing list. Add a new form to the project and save the unit as addresses. On the form, add a dbgrid component from the data controls tab and rename it dbgrid. Then double click on the dbgrid and add "Name" and "Email" as the column headers. Then add a button. The form should now look like this:

On the form's oncreate event, add the following code:
procedure TForm3.FormCreate(Sender: TObject);
begin
form1.ado1.TableName:='contacts';
form1.datasource1.DataSet:=ado1;
DBGrid1.DataSource:=form1.datasource1;
form1.ado1.Active:=true;
end;
The code above basically displays all the contact details. You can edit these records simply by selecting a record and typing your changes. Let's add the ability to remove records. Add another button and then add the following code:
procedure TForm3.BitBtn2Click(Sender: TObject);
begin
if messagedlg('Are you sure you want to remove '+dbgrid.SelectedField.Text+ ' ?',mtConfirmation, [mbYes, mbNo],
0) = mrYes
then
begin
form1.ado1.Delete;
end;
end;
In the above code we confirm whether the user really wants to delete the selected record, and if so we simply call the "delete" method and the record will be removed from the database.
Conclusion
We have looked at how to built a fully functional mailing list application, complete with a database and working code. The application can of course always be improved, but it does cover the basics, which are to send email messages simultaneously to everyone on the mailing list and also to add and remove contacts from the "address book." Remember to add your email connection details before testing the program.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |