Lazarus

Programming => Databases => Topic started by: nik_pc on March 23, 2011, 07:00:39 pm

Title: TDbf and Required fields
Post by: nik_pc on March 23, 2011, 07:00:39 pm
Hi all,
i'm trying to figure out how to set some fields in a TDbf table as mandatory. I supposed that the FieldDefs.Add function will do it, specifying the last parameter (Required) but nothing to do.
The fields are created but the Required flag is not set.
I've tried each dbf Level, from dBase III to FoxPro, but still not working.

Here's a sample of my code, what's wrong in it?

DbfFile.FieldDefs.Add('descr', 0, 7, True);
Title: Re: TDbf and Required fields
Post by: fabienwang on March 23, 2011, 08:07:28 pm
DbfFile.FieldDefs.Add( fieldname, type, size, required)

so for you it would be:


DbfFile.FieldDefs.Add( 'descr', ftString, 7, true);

PS: 7 chars seems a bit short for a description ^^
You can also use the type ftMemo

(types are defined in unit db)
Title: Re: TDbf and Required fields
Post by: nik_pc on March 24, 2011, 08:28:33 pm
I just wrote a sample here without taking care of the values, my real code is like you said.
You confirmed that the syntax is correct, but the field is not set to mandatory/required at all.
Title: Re: TDbf and Required fields
Post by: Lacak2 on March 25, 2011, 07:01:20 am
It seems to me, like bug in TDBF, there is procedure TDbf.InternalPost which must call inherited TDataSet.InternalPost, where are checked Required fields.
Title: Re: TDbf and Required fields
Post by: nik_pc on March 25, 2011, 06:52:34 pm
You mean that I have to modify the dbf.pas unit, adding a call to inherited in the InternalPost procedure?
Maybe I'm wrong, but reading InternalPost code, to me it seems it works on data edit or insert, and not on table/field creation.
Btw, supposing you're right, where should I put the inherited call?
Title: Re: TDbf and Required fields
Post by: Lacak2 on March 28, 2011, 08:04:07 am
in dbf.pas:
procedure TDbf.InternalPost; {override virtual abstract from TDataset}
var
  pRecord: pDbfRecord;
  I, newRecord: Integer;
begin
  inherited; //<---here add
  // if internalpost is called, we know we are active
  pRecord := pDbfRecord(ActiveBuffer);
Title: Re: TDbf and Required fields
Post by: nik_pc on March 28, 2011, 11:32:37 am
I've try your solution, but I'm sorry to say that the "inherited" don't solve the problem, and the fields still not set as required/mandatory.

I think that  the InternalPost function is used adding or editing rows, and not for table creation.
Title: Re: TDbf and Required fields
Post by: Lacak2 on March 29, 2011, 08:46:46 am
I think that  the InternalPost function is used adding or editing rows, and not for table creation.
Yes this is right.

May be, that I misunderstood your question  :(
AFAIK DBF does not support at table level - filed attributes (constraints) like required. So IMHO you can not create table which will hold in table definition such information.
Title: Re: TDbf and Required fields
Post by: nik_pc on March 29, 2011, 11:01:39 am
But then why the fourth parameter of the FieldDefs.Add function is called Required? And then why there is the Required property too for the FieldDefs of each field?
This is a lack.
Title: Re: TDbf and Required fields
Post by: Lacak2 on March 30, 2011, 07:05:27 am
And then why there is the Required property too for the FieldDefs of each field?
Because other databases support it (Oracle, Firebird, MySQL, SQlite, MS SQL, PostgreSQL, MS Access  etc.)
Title: Re: TDbf and Required fields
Post by: Peter F on April 06, 2011, 07:30:37 pm
Offcourse you can allways do it in code like i do;

if (IsEmpty(DBCombobox1.text)) or (IsEmpty(DBCombobox2.text)) or (IsEmpty(DBCombobox3.text)) or (IsEmpty(DBCombobox4.text)) then
   begin
      MessageDlg('I am sorry to inform you that all requird fields have to be filled.', mtInformation, [mbOk], 0);
      exit;

Cheers,

Peter
TinyPortal © 2005-2018