How to create fields programatically? lazarus / 16-Databases ---------------------------------------------------------------------- TurboRascal · 2012-09-01 17:58 I wanted to try something from a previous post here, but I chose to do it in a simple command line program and I've faced a problem I don't have in a Lazarus-managed GUI programs. I have to create all objects manually, but I've find out I have no idea how to create a TField instance! I've tried this according to the FCL documentation: Code: [Select]DataSet.Fields.Add(Field); Also this, simply using the constructor, as usual: Code: [Select]Field := TField.Create(nil); Whatever I did, either I got an access violation (SIGSEGV on Linux) or if I did not and then tried using the field in the dataset's Fields[n] property, I got an "index out of bounds" error even with the index being zero. I've checke the documentation, FCL docs do have TFields documented, but it is very brief and nothing of it works in this example. The only information I found on the wiki, in the documentation about databases, is a redlink with a promise there will be explained how to programatically create fields, but so far nobody has written it. So to recap once again, nothing that works in a GUI Lazarus app works here, and also I couldn't do it properly in the way objecs are usally managed.  So, obviously there is another step required, or is something done differently . Could somebody explain how is it done? Then perhaps we could finally complete the above mentioned wiki documentation EDIT: The dataset used was TMemDataset, so it is possible it affects the required steps... ---------------------------------------------------------------------- taazz · 2012-09-01 23:39 Code: [Select]  MemDataset1.FieldDefs.Add('FirstName',ftstring, 40);   MemDataset1.FieldDefs.Add('LastName',ftstring, 60);   MemDataset1.FieldDefs.Add('HomePhone',ftstring, 20);   MemDataset1.CreateTable; In short you create field definitions and the fields are created for you when the dataset is activated or created. ---------------------------------------------------------------------- TurboRascal · 2012-09-03 11:51 Quote from: taazz on September 02, 2012, 01:39:17 amCode: [Select]  MemDataset1.FieldDefs.Add('FirstName',ftstring, 40);   MemDataset1.FieldDefs.Add('LastName',ftstring, 60);   MemDataset1.FieldDefs.Add('HomePhone',ftstring, 20);   MemDataset1.CreateTable; In short you create field definitions and the fields are created for you when the dataset is activated or created. Thank you, this works fine without using TField. Obviously the FieldDefs does that which was previously automatically done for me. I do get an error, but perhaps it's a bug in my svn snapshot. I'll check with a fresh one and then with 1.0 if it fails, to make sure what the problem is. I'm still not sure how to properly initialize and use TField from scratch though... Any hints? ---------------------------------------------------------------------- taazz · 2012-09-03 12:09 The following is the CreateField as found in the TFieldDef class (removed the debug staff for clarity) QuoteCode: [Select]Function TFieldDef.CreateField(AOwner: TComponent): TField; Var TheField : TFieldClass; begin   TheField:=GetFieldClass;   if TheField=Nil then     DatabaseErrorFmt(SUnknownFieldType,[FName]);   Result:=Thefield.Create(AOwner);   Try     Result.Size:=FSize;     Result.Required:=FRequired;     Result.FFieldName:=FName;     Result.FDisplayLabel:=DisplayName;     Result.FFieldNo:=Self.FieldNo;     Result.SetFieldType(DataType);     Result.FReadOnly:= (faReadOnly in Attributes);     Result.Dataset:=TFieldDefs(Collection).Dataset;     If (Result is TFloatField) then       TFloatField(Result).Precision:=FPrecision;     if (Result is TBCDField) then       TBCDField(Result).Precision:=FPrecision;     if (Result is TFmtBCDField) then       TFmtBCDField(Result).Precision:=FPrecision;   except     Result.Free;     Raise;   end; end; As far as I can see you need to set all field's properties especially fieldname, size etc before you set the field's dataset and add it to the dataset's fields collection before the dataset is opened/activated. Regards ---------------------------------------------------------------------- TurboRascal · 2012-09-03 15:12 Great, thanks! I'll add this to the wiki to resolve the red link