Syouchi
asked on
how to set database's default values
Hello everyone.
I want to insert a default value in fields when new record is appended.
And I want to write this code in a procedure that a table is created.
Is there any property or method to implement this?
Table type is Paradox.
My code is as following.
------------------------
FieldDefs.Clear;
FieldDefs.Add('number',ftS tring,4,fa lse);
FieldDefs.Add('name',ftStr ing,36,fal se);
FieldDefs.Add('gender',ftS tring,2,fa lse);
FieldDefs.Add('age',ftStri ng,2,false );
..
..
..
IndexDefs.Clear;
IndexDefs.Add('','number', [ixPrimary ]);
CreateTable;
Open;
Edit;
//p[i] is array of field's name.
for i:=0 to High(p) do
FieldByName(p[i]).asString :='False';
Post;
-------------------------- -----
Now default values is inserted by last 3 lines's code.
But every new record have to pass this code.
I want to write more cool code.
In fact. MS-Access is possible to set a default value in creating table.
Thanks for your help.
Syouchi.
I want to insert a default value in fields when new record is appended.
And I want to write this code in a procedure that a table is created.
Is there any property or method to implement this?
Table type is Paradox.
My code is as following.
------------------------
FieldDefs.Clear;
FieldDefs.Add('number',ftS
FieldDefs.Add('name',ftStr
FieldDefs.Add('gender',ftS
FieldDefs.Add('age',ftStri
..
..
..
IndexDefs.Clear;
IndexDefs.Add('','number',
CreateTable;
Open;
Edit;
//p[i] is array of field's name.
for i:=0 to High(p) do
FieldByName(p[i]).asString
Post;
--------------------------
Now default values is inserted by last 3 lines's code.
But every new record have to pass this code.
I want to write more cool code.
In fact. MS-Access is possible to set a default value in creating table.
Thanks for your help.
Syouchi.
Specifically you should fill the Validity checks structure properly before creating table. If you have delphi professional or higher see the bdtables.pas TTable.CreateTable code and modify the VChkDesc accordingly before calling DbiCreateTable:
The structure is as follows:
pVCHKDesc = ^VCHKDesc;
VCHKDesc = packed record { Val Check structure }
iFldNum : Word; { Field number }
bRequired : WordBool; { If True, value is required }
bHasMinVal : WordBool; { If True, has min value }
bHasMaxVal : WordBool; { If True, has max value }
bHasDefVal : WordBool; { If True, has default value } <-give this to true
aMinVal : DBIVCHK; { Min Value }
aMaxVal : DBIVCHK; { Max Value }
aDefVal : DBIVCHK; { Default value } <-give here your defaul value
szPict : DBIPICT; { Picture string }
elkupType : LKUPType; { Lookup/Fill type }
szLkupTblName : DBIPATH; { Lookup Table name }
end;
Then when you just write
Table1.Insert;
Table1.Post;
You have a new record with default values. The other checks are also done from above structure such as min and max values for a field. However, if you are not involved I do not recomend you to play with Dbi low level functions...
So, do you really need this?
Regards, Igor
The structure is as follows:
pVCHKDesc = ^VCHKDesc;
VCHKDesc = packed record { Val Check structure }
iFldNum : Word; { Field number }
bRequired : WordBool; { If True, value is required }
bHasMinVal : WordBool; { If True, has min value }
bHasMaxVal : WordBool; { If True, has max value }
bHasDefVal : WordBool; { If True, has default value } <-give this to true
aMinVal : DBIVCHK; { Min Value }
aMaxVal : DBIVCHK; { Max Value }
aDefVal : DBIVCHK; { Default value } <-give here your defaul value
szPict : DBIPICT; { Picture string }
elkupType : LKUPType; { Lookup/Fill type }
szLkupTblName : DBIPATH; { Lookup Table name }
end;
Then when you just write
Table1.Insert;
Table1.Post;
You have a new record with default values. The other checks are also done from above structure such as min and max values for a field. However, if you are not involved I do not recomend you to play with Dbi low level functions...
So, do you really need this?
Regards, Igor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If your program is the only one that will touch the database you could define the defaults on the client side with the BeforeInsert Event. You can also set a property for the field with something like
Table1.Active := False;
Table1.Fields[0].DefaultEx pression := '''hello''';
Table1.Active := True;
This can also be done at design time by:
1. Double-click the dataset to invoke the Fields editor.
2. Select the field for which to set properties.
(You'll have to add the fields if you haven't done it yet)
3. Set the DefaultExpression propertie for the field in the Object Inspector. (Rember to put quotes around it, i.e. 'hello')
again this Default Expression is a client side thing and only applies when the user runs this program...so if you have other programs that will read this database it would be best to use iter's solution...but this might be simpler for a small application.
Table1.Active := False;
Table1.Fields[0].DefaultEx
Table1.Active := True;
This can also be done at design time by:
1. Double-click the dataset to invoke the Fields editor.
2. Select the field for which to set properties.
(You'll have to add the fields if you haven't done it yet)
3. Set the DefaultExpression propertie for the field in the Object Inspector. (Rember to put quotes around it, i.e. 'hello')
again this Default Expression is a client side thing and only applies when the user runs this program...so if you have other programs that will read this database it would be best to use iter's solution...but this might be simpler for a small application.
ASKER
Thanks.
I try to use the table's OnNewRecord event.
Syouchi
I try to use the table's OnNewRecord event.
Syouchi
I am sorry but as far as I know thats only possible with low level BDE call DbiCreateTable instead of the one specified above.
Regards, Igor