I don't know what the proper name is for it lol, but I have an assignment. I am fairly new to delphi. I understand the homework rules so I don't expect it all to be done I just need help in the understanding and feel free to help as much as you want.

Create a delphi program that will allow a user to save employee details. The system should save an employeeID, surname, forname and salary.

I appreciate any help given...
This is the assignment details

Requirements
store an employee record with the attributes employeeID, surname, forname and salary;
be able to add a new record;
be able to view a record;
provide navigation to move one record forward and one record back (use seek)
be able to delete a record;

The file must be saved in sequential order which means you will need to write an algorithm for correctly inserting the record. You will also add a button to delete a record based on the key field a user selects.

I have done some but I can't get it at the moment because of some server problem. The part I need help with is the sequntial ordering and a pointer to make the files viewable and go to next and previous.

Recommended Answers

All 6 Replies

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TNewFileNameLbl = class(TForm)
    EmployLbl: TLabel;
    Surname: TLabel;
    ForenameLbl: TLabel;
    SalaryLbl: TLabel;
    SurnameEdt: TEdit;
    ForenameEdt: TEdit;
    SalaryEdt: TEdit;
    EmployEdt: TEdit;
    Savebtn: TButton;
    ClearBtn: TButton;
    ReloadBtn: TButton;
    EraseBtn: TButton;
    RenameEdt: TButton;
    NewFileNameLbl: TLabel;
    NewFileNameEdt: TEdit;
    procedure ClearBtnClick(Sender: TObject);
    procedure SavebtnClick(Sender: TObject);
    procedure ReloadBtnClick(Sender: TObject);
    procedure EraseBtnClick(Sender: TObject);
    procedure RenameEdtClick(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  End;
    Employees=record
    EmployeeID:String[5];
    Surname:string[20];
    Forename:string[20];
    salary:string[8];
  end;

var
  NewFileNameLbl: TNewFileNameLbl;
  Employee:Employees;
  Employeesrecord:Employees;
  EmployeeFile:File of Employees;

implementation
{$R *.dfm}

procedure TNewFileNameLbl.ClearBtnClick(Sender: TObject);
begin
    Employedt.text:='';
    Surnameedt.text:='';
    Forenameedt.text:='';
    salaryedt.text:='';
end;

procedure TNewFileNameLbl.SaveBtnClick(Sender: TObject);
var
  Employeefile:file of Employees;

begin
Assignfile(employeefile, 'staff.dat');

IF FileExists('staff.dat') THEN
  RESET(EmployeeFile)
ELSE
  REWRITE(EmployeeFile);

  WITH Employeesrecord DO
  begin
    EmployeeID:=Employedt.text;
    Surname:=Surnameedt.text;
    Forename:=Forenameedt.text;
    salary:=salaryedt.text;

  end;

  assignfile(Employeefile,'staff.dat');
  reset(Employeefile);
  Seek(Employeefile,filesize(Employeefile));
  write(Employeefile,Employeesrecord);
  closefile(Employeefile);

end;

procedure TNewFileNameLbl.reloadBtnClick(Sender: TObject);

var
  Employeefile:file of Employees;

begin
  assignfile(Employeefile,'staff.dat');
  reset(Employeefile);
  read(Employeefile,Employeesrecord);
  closefile(Employeefile);

with Employeesrecord do
  begin
    EmployEdt.text:=EmployeeID;
    Surnameedt.text:=Surname;
    Forenameedt.text:=Forename;
    salaryedt.text:=salary;
  end;
end;

procedure TNewFileNameLbl.EraseBtnClick(Sender: TObject);
Var
EmployeeFile:TextFile;
begin
  AssignFile(EmployeeFile, 'staff.dat');
  Rewrite(EmployeeFile);
  CloseFile(EmployeeFile);
  Erase(EmployeeFile);
end;

procedure TNewFileNameLbl.RenameEdtClick(Sender: TObject);
var
  EmployeeFile:TextFile;
  text:string;

begin
  AssignFile(EmployeeFile, 'staff.dat');
  ReWrite(EmployeeFile);
  CloseFile(EmployeeFile);
  DeleteFile('NewName.txt');
  Rename(EmployeeFile, 'NewName.txt');
  AssignFile(EmployeeFile, 'NewName.txt');
  Reset(EmployeeFile);
while not Eof(EmployeeFile) do
  begin
    ReadLn(EmployeeFile, text);
    ShowMessage(text);
  end;
  CloseFile(EmployeeFile);

end;

end.

I know I'm new but I plan to stick around and get better and help out as much as I can, I can't get better without help,

Thanks, much appreciated.

Lynxus, I guess you are coming to Delphi from a Pascal background? In general it doesn't make much sense to use the Pascal file functions in Delphi. Streaming data to/from disk is better handled using the TFileStream and TMemoryStream classes. In your case, even that is not necessary - just use the native methods of TStringList instead.

If I understand your assignment correctly you are required to provide means of handling a simple flat file database. Why not simply store the database as a text file? Individual records would be CRLF delimited. Within each record the individual fields would be delimited using a safe character, e.g. |.

The structure would be

FirstNameA|LastNameA|EmployeeIDA|SalaryA
FirstNameB|LastNameB|EmployeeIDB|SalaryB
.......
FirstNamen|LastNamen|EmployeeIDn|Salaryn

Use the Delphi TStringList class to manage this database. Look at Delphi help on TStringList
for starters. Pay particular attention to the following properties and methods

  • Strings[]
  • Count
  • Text
  • CommaText
  • Insert
  • Delete
  • SaveToFile
  • LoadFromFile

Let the SaveToFile & LoadFromFile methods handle all streaming operations. You can use the Strings array to navigate through records in your database. The Insert & Delete methods can be used to add/delete individual employee records.

I suggest using | as a field separator so you can have spaces and commas in your individual field entries. Building records from supplied data is easy. Displaying existing records involves breaking up the record at each | to find its individual fields. Use the Pos function to do this.

As a general observation always try to make good use of Delphi stock objects such as TList, TStringList, TFileStream, TMemoryStream etc. Take some time to explore the Classes unit where these and other objects reside. It doesn't make much sense to reinvent the wheel.

You are correct that I have came from a pascal background, however the assignment requires me to use what I've learned in pascal and some delphi to do this.

Thanks for your help.

You are correct that I have came from a pascal background, however the assignment requires me to use what I've learned in pascal and some delphi to do this.

Thanks for your help.

I would say that making good use of Delphi stock classes is perfectly legit and shows great initiative. However, ultimately it is your teacher who will decide.

Yeah thanks, but he said we are going to go into more depths with delphi and not to go into them yet. I'm getting on with it though and think i'm almost done...

There's nothing wrong with saving a record directly to file like he does. All it means is that he only needs to dink with one thing at a time in the file instead of many. In fact, that's the whole point behind a record.

There are a number of organizational problems with your code, but as they aren't critical I won't bother you with them (unless you want).

The main feature of the assignment is that you are not supposed to load the file at any point: instead you are to only "view" a single record of the file at any given time.

Lets consider how a file of record looks:

[record 0][record 1][record 2][record 3]...

That is, it is a sequential list of records. To read or overwrite a specific record, just seek to the beginning of that record and read or write it.

So, the first thing you need is a way to seek out a specific record in the file, given its index. I suggest you make yourself a little function to do it, like: procedure indexEmployeeNumber( index: cardinal ); If the file is too short to have the 'index'ed record, just seek to the end of the file.

Next, your assignment asks you to keep track of which record you are currently viewing. That is, you must remember the index of the current record: 0, 1, 2, .... When you are asked to move to the previous or next record, adjust your index variable appropriately and use the indexEmployeeNumber utility procedure to select the record, and if valid, load and display it.
You can avoid seeking to invalid records by knowing how many records are in the file: file size in bytes div record size in bytes.

Finally, you must be able to insert and delete records. Since a file isn't a linked list, you can't just push a record in-between two others. You must overwrite a record. Here is a hint:

Given the array 'abqrz', insert 'k' in its sorted place.
Well, first I must find where to insert:
index 0: 'a' < 'k'
index 1: 'b' < 'k'
index 2: 'q' not less than 'k' --so I'll insert at 2.

The trick now is to move the contents of indices greater than or equal to 2 one index to the right. This is an exercise for you. You might do something like:
abkrz (saving q)
abkqz (saving r)
abkqr (saving z)
abkqrz (all done)
Or you might do something like:
abqrzz
abqrrz
abqqrz
abkqrz

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.