Recent

Author Topic: TDbf and Required fields  (Read 9018 times)

nik_pc

  • New Member
  • *
  • Posts: 10
TDbf and Required fields
« 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);

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: TDbf and Required fields
« Reply #1 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)
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

nik_pc

  • New Member
  • *
  • Posts: 10
Re: TDbf and Required fields
« Reply #2 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.

Lacak2

  • Guest
Re: TDbf and Required fields
« Reply #3 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.

nik_pc

  • New Member
  • *
  • Posts: 10
Re: TDbf and Required fields
« Reply #4 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?

Lacak2

  • Guest
Re: TDbf and Required fields
« Reply #5 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);

nik_pc

  • New Member
  • *
  • Posts: 10
Re: TDbf and Required fields
« Reply #6 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.

Lacak2

  • Guest
Re: TDbf and Required fields
« Reply #7 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.

nik_pc

  • New Member
  • *
  • Posts: 10
Re: TDbf and Required fields
« Reply #8 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.

Lacak2

  • Guest
Re: TDbf and Required fields
« Reply #9 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.)

Peter F

  • New Member
  • *
  • Posts: 13
Re: TDbf and Required fields
« Reply #10 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