Re: Delphi: Showing table details

[ Delphi Forum -- by DelphiLand ]

Posted by webmaster Guido on December 10, 2003 at 18:31:03:

In Reply to: Re: sorting record from a DBgrid posted by webmaster Guido on December 05, 2003 at 18:21:53:

: : 2. still on the student database what coding procdure will i write to retrieve the various marks obtained in various subjects entered on a different form for a particular student.

Let's try a small project, based on the "students" problem.

First of all, we create a DBF table in Delphi's Database Desktop utility, for example: STUDENTS.DBF. Of course, you could also use a Paradox table or whatever table type that is suitable for your Delphi version.
In this simple example, we have only one table that contains all the data. It has
least alfanumeric (string) fields for ID, LASTNAME, FIRSTNAME, CLASS, plus a few numeric fields for exams results, e.g. ENGLISH, MATH, and so on...
We create two indexes: one on the full name (to show students in alfabetic order) and one on the field ID (this is a unique value for each student-record).

The project has two forms:

The main form FormStudents shows an overview of the students. It contains:
- a Table component, pointing to table Students.dbf with an index on the full name;
- a DataSource component;
- a DBGrid component.

A secondary form FormDetails, that is hidden until it is opened by code. It contains:
- a Table component, also pointing to table Students.dbf, but with index on ID;
- a DataSource component;
- a number of DBText components, one per field that you want to show;
- for clarity, you add some Labels next to the DBText components, with captions "Last Name", "First Name", "English", and so on...
- optional: a Button component, to close the form.

Now here's the trick: in the OnDblClick event of the DBGrid of FormStudens, you position the table on FormDetails to the correct record, and next you show FormDetails. FormDetails is closed again by clicking its OK-button.

I wrapped it up in a small project that you can download from this password protected page:
User name: dclub
Password: your membership access password

All interested DelphiLand Club members are invited to download the example :))
The source code files and a sample table are included. I also included some extra's in the code ;-)

In a real life situation, with exams that are quite different per class, I would strongly advise to have TWO tables, that are linked by a common field "ID":
- STUDENTS.DBF would countain the students "basic" fields such as ID, LASTNAME,...;
- EXAMS.DBF would have the fields ID, EXAM, and POINTS. Plus optionally a date, a comment, plus whatever.

But that's quite a different story :-)

[ Delphi Forum ]
[ DelphiLand: free Delphi source code, tips, tutorials ]