I have created a Program that first searches and returns the contents of a file into a richedit then opens up the next file finds the matching record and displays that in the rich edit and then the same again with another file.

My problem is that when i search it displays an EinOut Error File Not Found but the File Is there becasuse i wrote the records to it from the program.

Just so you can get an idea here is my code for the Procedure that fails it fails at the 'reset(productfile)' and 'reset(Servicefile)' i've highlighted them in red.

Any help would be majorly appreciated because i have to get this finished thanks in advance.

procedure TForm12.Button1Click(Sender: TObject);
var found:boolean; buyer:string; warranty:string; service:string; billing:string; product:string; postcode:string; name:string;
begin
found := false;
RichEdit1.clear;
assignfile(CustomerFile, Customer);
reset(CustomerFile);
while not eof (CustomerFile) do
read(CustomerFile, CustomerRecord);
begin
if (CustomerRecord.Name = Edit1.text) or (CustomerRecord.Postcode = edit2.Text) or (CustomerRecord.Surname = edit3.Text) then
begin
Richedit1.Lines.Add('Customer Details');
Richedit1.Lines.Add('');
Richedit1.Lines.Add('Title: '+ CustomerRecord.Title);
Richedit1.Lines.Add('Name: '+ CustomerRecord.Name);
Richedit1.Lines.Add('Surname: '+ CustomerRecord.Surname);
Richedit1.Lines.Add('Street: '+ CustomerRecord.Street);
Richedit1.Lines.Add('Town: '+ CustomerRecord.Town);
Richedit1.Lines.Add('County: '+ CustomerRecord.County);
Richedit1.Lines.Add('Postcode '+ CustomerRecord.Postcode);
Richedit1.Lines.Add('Telephone '+ CustomerRecord.Telephone);
Richedit1.Lines.Add('Mobile: '+ CustomerRecord.Mobile);
Richedit1.Lines.Add('Email: '+ CustomerRecord.Email);
Richedit1.Lines.Add('Notes: '+ CustomerRecord.Notes);
Richedit1.Lines.Add('');
Richedit1.Lines.Add('');
billing := CustomerRecord.billing;
buyer := CustomerRecord.Buying;
service := CustomerRecord.Service;
warranty := CustomerRecord.Warranty;
product := CustomerRecord.ProductS;
Postcode := CustomerRecord.Postcode;
name := CustomerRecord.Name;
found := true;
end;
If billing = 'y' then
Begin
Richedit1.Lines.Add('Billing Details');
Richedit1.Lines.Add('');
Richedit1.Lines.Add('Billing Title: '+ CustomerRecord.BTitle);
Richedit1.Lines.Add('Billing Name: '+ CustomerRecord.BName);
Richedit1.Lines.Add('Billing Surname: '+ CustomerRecord.BSurname);
Richedit1.Lines.Add('Billing Street: '+ CustomerRecord.BStreet);
Richedit1.Lines.Add('Billing Town: '+ CustomerRecord.BTown);
Richedit1.Lines.Add('Billing County: '+ CustomerRecord.BCounty);
Richedit1.Lines.Add('Billing Postcode: '+ CustomerRecord.BPostcode);
Richedit1.Lines.Add('Billing Telephone: '+ CustomerRecord.BTelephone);
Richedit1.Lines.Add('Billing Mobile: '+ CustomerRecord.BMobile);
Richedit1.Lines.Add('Billing Email: '+ CustomerRecord.BEmail);
Richedit1.Lines.Add('');
Richedit1.Lines.Add('');
End;
If warranty = 'y' then
Begin
Richedit1.Lines.Add('Warranty Details');
Richedit1.Lines.Add('');
Richedit1.Lines.Add('Warranty Start: '+ CustomerRecord.Wstart);
Richedit1.Lines.Add('Warranty Length: '+ CustomerRecord.WLength);
Richedit1.Lines.Add('');
Richedit1.Lines.Add('');
End;
If service = 'y' then
Begin
Richedit1.Lines.Add('Service Details');
Richedit1.Lines.Add('');
Richedit1.Lines.Add('Installation date: '+ CustomerRecord.IDate);
Richedit1.Lines.Add('New / Refurbished: '+ CustomerRecord.NR);
Richedit1.Lines.Add('Service Interval: '+ CustomerRecord.SInterval);
Richedit1.Lines.Add('Next Service: '+ CustomerRecord.NxtSDate);
Richedit1.Lines.Add('');
Richedit1.Lines.Add('');
End;
end;
closefile(CustomerFile);

assignfile(ProductFile, Product);
reset(ProductFile);
while not eof (ProductFile) do
read(ProductFile, ProductRecord);
begin
if ProductRecord.Serial = product then
Begin
Richedit1.Lines.Add('Product Details');
Richedit1.Lines.Add('');
Richedit1.Lines.Add('Product Serial: '+ ProductRecord.Serial);
Richedit1.Lines.Add('Make / Model: '+ ProductRecord.MakeModel);
Richedit1.Lines.Add('Date Of Purchase: '+ ProductRecord.DOP);
Richedit1.Lines.Add('Colour: '+ ProductRecord.Colour);
Richedit1.Lines.Add('Elec Or Manual: '+ ProductRecord.EorM);
Richedit1.Lines.Add('Price: '+ ProductRecord.Cost);
Richedit1.Lines.Add('Reference No.: '+ ProductRecord.RefNo);
End;
end;
closefile(ProductFile);

assignfile(ServiceFile, Service);
reset(ServiceFile);
while not eof (ServiceFile) do
read(ServiceFile, ServiceRecord);
begin
if (ServiceRecord.Nameofcustomer = name) and (ServiceRecord.postcodeofcustomer = postcode) then
Begin
Richedit1.Lines.Add('Date Of Service: '+ ServiceRecord.Dateofservice);
Richedit1.Lines.Add('Parts Used: '+ ServiceRecord.PartsUsed);
Richedit1.Lines.Add('Notes: '+ ServiceRecord.Notes);
End;
end;
closefile(ServiceFile);
if found = false then
Begin
showmessage('not on the file');
End;
end;

Recommended Answers

All 2 Replies

Sheady,

It is never very easy pinpointing errors based on very long code listings such as yours. Having taken a brief look at your code I have several suggestions to make

  • In my book any object procedure that exceeds around 20 lines of code needs to be broken up into multiple function/procedure calls.
  • You are handling several distinct tasks in your code so you could do that quite easily - and it will result in more readable code.
  • Are you aware of the with keyword? You might be able to use it to good effect here to write code that is easier to read and to maintain
  • Doing file operations without a try..except block to handle the unexpected is very unwise. At the very least you should have that.
  • ReSet returns an error in IOResult which might do with checking when the unexpected does happen
  • You might be better of doing one of the following in place of your AssignFile, ReSet... code
  1. Read the file into a TStringStream or a TStringList
  2. Check that the file exists using FileExists
  3. If the path to your file is being generated by code make sure that the path is correct by outputting it to DebugView - or a message box if you don't feel upto DebugView
  4. Finally, when things go wrong in code such as yours and logic does not help sort out the problem it is useful to follow a "divide and rule" strategy - systematically comment out portions of your code to help pinpoint the error

Perhaps you need to add ExtractFilePath(param(0)) + FileName to your file calls.

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.