Lazarus
Programming => Databases => Topic started 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);
-
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)
-
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.
-
It seems to me, like bug in TDBF, there is procedure TDbf.InternalPost which must call inherited TDataSet.InternalPost, where are checked Required fields.
-
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?
-
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);
-
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.
-
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.
-
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.
-
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.)
-
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