Title: How many days to the next Birthday of one of my friends?
Question: You will have a programm to check at every start which of
my friends have Birthday and when.
Answer:
create a table (e.g. MyBirthday.db) with this fields:
Nbr + *
BirthDay D // (e.g. 07.09.2001)
Age S // (e.g. 30)
Day A 2 // (e.g. Mo, Tu,...)
BirthDate D // (e.g. 07.09.1971)
FirstName A 30
//....
place on Form1
a Table-object named BirthDay
a DataSource1 and
a DBGrid1
// the Procedures createSX() and DBGrid1Sort() are found on article:
// 'How to sort DBGrid-Columns, create/delete secondary Indizes'
For all other Countries then Germany (e.g. USA ...) you must
change the names in the days-array only.
const days: array[1..7] of string =
('So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa');
// create secondary indices to sort the dates
procedure TForm1.FormCreate(Sender: TObject);
begin
// ....
CreateSX(GebTg, 'BirthDay');
CreateSX(GebTg, 'BirthDate');
// ....
end;
// open the table and compute the new BirthDays
procedure TForm1.FormShow(Sender: TObject);
begin
// ...
BirthDay.open;
BirthDays;
end;
// get the weekday-name
function TForm1.DOF(aDate: TDateTime): string;
begin
result := days[DayOfWeek(aDate)];
end;
// compute all BirthDays new
procedure TForm1.BirthDays;
var
gDate, gYear, gMonth, gDay, hYear, hMonth, hDay: TDateTime;
begin
with BirthDay do begin
first;
DisableControls;
while not eof do begin
edit;
if not (FieldByName('BirthDate').isNull) then begin // BirthDate exists
gDate := BirthDay['BirthDate'];
// decode the BirthDate and the current date
DecodeDate(gDate, gYear, gMonth, gDay);
DecodeDate(now, hYear, hMonth, hDay);
// compute the BirthDay by using current Year
// plus Month and Day from BirthDate
BirthDay['Day'] := EncodeDate(hYear, gMonth, gDay);
// compute the Age by current year minus Birthdate-Year
BirthDay['Age'] := hYear - gYear;
// compute the weekday name
BirthDay['Day'] := DOF(BirthDay['BirthDay']);
end else
// no BirthDay given - set value to null
BirthDay['BirthDay'] := Null;
next;
end;
First;
EnableControls;
IXField := 'BirthDay';
// sort the grid by BirthDay
DBGrid1Sort(BirthDay, 'BirthDay');
// position to next BirthDay after today
BirthDay.FindNearest([Now]);
// set focus to firstname-field
DBGrid1.setfocus;
DBGrid1.SelectedField := BirthDay.FieldByName('FirstName');
end;
end;
You can place a Button on Form1 to call the procedure BirthDays()
after you have inserted or updated any records.
Regards
Kurt