Ok, im making my first ever address book, and in Delphi 7. I have the following code:
Code:
unit Details;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TfrmDetails = class(TForm)
txtName: TEdit;
lblName: TLabel;
txtDateOfBirth: TEdit;
lblDateOfBirth: TLabel;
txtAddress: TMemo;
lblAddress: TLabel;
lblHomeNumber: TLabel;
txtHomeNumber: TEdit;
txtMobileNumber: TEdit;
lblMobileNumber: TLabel;
txtEmailAddress: TEdit;
lblAIMContact: TLabel;
txtNotes: TMemo;
lblNotes: TLabel;
cmdCancel: TBitBtn;
cmdOK: TBitBtn;
Procedure AddEntry(Fn,Ad,HP,M,E,N:string);
procedure cmdCancelClick(Sender: TObject);
procedure cmdOKClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmDetails: TfrmDetails;
Rgn:THandle;
implementation
uses Main;
{$R *.dfm}
Procedure TfrmDetails.AddEntry(Fn,Ad,HP,M,E,N:string);
begin
if Not fileExists(QFileName) then rewrite(QFile)
else if Not Edit then begin
i:=Fsize;
end
else begin
frmMain.cboNameList.Items.Delete(i);
i:=Found;
end;
seek(QFile,i);
with Entries do begin
FullName:=Fn;
Address:=ad;
Hphone:=hp;
Mobile:=m;
Email:=E;
Note:=N;
write(QFile,Entries);
end;
closefile(QFile);
reset(QFile);
fsize:=filesize(QFile);
frmMain.cboNameList.Items.add(txtName.text);
i:=frmMain.cboNameList.Items.IndexOf(txtName.text);
frmMain.cboNameList.ItemIndex:=i;
end;
procedure TfrmDetails.cmdCancelClick(Sender: TObject);
begin
Close;
end;
procedure TfrmDetails.cmdOKClick(Sender: TObject);
var n:integer;
begin
if Not Edit then begin
for n:=0 to frmMain.cboNameList.Items.count-1 do begin
if txtName.text=frmMain.cboNameList.Items[n] then begin
MessageBox(handle,'The name already exists! Type a different name.',
'Name Exists',MB_Ok);
exit;
end;
end;
end;
AddEntry(txtName.text,txtAddress.text,txtHomeNumber.text,txtMobileNumber.text,txtEmailAddress.text,txtNotes.text);
frmMain.seekRecord;
if Rgn<>0 then DeleteObject(Rgn);
frmDetails.Close;
frmDetails.Release;
end;
end.
But when i click my OK button, it errors. DOnt know the problem, anyone care to assist?
As you have provided no details of the error I am stabbing in the dark.
Arn't you supposed to use the assign procedure to associate the variable QFile with the file location before using any other file I/O functions? Plus if the file does exist you don't even open it.
Code:
Assign (QFile, QFileName);
if Not fileExists(QFileName) then rewrite(QFile)
else if Not Edit then begin
Append (QFile);
i:=Fsize;
end
else begin
frmMain.cboNameList.Items.Delete(i);
i:=Found;
end;
Ok, i did a check now to find out whether the file exists in APP.Path, and if it doesnt to prompt the user to create the file:
Code:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ToolWin, StdCtrls, Menus, ImgList, mmsystem, ShellApi,
Mask;
type
TfrmMain = class(TForm)
procedure mnuFileNewContactClick(Sender: TObject);
procedure mnuFileEditContactClick(Sender: TObject);
Procedure SeekRecord;
procedure mnuFileDeleteContactClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
QRecord = record
FullName:string[40];
Address:string[150];
DateOfBirth:string[40];
Hphone,Mobile:string[20];
Email:string[50];
Note:string[100];
end;
var
frmMain: TfrmMain;
Entries: QRecord;
QFile, TempFile: File of QRecord;
i,FSize, Found:integer;
Deleted, Edit:boolean;
QFileName,TempFileName: string;
implementation
uses Details;
{$R *.dfm}
Procedure TfrmMain.SeekRecord;
var n:Integer;
begin
for n:=0 to frmMain.cboNameList.Items.count-1 do begin
seek(Qfile,n);
Read(QFile,Entries);
if entries.FullName=frmMain.cboNameList.Items[cboNameList.ItemIndex] then begin
with Entries do begin
with frmMain do begin
txtAddress.text:=Address;
txtHomeNumber.Text:=Hphone;
txtMobileNumber.Text:=Mobile;
txtEmailAddress.Text:=Email;
txtNotes.Text:=Note;
cboNameList.ItemIndex:=i;
end;
Found:=n;
break;
end;
end;
end;
end;
procedure TfrmMain.mnuFileNewContactClick(Sender: TObject);
begin
frmDetails.Caption:= 'Add new contact';
frmDetails.ShowModal;
end;
procedure TfrmMain.mnuFileEditContactClick(Sender: TObject);
begin
frmDetails.Caption:= 'Edit contact';
frmDetails.ShowModal;
end;
procedure TfrmMain.mnuFileDeleteContactClick(Sender: TObject);
begin
if cboNameList.Text = 'No contacts in address book' then
ShowMessage('There is no contact to delete from Address Book')
else
//
end;
procedure TfrmMain.FormCreate(Sender: TObject);
var n:integer;
begin
TempFileName:=ExtractFilePath(application.exename)+'RecTemp.dat';
AssignFile(TempFile,TempFileName);
if FileExists(TempFileName) then Erase(TempFile);
QFileName:=ExtractFilePath(application.exename)+'Records.dat';
Assignfile(QFile, QFileName);
if FileExists(QfileName) then begin
reset(QFile);
FSize:=FileSize(QFile);
if FSize>0 then begin
for n:=0 to Fsize-1 do
begin
seek(Qfile,n);
Read(QFile,Entries);
cboNameList.Items.add(Entries.FullName);
end;
cboNameList.ItemIndex:=0;
SeekRecord;
end
end
else begin
if Application.MessageBox('Address Book requires new data file in order to save contacts'+#13#10+#13#10+'Would you like to save it now?',
'Address Book', MB_YesNo)=IDyes then begin
ReWrite(QFile);
Reset(QFile);
i:=0;
end;
end;
i:=0;
end;
Which works perfect, now when i click File > New contact from me menu, fill in required details, and click OK it works great too. Only problem is, when i close my app, and rerun it, it errors again:
I dont know really unless i make the app myself cuz i'm just begining Delphi also but would this help?
//---
Assignfile(QFile, QFileName);
if FileExists(QfileName) then begin
reset(QFile);
IF IOResult >0 THEN BEGIN
for n:=0 to Fsize-1 do
//---
Last problem, i hope How do you count the number of items in a Combobox? I have:
procedure TfrmMain.mnuViewStatisticsClick(Sender: TObject);
var
S:String;
i:Integer;
begin
For I:=0 To cboNameList.Items.Count - 1 do
S:= S + cboNameList.Items.Text[i] + ' ';
ShowMessage('There are '+ S +' contacts available in the Address Book')
end;
end.
But that just shows the text in the combo box, not the amount of items
I have now sucesfully got a working array
I can now add new contacts, delete them and show them when i load my program up. There is one problem though, in which if i add a new contact, then click File > New again, it errors. I dont know why it errors (maybe i need to close the file, if so how?). If thats not the problem, maybe i can shove an error handle in there somewhere.
See that? I would never of come close to making something like this in VB, Delphi seems to take p**s out of VB, hahahaha.
So ill try that method, first though, how do you close a file? The reason i ask is, i mentioned that i can create a new contact with no problems, but trying to add a new one straight after errors. Well if i add a contact, close the program then re-open, i can add a contact with no problems. So i must be getting an overflow error trying to write to the file whilst it is already open, thats my theory anyway.
Any other suggestions appreciated
EDIT: Those error handles stated above, thats just for IO operations right? I only want to know how to do a simlilar thing to On Error Resume Next , nothing else, forget the files part, this is a question irrelated to IO
//---
Assignfile(QFile, QFileName);
if FileExists(QfileName) then begin
reset(QFile);
FSize:=FileSize(QFile);
if FSize>0 then begin
for n:=0 to Fsize-1 do
begin
seek(Qfile,n);
Read(QFile,Entries);
cboNameList.Items.add(Entries.FullName);
end;
cboNameList.ItemIndex:=0;
SeekRecord;
CloseFile(QFile); // added this
end
end
else begin
//---
Before if i tried to add a new contact, then another it errored as soon as i clicked File > New. Now this doesnt happen, it shows my form to add the new contact, but when i click OK it errors, maybe i need to open the file when the form loads? If not im confused