Solved

how to set database's default values

Posted on 1998-09-03
5
383 Views
Last Modified: 2010-04-03
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',ftString,4,false);
     FieldDefs.Add('name',ftString,36,false);
     FieldDefs.Add('gender',ftString,2,false);
     FieldDefs.Add('age',ftString,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.
0
Comment
Question by:Syouchi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1338523
Hi,
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
0
 
LVL 5

Expert Comment

by:inter
ID: 1338524
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
0
 

Accepted Solution

by:
oberdan earned 50 total points
ID: 1338525
Hi,

      Try to use the table's OnNewRecord event. The OnNewRecord
event occurs anytime a new record is added to a table. You can
use it to supply values for a table's columns.
      Here is a sample:

procedure TForm1.Table1NewRecord(DataSet: TDataSet);
begin
      Table1Number.Value:=1; //or any other value
        Table1Name.AsString:='John Doe';
end;

Where Table1Number and Table1Name are table fields that can be
acessed by Fields Editor (double click at TTable Component and
select add on Popup menu).

      I hope it can help you.


                                        []'s

                              Oberdan
0
 
LVL 1

Expert Comment

by:Greedy
ID: 1338526
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].DefaultExpression := '''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.

0
 

Author Comment

by:Syouchi
ID: 1338527
Thanks.
I try to use the table's OnNewRecord event.

Syouchi
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question